Implement a representation of the connect four board in a cl

Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if you wish and include any data structures/variables you feel would be necessary. However, you MUST implement ALL of the methods described in Task2 for full credit. (0, 0) represents the upper left corner of the board, (i.e. if a player were to place a token in column 0, the first token to be placed would fall to (r-1, 0) where r is the number of rows in your board. All positions in the board should be initialized to an empty space \' \'. Define your fields (aka instance variables) necessary to implement the following operations implement the following methods: Your Board.java MUST have the following methods (DO NOT MODIFY ANY OF THE METHOD SIGNATURES) Constructors: Board();//creates a default board of size 7 columns times 6 rows Board (int row, int col);//creates a board of size row x col Both constructors above should initialize all positions to \'\' public intgetNumRows();//returns the number of rows in board public int getNumCols();//returns the number of cols in board public char getPlayerOne();//returns char representing player 1 public char getPlayerTwo();//returns char representing player 2 public void setPlayerOne(char o);//sets char representing player 1 public void setPlayerTwo(char t);//sets char representing player 2 public char getTokenfint row, int col);//returns the char representing token at location row, col, returns \'\\0\'if indices are invalid public boolean canPlay();//returns true if a token is still able to placed onto the board, false otherwise public boolean play(int p, int c);//places the appropriate token for player p in column c. returns true if successful, false otherwise. public int isFinished();//returns either the ID of the player who has won (1 or 2) OR 0 if the game has ended in a tie OR -1 if nobody has won yet

Solution

Following is the code

Board.java

package connectFourBoard;

public class Board {

private int COLS = 6;

private int ROW1 = 5;

char[][] board;

char Player1;

char Player2;

   Board() //creates a default board of size 7 COLSs x 6 ROW1s

   {

       board = new char[COLS][ROW1];

       for(int i=0; i<COLS; i++)

       {

           for(int j=0; j<ROW1; j++)

           {

               board[i][j] = \' \';

           }

       }

   }

   Board(int ROW1, int col) //creates a board of size ROW1 x col

   {

       COLS = col;

       ROW1 = ROW1;

       board = new char[COLS][ROW1];

       for(int i=0; i<COLS; i++)

       {

           for(int j=0; j<ROW1; j++)

           {

               board[i][j] = \' \';

           }

       }

   }

   //Both constructors above should initialize all positions to ‘ ‘

   //returns the number of ROW1s in board

   public int getNumROW1s()

   {

       return ROW1;

   }

   //returns the number of cols in board

   public int getNumCols()

   {

       return COLS;

   }

   //returns char representing player 1

   public char getPlayerOne()

   {

       return Player1;

   }

   //returns char representing player 2

   public char getPlayerTwo()

   {

       return Player2;

   }

   //sets char representing player 1

   public void setPlayerOne(char o)

   {

       Player1 = o;

   }

   //sets char representing player 2

   public void setPlayerTwo(char t)

   {

       Player2 = t;

   }

    public char getToken(int ROW1, int col)

   {

       char token = \'\\0\';

       for(int i=0; i<COLS; i++)

       {

           for(int j=0; j<ROW1; j++)

           {

               if(ROW1 == ROW1 && COLS == col)

               {

                   token = board[i][j];

                   break;

               }

           }

       }

       return token;

   }

  

   public boolean canPlay()

   {

       boolean canPlay = false;

       for(int i=0; i<COLS; i++)

       {

           for(int j=0; j<ROW1; j++)

           {

               if(board[i][j] == \' \')

               {

                   canPlay = true;

                   break;

               }

           }

       }

       return canPlay;

   }

   public boolean play(int p, int c)

   {

       boolean isSuccessful = false;

       char token;

       if(p == 1)

           token = getPlayerOne();

       else

           token = getPlayerTwo();

       for(int j=0; j<ROW1; j++)

       {

           if(board[j][c-1] == \' \')

           {

               board[j][c-1] = token;

               isSuccessful = true;

               break;

           }

       }

       return isSuccessful;

   }

   public int isFinished()

   {

       int Finished = -1;

       if((CheckDiagonally(getPlayerOne()) || CheckHorizontally(getPlayerOne()) || Checkvertically(getPlayerOne()))

               &&(CheckDiagonally(getPlayerTwo()) || CheckHorizontally(getPlayerTwo()) || Checkvertically(getPlayerTwo())))

           return 0;

       else if(CheckDiagonally(getPlayerOne()) || CheckHorizontally(getPlayerOne()) || Checkvertically(getPlayerOne()))

           return 1;

       else if(CheckDiagonally(getPlayerTwo()) || CheckHorizontally(getPlayerTwo()) || Checkvertically(getPlayerTwo()))

           return 2;

     

       return Finished;

   }

   public boolean CheckDiagonally(char playerToken){

boolean flag = true;

int counter = 0;

while(flag){

for(int w = 0; w<COLS; w ++){

for(int h = 0; h < ROW1; h++){

   if(w==h)

   {

   if(board[w][h] == playerToken){

   counter += 1;

   }else{

   counter = 0;

   }

   if(counter >= 4){

   flag = false;

   }

   }

}

}

break;

}

return !flag;

}

   public boolean CheckHorizontally(char playerToken){

boolean flag = true;

int counter = 0;

while(flag){

for(int w = 0; w<COLS; w ++){

for(int h = 0; h < ROW1; h++){

if(board[w][h] == playerToken){

counter += 1;

}else{

counter = 0;

}

if(counter >= 4){

flag = false;

}

}

}

break;

}

return !flag;

}

   public boolean Checkvertically(char playerToken){

boolean flag = true;

int counter = 0;

while(flag){

for(int w = 0; w<ROW1; w ++){

for(int h = 0; h < COLS; h++){

if(board[h][w] == playerToken){

counter += 1;

}else{

counter = 0;

}

if(counter >= 4){

flag = false;

}

}

}

break;

}

return !flag;

}

}

BooardDriver.java

package connectFourBoard;

public class BooardDriver {

   /**

   * @param args

   */

   public static void main(String[] args) {

       Board board = new Board();

       board.setPlayerOne(\'O\');

       board.setPlayerTwo(\'X\');

       board.play(1, 1);

       board.play(2, 2);

       board.play(1, 1);

       board.play(2, 2);

       board.play(1, 1);

       board.play(2, 2);

       board.play(1, 1);

       board.play(2, 2);

       if(board.isFinished() == 0)

           System.out.println(\"Both players win !\");

       else if(board.isFinished() == -1)

           System.out.println(\"Game is not yet finished !\");

       else

           System.out.println(\"Player \" + board.isFinished() + \"wins !\");

   }

}

 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if
 Implement a representation of the connect four board in a class called Board Board representation details: You are free to implement any additional methods if

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site