Connect four

Hey i am writing a connect four program that uses a 2d array can some one help me understand the logic behind it?like how to write it i know that you are suppose to choose a column then they fill up from the bottom. This is what i have so far

import java.util.*;

import java.io.*;

import javax.swing.*;

class ConnectFour{

publicstaticvoid main (String args[]){

try{

int[][] ConnectFourArray =newint[6][7] ;

int[] column0={ConnectFourArray[0][0],ConnectFourArray[1][0],ConnectFourArray[2][0],ConnectFourArray[3][0],

ConnectFourArray[4][0],ConnectFourArray[5][0]};

int num1, num2;

BufferedReader stdin =new BufferedReader (new InputStreamReader(System.in));

String player1;

System.out.println ("What is your Name, player 1?");

player1 = stdin.readLine();

System.out.println ("Hey "+player1+" are you prepared to CONNECT FOUR");

System.out.println();

BufferedReader stdin2 =new BufferedReader (new InputStreamReader(System.in));

String player2;

System.out.println ("What is your Name, player 2?");

player2 = stdin2.readLine();

System.out.println ("Hey "+player2+" are you ready, its time to square off against "+player1);

for(int limit=21;limit!=0;limit--){

BufferedReader selecter =new BufferedReader (new InputStreamReader(System.in));

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);

column1 = selecter.readLine();

num1= Integer.parseInt(column1);

System.out.println();

if (num1==0){

for (int i=5;i!=-1;i--){

int[] temp={1,0,0,0,0,0,0};

ConnectFourArray[i]=temp;

}

}

elseif (num1==1){

int[] temp={0,1,0,0,0,0,0};

ConnectFourArray[5]=temp;

}

elseif (num1==2){

int[] temp={0,0,1,0,0,0,0};

ConnectFourArray[5]=temp;

}

elseif (num1==3){

int[] temp={0,0,0,1,0,0,0};

ConnectFourArray[5]=temp;

}

elseif (num1==4){

int[] temp={0,0,0,0,1,0,0};

ConnectFourArray[5]=temp;

}

elseif (num1==5){

int[] temp={0,0,0,0,0,1,0};

ConnectFourArray[5]=temp;

}

elseif (num1==6){

int[] temp={0,0,0,0,0,0,1};

ConnectFourArray[5]=temp;

}

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[i]);

}*/}

System.out.println();

}

catch (Exception E){

System.out.println("Error with input");

}

}

}

>

[6215 byte] By [link33333a] at [2007-11-26 14:42:53]
# 1
the logic? http://en.wikipedia.org/wiki/Connect_Four
CeciNEstPasUnProgrammeura at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 2
thanks
link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 3
is there away to make the column read vertical rather then horizontal?
link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 4

> is there away to make the column read vertical rather

> then horizontal?

vertical? horizontal? You have a 2D array, there is no such thing as orientation. :) Anyway, what do you mean by "read"? Just iterate over the other coordinate, if that's what you need.

By the way, have you heard about the switch statement?

CeciNEstPasUnProgrammeura at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 5

well i want the program to drop them in so i made a revision here is my code now. But I can only get one value per row because i can't figure out how to go up in each column i understand that i am just replacing the array and thats why i am getting what i am, but how do i fix that? whats the switch statement?

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;

BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

String player1;

System.out.println ("What is your Name, player 1?");

player1 = stdin.readLine();

System.out.println ("Hey "+player1+" are you prepared to CONNECT FOUR");

System.out.println();

BufferedReader stdin2 = new BufferedReader (new InputStreamReader(System.in));

String player2;

System.out.println ("What is your Name, player 2?");

player2 = stdin2.readLine();

System.out.println ("Hey "+player2+" are you ready, its time to square off against "+player1);

for(int limit=21;limit!=0;limit--){

BufferedReader selecter = new BufferedReader (new InputStreamReader(System.in));

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);

column1 = selecter.readLine();

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[i]);

}*/

//increase=increase+1;

}

System.out.println();

}

catch (Exception E){

System.out.println("Error with input");

}

}

}

>

link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 6

Now that I look closer about it, your logic is flawed.

- Have a Board class that encapsulates the arrays and has something like a

public boolean int(int column, Player p)

method. Input: the player (represented in whichever way oyu like, even a boolean) and the column. It'll add one piece to the appropriate column. You can use a 2D array for that, or an array of LinkedLists (preferably). Remember that you need to be able to increment the columns independently. Your logic can't do that.

It could e.g. return 2 if there's a win situation, 0 for the next turn or 1 for a draw (board full) and -1 for an illegal move (add to a filled column).

It should of course also have methods to query the columns to see what the board looks like - it could return a int[][] e.g.

The advantage will be that if you learn Swing later, you can simply re-use the same class for a GUI-based game.

CeciNEstPasUnProgrammeura at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 7
I dont know how to make a board class:( my assignment just says "You should use a 2 dimensional array to store the values (7 x 6). " with a connect four game i wasn't taught a board class.
link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 8
Well, I couldn't smell it's a homework assignment. It's your task to solve that.
CeciNEstPasUnProgrammeura at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 9
> Well, I couldn't smell it's a homework assignment.> It's your task to solve that.The OP has also created 3 threads with the same question.
kajbja at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 10
i know i wanted to increase the possibility of someone helping me
link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 11
> i know i wanted to increase the possibility of> someone helping meYou are actually reducing it. Many of us don't like cross/multi-posts
kajbja at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 12
:'( im sorry i am just hardcore stuck:'(
link33333a at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...
# 13
You should in that case direct all readers to one single thread, e.g. this one: http://forum.java.sun.com/thread.jspa?threadID=5123410(since the other one is tagged as a cross-post and links to that one)
kajbja at 2007-7-8 8:30:29 > top of Java-index,Java Essentials,New To Java...