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 InputStreamReader(System.in));
int player1;
System.out.println ("What is your Number, player 1?");
player1 = scan.nextInt();
System.out.println ("Hey number"+player1+" are you prepared to CONNECT FOUR");
System.out.println();
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;cup2=5;cup3=5;cup4=5;cup5=5;cup6=5;
//int num1, num2;
Scanner scan = new Scanner(System.in);
// BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
int player2;
System.out.println ("What is your Number, player 2?");
player2 = scan.nextInt();
System.out.println ("Hey "+player2+" are you prepared to CONNECT FOUR");
System.out.println();
//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 [][]ConnectFourArray;
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;cup2=5;cup3=5;cup4=5;cup5=5;cup6=5;
int[][] ConnectFourArray = new int[6][7];
int num1, num2;
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 ");
column1 = selecter.readLine();
num1= Integer.parseInt(column1);
System.out.println();
if (num1==0){
ConnectFourArray[cup0][0]=1;
cup0=cup0-1;
}
else if (num1==1){
ConnectFourArray[cup1][1]=1;
cup1=cup1-1;
}
else if (num1==2){
ConnectFourArray[cup2][2]=1;
cup2=cup2-1;
}
else if (num1==3){
ConnectFourArray[cup3][3]=1;
cup3=cup3-1;
}
else if (num1==4){
ConnectFourArray[cup4][4]=1;
cup4=cup4-1;
}
else if (num1==5){
ConnectFourArray[cup5][5]=1;
cup5=cup5-1;
}
else if (num1==6){
ConnectFourArray[cup6][6]=1;
cup6=cup6-1;
}
System.out.println();
BufferedReader selecter2 = new BufferedReader (new InputStreamReader(System.in));
String column2;
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 ");
column1 = selecter.readLine();
num1= Integer.parseInt(column1);
System.out.println();
if (num1==0){
ConnectFourArray[cup0][0]=2;
cup0=cup0-1;
}
else if (num1==1){
ConnectFourArray[cup1][1]=2;
cup1=cup1-1;
}
else if (num1==2){
ConnectFourArray[cup2][2]=2;
cup2=cup2-1;
}
else if (num1==3){
ConnectFourArray[cup3][3]=2;
cup3=cup3-1;
}
else if (num1==4){
ConnectFourArray[cup4][4]=2;
cup4=cup4-1;
}
else if (num1==5){
ConnectFourArray[cup5][5]=2;
cup5=cup5-1;
}
else if (num1==6){
ConnectFourArray[cup6][6]=2;
cup6=cup6-1;
}
System.out.println();
}
System.out.println();
}
catch (Exception E){
System.out.println("Error with input");
}
System.out.println("Would you like to play again");
try{
{
String value;
BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
System.out.println("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.println("Start again");
StartGame(); // calling the StartGame method to play a game once more
}
else if (value.equals("no"))
{
System.out.println("No more games to play");
// System.exit(0);
}
else
{
System.exit(0);
}
}
System.out.println();
}
catch (Exception e){
System.out.println("Error with input");
}
finally
{
System.out.println(" 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 (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 = true;//int winner;
if(status == player1)
{
System.out.println("Player 1 is the winner");
}
else if(status == player2)
{
System.out.println("Player 2 is the winner" );
}
}//end inner for loop
}// end outer for loop
} // end method Winner
return status;
}
}//end class
ClassConnectFour which designs the board........................
import java.util.*;
import java.io.*;
import javax.swing.*;
public class ClassConnectFour
{
//Player player1;
//Player player2;
public ClassConnectFour()
{
//this.player1 = player1;
}
public void DrawBoard()
{
int[][] ConnectFourArray = new int[6][7] ;
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();
}
}//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)
{
ClassConnectFour cf = new ClassConnectFour();
cf.DrawBoard();
Player1 player1 = new Player1();
Player2 player2 = new Player2();
player1.move();
player2.move();
System.out.println("Number 1 belongs to player " + player1.name());
System.out.println("Number 2 belongs to player " + player2.name());
PlayGame pg = new PlayGame();
pg.StartGame();
pg.hasWon();
//pg.Play();
//System.out.println(player.name());
//System.out.println(player2.name());
}// 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
//