Testing a 2D array for four elements at once- Game (Connect Four)
Hey,
This is my first forum at this specific location, so if my jargon or anything else is screwed, just ignore it....
Ok i've coded this game "Connect Four" where you drop a chip into a column until you/opponent wins. you can win by connecting 4 chips diagonally, horz, vert. etc...
Do you get the pic.
I haven't implemented graphics yet, because i'm stuck in the programs guts. You should notice two functions for win detection (win_check & dir_check) they don't work...
Please help!
import ann.easyio.*;
// THESE ARE MY CONSTANTS
publicclass connectfour
{
publicstaticfinalint SIZEc = 7;
publicstaticfinalint SIZEr = 6;
publicstaticfinalchar EMPTY ='+';
publicstaticfinalchar P1 ='X';
publicstaticfinalchar P2 ='O';
publicstaticvoid main(String[] args)
{
// I'M USING A BOOLEAN TO INDICATE A WIN/LOSE SITUATION
boolean did_win =false;
String p1_name, p2_name;
Keyboard input =new Keyboard();
System.out.println("Player "+P1+"'s name is...");
p1_name = input.readLine();
System.out.println("Player "+P2+"'s name is...");
p2_name = input.readLine();
System.out.println("Welcome to Connect four "+p2_name+" and "+p1_name+", \n I'll be your host, and no you don't need my name. \n \n I expect that you know the rules already so let's begin...");
//CALLING THE GAME FUNCTION, TO PLAY A GAME OF CONNECT FOUR
did_win = play_game(input,p1_name,p2_name);
}
// FIRST WIN-CHECK FUNCTION
publicstaticboolean win_check(char[][] chart,char player,int i,int j)
{
return (
dir_check(chart,player,i,j,0,1)||
dir_check(chart,player,i,j,1,0)||
dir_check(chart,player,i,j,1,1)||
dir_check(chart,player,i,j,-1,1));
}
//SECOND(FINAL) WIN-CHECK FUNCTION
publicstaticboolean dir_check(char[][]chart,char player,int i,int j,int di,int dj)
{
int sum=1;
int x,y;
x=i+di;
y=j+dj;
while(x>=0 && x<SIZEr && y><SIZEc && chart[i][j]==player)
{
x+=di;
y+=dj;
sum++;
}
x=i-di;
y=j-dj;
while(x><SIZEr && x>=0 && y>=0 && chart[i][j]==player)
{
x-=di;
y-=dj;
sum++;
}
return (sum>=4);
}
// MAIN GAME FUNCTION
publicstaticboolean play_game(Keyboard input, String p1_name, String p2_name)
{
char[][] chart =newchar[SIZEr][SIZEc];
boolean done =false;
fill_array(chart);
while(done ==false)
{
print_me(chart);
//PLAYER ONE GO...
done = player_cord(chart, p1_name,input, 1 );
print_me(chart);
statement(done,'X');
if(done ==true)
break;
//PLAYER 2 GOES...
done = player_cord(chart, p2_name,input, 2);
print_me(chart);
statement(done,'O');
if(done ==true)
break;
}
returntrue;
}
//GETTING THE PLAYER'S COORDINATES
publicstaticboolean player_cord(char[][] chart, String player,Keyboard input,int which)
{
int j = 0,
i = 0,
p = 0;
boolean dont =true;
boolean done =false;
while(dont)
{
System.out.println("Your up "+player+" Give me a slot number.");
j = input.readInt();
if(j>=SIZEc || j<0)
continue;
for(p=0;p<SIZEr;p++)
{
if(chart[p][j] == EMPTY)
i = p;
}
if(chart[0][j] != EMPTY)
continue;
if(which == 1)
{ chart[i][j] = P1;
done = win_check(chart,'X', i, j);
}
if(which == 2)
{chart[i][j] = P2;
done = win_check(chart,'O', i , j);
}
dont =false;
}
return done;
}
//WIN- CONTINUE STATEMENTS
publicstaticvoid statement(boolean win,char player)
{
if(win ==true)
System.out.println("Player "+player+" won!");
if(win ==false)
System.out.println("Keep going...");
}
//FILLING INITIAL ARRAY FULL OF EMPTY CHARACTERS
publicstaticvoid fill_array(char[][] chart)
{
int i,j;
for(i=0;i<SIZEr;i++)
{
for(j=0;j<SIZEc;j++)
{
chart[i][j] = EMPTY;
}
}
}
//PRINTING ARRAY
publicstaticvoid print_me(char[][] chart)
{
int i,j;
System.out.println("");// Space between chart and previous work
// System.out.print(" ");
for(i=0;i<SIZEc;i++)
System.out.print(i+" ");
System.out.println("");
for(i=0;i<SIZEr;i++)
{
for(j=0;j<SIZEc;j++)
{
System.out.print(chart[i][j]+" ");
}
System.out.println("");
}
}
}
>

