473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Connect Four Game problem

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
7 8510
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
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
koonda
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
JosAH
11,448 Expert 8TB
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
alidou
1
what is the code for 2D board to connect4?
how many classes contains the projects connect4?
May 20 '07 #6
JosAH
11,448 Expert 8TB
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
koonda
34
Thanks all for your help I really appreciate that.

Koonda
May 20 '07 #8

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

Similar topics

3
by: MaKiT | last post by:
I have a program which on start makes a 4x4 grid of tiles. These tiles are all blank. Two Array variables hold the position of 8 pairs of images. Then when you click a tile it is uncovered and you...
2
by: Jasper | last post by:
Hi All Iam using the libmysql.lib in my C++ app, I have a mySQL database setup on a remote server something.dyndns.org and using port 2000, I use the following little bit of code to try and...
5
by: koonda | last post by:
Hi all, I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the...
5
by: Shermanater182 | last post by:
I need a simple way to code a connect four game for vb. I have no idea how to make or code it yet because i had last choice in picking a game to code im stuck with it. any help is appreciated.
2
by: =?iso-8859-1?Q?Vicente_Garc=EDa?= | last post by:
Hello all, I have an a bit strange printer and I would like to connect a C# program running on my computer to the printer. I know that the printer can be connected by LAN (TCP/IP protocol,...
10
by: aboolamoola | last post by:
hi, I recently started programing about a month ago. my teacher ask me to make a game. A connect 4 game. I try to make, but failed. I need help. here is my code: import java.awt.*; import...
1
by: Carl Banks | last post by:
On Apr 11, 12:08 pm, "Neil Cerutti" <mr.ceru...@gmail.comwrote: That depends. No, it's prudence. If I am writing a banal, been-done-a-million-times before, cookie- cutter text adventure,...
19
by: foolsmart2005 | last post by:
I have written a snake game. There are 2 levels in the game(I finished 1st level). It can run in VC++ without problem but, when I run it on the dev C++ 4.9.9.2, it cannot run. I want to...
1
by: Netwatcher | last post by:
i have a problem with the following (some vaiation of a word quiz game) (ignore first group of hashs) import random WordTank='' GuessTank='' GuessList=12 GuessWords=...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.