By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,990 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,990 IT Pros & Developers. It's quick & easy.

Connect Four Game problem

P: 34
Hi all,

I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The Assignment is about creating a Connect Four Game. I have designed the board and two players can play and fill the board. But when one playe fills 4 columns that player should win. I have problems implementing it like this. The other problem is to decide about Vertical, Horizontal and Diagonal win. Would you guys be able to help me with these problems for connect four game. I really appreciate your help. Below is the hasWon() method which decides for Horizontal Win. But it is not working and it does not decide about the win and some times it give ArrayIndexOutofBound Exception.




//check for horizontal win

public int hasWon()
{
int status = 0;


for (int row=0; row<6; row++)
{
for (int col=0; col<4; col++)
{
if (ConnectFourArray[col][row] != 0 &&
ConnectFourArray[col][row] == ConnectFourArray[col+1][row] &&
ConnectFourArray[col][row] == ConnectFourArray[col+2][row] &&
ConnectFourArray[col][row] == ConnectFourArray[col+3][row])
{status = 1;}
//status = true;//int winner;

if(status == 1)
{


System.out.println("Player 1 is the winner");
}

else if(status == 0)
{


System.out.println("Player 2 is the winner" );
}

}//end inner for loop
}// end outer for loop
} // end method Winner

return status;
}





Try to also send me some code so I can finish my project by then.

Thanks a lot for your help. Looking forward to your reply.

Koonda
May 15 '07 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
Hi all,

I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The Assignment is about creating a Connect Four Game. I have designed the board and two players can play and fill the board. But when one playe fills 4 columns that player should win. I have problems implementing it like this. The other problem is to decide about Vertical, Horizontal and Diagonal win. Would you guys be able to help me with these problems for connect four game. I really appreciate your help. Below is the hasWon() method which decides for Horizontal Win. But it is not working and it does not decide about the win and some times it give ArrayIndexOutofBound Exception.




//check for horizontal win

public int hasWon()
{
int status = 0;


for (int row=0; row<6; row++)
{
for (int col=0; col<4; col++)
{
if (ConnectFourArray[col][row] != 0 &&
ConnectFourArray[col][row] == ConnectFourArray[col+1][row] &&
ConnectFourArray[col][row] == ConnectFourArray[col+2][row] &&
ConnectFourArray[col][row] == ConnectFourArray[col+3][row])
{status = 1;}
//status = true;//int winner;

if(status == 1)
{


System.out.println("Player 1 is the winner");
}

else if(status == 0)
{


System.out.println("Player 2 is the winner" );
}

}//end inner for loop
}// end outer for loop
} // end method Winner

return status;
}





Try to also send me some code so I can finish my project by then.

Thanks a lot for your help. Looking forward to your reply.

Koonda
1.) When posting code, please use code tags.
2.) We won't write the codes and send them to you. We can only help you to write them correctly and efficiently.
3.) If you are sure your design is correct, write your code part by part and post if you get problems on any specific area.
May 15 '07 #2

Expert 10K+
P: 11,448
I simply repeat my previous answer from your other thread here and I'm really
curious what you didn't understand about my hint:

If your 'logical' board measures 7x6 you should use a 'physical' board of 9x8:
Expand|Select|Wrap|Line Numbers
  1. +---+---+---+---+---+---+---+---+---+
  2. | X | X | X | X | X | X | X | X | X |
  3. +---+---+---+---+---+---+---+---+---+
  4. | X |   |   |   |   |   |   |   | X |
  5. +---+---+---+---+---+---+---+---+---+
  6. | X |   |   |   |   |   |   |   | X |
  7. +---+---+---+---+---+---+---+---+---+
  8. | X |   |   |   |   |   |   |   | X |
  9. +---+---+---+---+---+---+---+---+---+
  10. | X |   |   |   |   |   |   |   | X |
  11. +---+---+---+---+---+---+---+---+---+
  12. | X |   |   |   |   |   |   |   | X |
  13. +---+---+---+---+---+---+---+---+---+
  14. | X |   |   |   |   |   |   |   | X |
  15. +---+---+---+---+---+---+---+---+---+
  16. | X | X | X | X | X | X | X | X | X |
  17. +---+---+---+---+---+---+---+---+---+
the 'X' values represent a non-empty square not taken by any of the two players.
That way you don't need to do any explicit boundary checks in any of your
methods. The logical playing board ranges from [1 ... 7] and [1 ... 6] and any
cell outside that range is invalid (although it physically exists).

w.r.t. my second remark: if none of the players has won and a current move
causes one of the players to win then the position of that last current move
is part of the winning configuration. If it weren't, a previous configuration would
have been a winning situation for one of the players therefore contradicting the
previous assumption that none of the players did win already.

So checking in any possible directions starting from that last move is sufficient
to find a possible winning configuration.

kind regards,

Jos
May 15 '07 #3

P: 34
Thank you all for your help. Now the game is working, I only have some problem to implement the Automatic player. I made the two human players working, they play with each and one wins. But I have a little problem with the Automatic Player I mean the Computer Player. The Automatic Player plays with the Human player but never wins. I tried my best to make very bad moves for the Human Player to let the Computer win but it won't. I just want the Automatic Player to win.

Here is the code for the Automatic Play. Would you guys make some corrections in the code if it is wrong. But it still plays and generates random numbers.

Expand|Select|Wrap|Line Numbers
  1. public int move () { 
  2.         try {
  3.  
  4.             Random automaticplayer = new Random();
  5.  
  6.             int randomnumber;
  7.  
  8.             randomnumber = automaticplayer.nextInt(6);
  9.  
  10.             return randomnumber;
  11.  
  12.                     } catch (Exception exn) {
  13.             return (this.move ());
  14.         }
  15.     }
Again thanks a lot for your help I really appreciate that. Looking forward to your reply.

Thanks

Koonda
May 17 '07 #4

Expert 10K+
P: 11,448
The optimal (or a near optimal) strategy for a connect-four game hasn't found yet
so you have to fall back to sub-optimal heuristics here. One of the worst is a
random move (as you did). Google for "alpha-beta pruning" for quite some clever
heuristic game search routines.

kind regards,

Jos
May 17 '07 #5

P: 1
what is the code for 2D board to connect4?
how many classes contains the projects connect4?
May 20 '07 #6

Expert 10K+
P: 11,448
what is the code for 2D board to connect4?
how many classes contains the projects connect4?
Short answers:

1) nobody knows; it's a secret.
2) see above.

Longer answers:

you do realize that there are several implementations of that game, spread all
over the world and every single implementation uses different ideas, methods
and classes. All the implementations try to aim for the same thing: play the
Connect-4 game, either with two human players, a human against the computer
and even AI-based programs where two computers play against each other and
both computers attempt to learn from their mistakes.

I hope you realize that your question cannot be answered in a unique way.

kind regards,

Jos
May 20 '07 #7

P: 34
Thanks all for your help I really appreciate that.

Koonda
May 20 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.