473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Urgent Help Required by Implementing a ConnectFour Game

34 New Member
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 internet which helped me little bit. But there are lot of problems I am facing developing the whole game. I have drawn the Board and the two players can play. The players numbers can fill the board, but I have problem implementing the winner for the game. I need to implement the hasWon() method for Horizontal win, Vertical win and Diagonal win. I also found some code examples on the net but I was unable to make it working. I have 5 classes and one interface which I m implementing. The main problem is how to implement the hasWon() method in the PlayGame class below with Horizontal, vertical and diagonal moves.


Sorry there is so much code.

This the interface I must implement, but now I am only implementing the int move() of this interface. I will implement the rest later after solving the winner problem with your help.

Interface code........... ...
interface Player {
void init (Boolean color);
String name ();
int move ();
void inform (int i);

}

Player1 class.......... ............

import java.util.*;
import java.io.*;
import javax.swing.*;





public class Player1 implements Player
{

public Player1()
{}
public int move()
{

Scanner scan = new Scanner(System. in);
// BufferedReader stdin = new BufferedReader (new InputStreamRead er(System.in));
int player1;
System.out.prin tln ("What is your Number, player 1?");
player1 = scan.nextInt();
System.out.prin tln ("Hey number"+player1 +" are you prepared to CONNECT FOUR");
System.out.prin tln();
return player1;

//Player.move();
//return player1;

}//end move method




public void init (Boolean color)
{}
public void inform (int i)
{}


public String name()
{
return "Koonda";
}

}//end player1 class




Player2 class.......... ............... ..

import java.util.*;
import java.io.*;
import javax.swing.*;





public class Player2 implements Player
{

public int move()
{

//int cup0,cup1,cup2, cup3,cup4,cup5, cup6;
// cup0=5;cup1=5;c up2=5;cup3=5;cu p4=5;cup5=5;cup 6=5;

//int num1, num2;
Scanner scan = new Scanner(System. in);
// BufferedReader stdin = new BufferedReader (new InputStreamRead er(System.in));
int player2;
System.out.prin tln ("What is your Number, player 2?");
player2 = scan.nextInt();
System.out.prin tln ("Hey "+player2+" are you prepared to CONNECT FOUR");
System.out.prin tln();
//return player1;




return player2;

}//end move method




public void init (Boolean color)
{}
public void inform (int i)
{}


public String name()
{
return "malook";
}

}//end player1 class


PlayGame class which contains all the functionality.. ............... ............... ............... ..........


import java.util.*;
import java.io.*;
import javax.swing.*;


public class PlayGame
{
//Player player1;
//Player player2;
int [][]ConnectFourArra y;
boolean status;
int winner;
int player1;
int player2;
public PlayGame()
{
//this.player1 = player1;
//this.player2 = player2;
}

public void StartGame()
{

try{
// int X = 0, Y = 0;
//int value;
int cup0,cup1,cup2, cup3,cup4,cup5, cup6;
cup0=5;cup1=5;c up2=5;cup3=5;cu p4=5;cup5=5;cup 6=5;
int[][] ConnectFourArra y = new int[6][7];
int num1, num2;
for(int limit=21;limit! =0;limit--)
{
BufferedReader selecter = new BufferedReader (new InputStreamRead er(System.in));
String column1;
System.out.prin tln();
for ( int row=0; row < ConnectFourArra y.length; row++ ){
System.out.prin t("Row " + row + ": ");
for ( int col=0; col < ConnectFourArra y[row].length; col++ )
System.out.prin t( ConnectFourArra y[row][col] + " ");
System.out.prin tln();
}
System.out.prin tln();
System.out.prin tln ("Please Select a column of 0 through 6 ");
column1 = selecter.readLi ne();
num1= Integer.parseIn t(column1);
System.out.prin tln();

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


BufferedReader selecter2 = new BufferedReader (new InputStreamRead er(System.in));
String column2;
System.out.prin tln();
for ( int row=0; row < ConnectFourArra y.length; row++ ){
System.out.prin t("Row " + row + ": ");
for ( int col=0; col < ConnectFourArra y[row].length; col++ )
System.out.prin t( ConnectFourArra y[row][col] + " ");
System.out.prin tln();
}
System.out.prin tln();

System.out.prin tln ("Please Select a column of 0 through 6 ");
column1 = selecter.readLi ne();
num1= Integer.parseIn t(column1);
System.out.prin tln();

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







}
System.out.prin tln();
}
catch (Exception E){
System.out.prin tln("Error with input");
}





System.out.prin tln("Would you like to play again");
try{
{

String value;
BufferedReader reader = new BufferedReader (new InputStreamRead er(System.in));
// Scanner scan = new Scanner(System. in);
System.out.prin tln("Enter yes to play or no to quit");
// value = scan.nextLine() ;
// String value2;
value = reader.readLine ();
//value2 = reader.readLine ();
if (value.equals(" yes"))
{

System.out.prin tln("Start again");
StartGame(); // calling the StartGame method to play a game once more
}
else if (value.equals(" no"))
{

System.out.prin tln("No more games to play");
// System.exit(0);
}
else
{

System.exit(0);
}

}
System.out.prin tln();
}
catch (Exception e){
System.out.prin tln("Error with input");
}

finally
{

System.out.prin tln(" playing done");
//StartGame();


}


}








//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 (ConnectFourArr ay[col][row] != 0 &&
ConnectFourArra y[col][row] == ConnectFourArra y[col+1][row] &&
ConnectFourArra y[col][row] == ConnectFourArra y[col+2][row] &&
ConnectFourArra y[col][row] == ConnectFourArra y[col+3][row])
{
//status = true;//int winner;

if(status == player1)
{


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

else if(status == player2)
{


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

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

return status;
}








}//end class



ClassConnectFou r which designs the board.......... ..............

import java.util.*;
import java.io.*;
import javax.swing.*;


public class ClassConnectFou r
{
//Player player1;
//Player player2;
public ClassConnectFou r()
{
//this.player1 = player1;
}

public void DrawBoard()
{
int[][] ConnectFourArra y = new int[6][7] ;
for ( int row=0; row < ConnectFourArra y.length; row++ ){
System.out.prin t("Row " + row + ": ");
for ( int col=0; col < ConnectFourArra y[row].length; col++ )
System.out.prin t( ConnectFourArra y[row][col] + " ");
System.out.prin tln();
}
System.out.prin tln();

}


}//end class


TestConnetFour class which uses most of the above class.......... ........

import java.util.*;
import java.io.*;
import javax.swing.*;


public class TestConnectFour
{

public static void main(String[] args)
{

ClassConnectFou r cf = new ClassConnectFou r();
cf.DrawBoard();


Player1 player1 = new Player1();
Player2 player2 = new Player2();




player1.move();
player2.move();
System.out.prin tln("Number 1 belongs to player " + player1.name()) ;
System.out.prin tln("Number 2 belongs to player " + player2.name()) ;
PlayGame pg = new PlayGame();
pg.StartGame();
pg.hasWon();



//pg.Play();
//System.out.prin tln(player.name ());
//System.out.prin tln(player2.nam e());
}// end main


}//end class


I am sorry for all this junk code but I only understand it this way. Your urgent help is required. Looking forward to your reply.

Thanks in advance.

Koonda






//
May 12 '07 #1
5 3537
dmjpro
2,476 Top Contributor
good approach.

do one thing.
try to represent the board as a matrix with the help of two-dimesional array.
i think u can do it.
then do mimmum decision making equations by which u can reach at a decision who won.
that means .... when a mark in done by a player check three conditions on the basis of current marked square.
if sequential vertical or horizontal or diagonal mark is done by that player then that player won.

mark the sqaures with unique character for each player.
check every time when a player marks a square and update the square with player's indentification character.

i think u got my point ..... so immediate implement this ... if any problem is there plz post ur hasWon method ..

i think i ll able to correct ur bug if have any.
best of luck.

kind regards.
dmjpro.
May 13 '07 #2
JosAH
11,448 Recognized Expert MVP
There's a little very practical trick: the board measures 7x6 cells (if I'm not
mistaken). Make the real board 9x8 where the border cells contain sentinel
values (i.e. unequal to the three possible values). That way you never have to
explicitly check your indexes for crossing a border.

Another observation is: suppose there's no winner. If, after the current move
there does happen to be a winner, the position of the last move is part of the
winning configuration. All you have to do is track in horizontal, vertical and
diagonal directions starting at that last move's position and count for the
number of coins in that direction.

kind regards,

Jos
May 13 '07 #3
koonda
34 New Member
good approach.

do one thing.
try to represent the board as a matrix with the help of two-dimesional array.
i think u can do it.
then do mimmum decision making equations by which u can reach at a decision who won.
that means .... when a mark in done by a player check three conditions on the basis of current marked square.
if sequential vertical or horizontal or diagonal mark is done by that player then that player won.

mark the sqaures with unique character for each player.
check every time when a player marks a square and update the square with player's indentification character.

i think u got my point ..... so immediate implement this ... if any problem is there plz post ur hasWon method ..

i think i ll able to correct ur bug if have any.
best of luck.

kind regards.
dmjpro.





Hi dmjpro,

Thanks for your great help. But I really don't know how to implement it your way. Could you please send me some code so I can try that.

Looking forward to your reply.

Best regards,

Koonda
May 13 '07 #4
koonda
34 New Member
There's a little very practical trick: the board measures 7x6 cells (if I'm not
mistaken). Make the real board 9x8 where the border cells contain sentinel
values (i.e. unequal to the three possible values). That way you never have to
explicitly check your indexes for crossing a border.

Another observation is: suppose there's no winner. If, after the current move
there does happen to be a winner, the position of the last move is part of the
winning configuration. All you have to do is track in horizontal, vertical and
diagonal directions starting at that last move's position and count for the
number of coins in that direction.

kind regards,

Jos
Hi Jos,

I think I did not understand your way of implementing it. If possible could you please send me some code examples to implement that.

Looking forward to your reply.

Thanks a lot

Best regards,

Koonda
May 13 '07 #5
JosAH
11,448 Recognized Expert MVP
Hi Jos,

I think I did not understand your way of implementing it. If possible could you please send me some code examples to implement that.

Looking forward to your reply.

Thanks a lot

Best regards,

Koonda
Nope, no code; code is for computers and similar silly machines. What I was
saying is that 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 13 '07 #6

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

Similar topics

1
1601
by: mandar | last post by:
Hi Friends, We are a consultancy and we are having the following openings with us ------------------------------------------------------------------------------------------------------ 1) Position Executive Presales --------------------------------------------
16
2911
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In: http://www.mywebsite.org/WebResource.axd?d=5WvLfhnJp5Lc8WhQSD4gdA2&t=632614619884218750 -------------------------------------------------------------------------------- System.Security.Cryptography.CryptographicException:...
0
3142
by: swaroopa | last post by:
Hello Partner Please forward some good resumes with excellent communication skills. 1.REMEDY AR CONSULTANT Exp: 6-10YEAR Location: San Jose CA Duration : 2 Months
1
1621
by: SpiderSwamy | last post by:
Hi, I know little bit about asp, I am facing a problem in Validating the ASP Form.. Example: Stud ID: 501242016 FirstName: Ajit LastName: Kar
0
1840
by: Martin | last post by:
Hello, We are in the process of putting together an A class C++ Development team(ecommerce / billing applications) for a project in the Bay Area. Reqt. details: ReqId : Req-3911 - C++ Senior Developer Primary Skills: C++, SQL Secondary Skills: XML, Oracle Description: As a part of the development team for our client's
0
1492
by: Rahtgaz | last post by:
Hello everyone, I'm wanting to start implementation of a pet project game IO mechanism. The game is console based. A cross between MUD and Rogue types. For I/O operations I'll be using ncurses (PDCurses to be accurate). Mainly taking advantage of colors in the console and wanting to minimize any work in porting it to Linux and Mac. ...
9
7820
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every chess position has around 30 moves, it would mean every node of the tree would have 30 branches (on average), which in turn themselves would average...
1
1782
by: latha | last post by:
Job Description: Current Location :St.Louis, MO Duration : 14 months The main objective of this role is to implement SAP projects in support of new Monsanto business lines and globalization efforts. Our team supports the following SAP modules (version 4.6C): General Ledger, Special Ledger, Asset Management, Cost Center Accounting, Product...
8
2630
by: AeonOfTime | last post by:
Hi everyone, I have a concept for a pretty simple web game, and am currently considering my options regarding the storage of the game state. Ideally, I would like to have a custom server running that keeps the most accessed data in memory, and which all clients connect to. Nothing new there, but I am wondering as to what protocol to use for...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7824
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8176
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8321
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7931
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.