1 2 Previous Next 15 Replies Latest reply on Jan 10, 2007 8:21 PM by 807599

# connect four algorithm

Does any one have a connect four algorithm?
• ###### 1. Re: connect four algorithm
I searched and this was my first relevant hit: http://www.siteexperts.com/games/connect4/page1.asp?
• ###### 2. Re: connect four algorithm
i just need it for human vs. human thanks, can someone share?
• ###### 3. Re: connect four algorithm
What do you mean by "algorithm", then? If you want a human-vs-human game, all you have to do is respond to clicks and declare the final winner.
• ###### 4. Re: connect four algorithm
yea thats what i mean
• ###### 5. Re: connect four algorithm
yea thats what i mean
Where are you stuck?
• ###### 6. Re: connect four algorithm
I have set up the start, i have to use a 2d array to display my data. But I can't figure out how to access individual points in the array. so far the only thing that i can figure out is that i have replaced the array(2d arrays are a collection of arrays) so the there are 6 rows i replace a row everytime, but that means that i can only have one value in the row. I am using 1 as player1s colour and 2 as player2s colour. so i am stuck right now at how to drop the peices into 2 or more sections of the rows. here is my code
``````import java.util.*;
import java.io.*;
import javax.swing.*;

class ConnectFour{
public static void main (String args[]){
try{
int[][] ConnectFourArray = new int[6][7] ;
//int[] column0={ConnectFourArray[0][0],ConnectFourArray[1][0],ConnectFourArray[2][0],ConnectFourArray[3][0],
//                    ConnectFourArray[4][0],ConnectFourArray[5][0]};
int cup0,cup1,cup2,cup3,cup4,cup5,cup6;
cup0=5;cup1=5;cup2=5;cup3=5;cup4=5;cup5=5;cup6=5;

int num1, num2;
String player1;
System.out.println ("What is your Name, player 1?");
System.out.println ("Hey "+player1+" are you prepared to CONNECT FOUR");
System.out.println();

String player2;
System.out.println ("What is your Name, player 2?");
System.out.println ("Hey "+player2+" are you ready, its time to square off against "+player1);

for(int limit=21;limit!=0;limit--){
String column1;
System.out.println();
for ( int row=0; row < ConnectFourArray.length; row++ ){
System.out.print("Row " + row + ": ");
for ( int col=0; col < ConnectFourArray[row].length; col++ )
System.out.print( ConnectFourArray[row][col] + " ");
System.out.println();
}
System.out.println();
System.out.println ("Please Select a column of 0 through 6, "+player1);
num1= Integer.parseInt(column1);
System.out.println();

if (num1==0){
int[] temp={1,0,0,0,0,0,0};
ConnectFourArray[cup0]=temp;
cup0=cup0-1;
}
else if (num1==1){
int[] temp={0,1,0,0,0,0,0};
ConnectFourArray[cup1]=temp;
cup1=cup1-1;
}
else if (num1==2){
int[] temp={0,0,1,0,0,0,0};
ConnectFourArray[cup2]=temp;
cup2=cup2-1;
}
else if (num1==3){
int[] temp={0,0,0,1,0,0,0};
ConnectFourArray[cup3]=temp;
cup3=cup3-1;
}
else if (num1==4){
int[] temp={0,0,0,0,1,0,0};
ConnectFourArray[cup4]=temp;
cup4=cup4-1;
}
else if (num1==5){
int[] temp={0,0,0,0,0,1,0};
ConnectFourArray[cup5]=temp;
cup5=cup5-1;
}
else if (num1==6){
int[] temp={0,0,0,0,0,0,1};
ConnectFourArray[cup6]=temp;
cup6=cup6-1;
}
System.out.println();
/*for ( int row=0; row < ConnectFourArray.length; row++ ){
System.out.print(" ");
for ( int col=0; col < ConnectFourArray[row].length; col++ )
System.out.print( ConnectFourArray[row][col] + " ");
System.out.println();
}

for (int i=5;i<column0.length;i--){
System.out.println(column0);
}*/
//increase=increase+1;
}
System.out.println();
}
catch (Exception E){
System.out.println("Error with input");
}

}
} ``````
• ###### 7. Re: connect four algorithm
Step back and think about design. A major concept is the board. Concepts become classes. What operations does the board expose? How will you test them to see if they work? consider the user interface later. This same board class should be able to work with an application that uses console output, Swing, a web application, human players/algorithms, etc...
• ###### 8. Re: connect four algorithm
Step back and think about design. A major concept is
the board. Concepts become classes. What operations
does the board expose? How will you test them to see
if they work? consider the user interface later. This
same board class should be able to work with an
application that uses console output, Swing, a web
application, human players/algorithms, etc...
ok the board then. My board must be a 2d array and when i select a column the object goes into the lowest available slot. but i need some kind of method to sort the columns right? thats what i tried before but the arrays were just replaced. i can test them by using the set up interface that i have that displays the array after every entry.
• ###### 9. Re: connect four algorithm
I don't see a need to sort. What does sorting complish?

The way I see it, if a new piece is to be placed in the first column, I would loop down that column and find if any pieces were already in the column.

*) If not, the new piece is placed in the bottom row of that column.
*) Otherwise, if the top-most existing piece is in row R, the new piece will be place in the row above R. Does that make sense?
• ###### 10. Re: connect four algorithm
yes that makes perfect sense, i grasped that before, but say that you where
entering a peice into column 4 and how would you do that, can you just replace
one object rather then the whole column if so i dont know how:(
• ###### 11. Re: connect four algorithm
Don't replace rows or columns -- why do that?

Consider this sample grid:
``````int[][] data = {
{0,0,0,0},
{0,0,0,1},
{0,0,1,1},
{0,1,1,1}
};``````
Column 0 has no 1's
Column 1 has its first 1 in row 3
Column 2 has its first 1 in row 2
Column 3 has its first 1 in row 1

First, write some code that discovers that.
• ###### 12. Re: connect four algorithm
something like this?
``````int num1;//column entered by the player

String column1;
System.out.println();
System.out.println ("Please Select a column of 0 through 6, "+player1);
num1= Integer.parseInt(column1);
System.out.println();

if (num1=0){
if (data[5][0]=0){
data[5][0]=1
}
else if ([5][0]=1){
if(data[4][0]=0){
data[4][0]=1}
}
...
}``````
• ###### 13. Re: connect four algorithm
First, forget about trapping user input -- just write a function like:
``public int getOpenRowForColumn(int Column) {...}``
More importantly - write a loop! Otherwise you will write 100 times the code you need to. Can you write a simple loop that search for the row?
• ###### 14. Re: connect four algorithm
ok here i changed my original code to this i like it better and now they stack up check it out:D
``````import java.util.*;
import java.io.*;
import javax.swing.*;

class ConnectFour{
public static void main (String args[]){
try{
int[][] ConnectFourArray = new int[6][7] ;
//int[] column0={ConnectFourArray[0][0],ConnectFourArray[1][0],ConnectFourArray[2][0],ConnectFourArray[3][0],
//                    ConnectFourArray[4][0],ConnectFourArray[5][0]};
int cup0,cup1,cup2,cup3,cup4,cup5,cup6;
cup0=5;cup1=5;cup2=5;cup3=5;cup4=5;cup5=5;cup6=5;

int num1, num2;
String player1;
System.out.println ("What is your Name, player 1?");
System.out.println ("Hey "+player1+" are you prepared to CONNECT FOUR");
System.out.println();

String player2;
System.out.println ("What is your Name, player 2?");
System.out.println ("Hey "+player2+" are you ready, its time to square off against "+player1);

for(int limit=21;limit!=0;limit--){
String column1;
System.out.println();
for ( int row=0; row < ConnectFourArray.length; row++ ){
System.out.print("Row " + row + ": ");
for ( int col=0; col < ConnectFourArray[row].length; col++ )
System.out.print( ConnectFourArray[row][col] + " ");
System.out.println();
}
System.out.println();
System.out.println ("Please Select a column of 0 through 6, "+player1);
num1= Integer.parseInt(column1);
System.out.println();

if (num1==0){
ConnectFourArray[cup0][0]=1;
cup0=cup0-1;
}
else if (num1==1){
ConnectFourArray[cup1][1]=1;
cup1=cup1-1;
}
else if (num1==2){
ConnectFourArray[cup2][2]=1;
cup2=cup2-1;
}
else if (num1==3){
ConnectFourArray[cup3][3]=1;
cup3=cup3-1;
}
else if (num1==4){
ConnectFourArray[cup4][4]=1;
cup4=cup4-1;
}
else if (num1==5){
ConnectFourArray[cup5][5]=1;
cup5=cup5-1;
}
else if (num1==6){
ConnectFourArray[cup6][6]=1;
cup6=cup6-1;
}
System.out.println();

String column2;
System.out.println();
for ( int row=0; row < ConnectFourArray.length; row++ ){
System.out.print("Row " + row + ": ");
for ( int col=0; col < ConnectFourArray[row].length; col++ )
System.out.print( ConnectFourArray[row][col] + " ");
System.out.println();
}
System.out.println();

System.out.println ("Please Select a column of 0 through 6, "+player2);
num1= Integer.parseInt(column1);
System.out.println();

if (num1==0){
ConnectFourArray[cup0][0]=2;
cup0=cup0-1;
}
else if (num1==1){
ConnectFourArray[cup1][1]=2;
cup1=cup1-1;
}
else if (num1==2){
ConnectFourArray[cup2][2]=2;
cup2=cup2-1;
}
else if (num1==3){
ConnectFourArray[cup3][3]=2;
cup3=cup3-1;
}
else if (num1==4){
ConnectFourArray[cup4][4]=2;
cup4=cup4-1;
}
else if (num1==5){
ConnectFourArray[cup5][5]=2;
cup5=cup5-1;
}
else if (num1==6){
ConnectFourArray[cup6][6]=2;
cup6=cup6-1;
}
System.out.println();
/*for ( int row=0; row < ConnectFourArray.length; row++ ){
System.out.print(" ");
for ( int col=0; col < ConnectFourArray[row].length; col++ )
System.out.print( ConnectFourArray[row][col] + " ");
System.out.println();
}

for (int i=5;i<column0.length;i--){
System.out.println(column0);
}*/
}
System.out.println();
}
catch (Exception E){
System.out.println("Error with input");
}

}
}

Message was edited by: