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:  (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 ‘ ‘.   Task1: Define your fields (aka instance variables) necessary to implement the following operations  Task2: 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 x 6 rows Board(int row, int col); //creates a board of size row x col  Both constructors above should initialize all positions to ‘ ‘  public int getNumRows(); //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 getToken(int row, int col); //returns the char representing token at location row,col, \'\\0\' if invalid indices  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

PROGRAM CODE:
Board.java

package connectFourBoard;

public class Board {

   private int COLUMN = 6;
private int ROW = 5;
char[][] board;
char Player1;
char Player2;
   Board() //creates a default board of size 7 columns x 6 rows
   {
       board = new char[COLUMN][ROW];
       for(int i=0; i<COLUMN; i++)
       {
           for(int j=0; j<ROW; j++)
           {
               board[i][j] = \' \';
           }
       }
   }
  
   Board(int row, int col) //creates a board of size row x col
   {
       COLUMN = col;
       ROW = row;
       board = new char[COLUMN][ROW];
       for(int i=0; i<COLUMN; i++)
       {
           for(int j=0; j<ROW; j++)
           {
               board[i][j] = \' \';
           }
       }
   }
   //Both constructors above should initialize all positions to ‘ ‘
  
   //returns the number of rows in board
   public int getNumRows()
   {
       return ROW;
   }
  
   //returns the number of cols in board
   public int getNumCols()
   {
       return COLUMN;
   }

   //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;
   }

   //returns the char representing token at location row,col, \'\\0\' if invalid indices
   public char getToken(int row, int col)
   {
       char token = \'\\0\';
       for(int i=0; i<COLUMN; i++)
       {
           for(int j=0; j<ROW; j++)
           {
               if(ROW == row && COLUMN == col)
               {
                   token = board[i][j];
                   break;
               }
           }
       }
       return token;
   }

   //returns true if a token is still able to placed onto the board, false otherwise
   public boolean canPlay()
   {
       boolean canPlay = false;
       for(int i=0; i<COLUMN; i++)
       {
           for(int j=0; j<ROW; j++)
           {
               if(board[i][j] == \' \')
               {
                   canPlay = true;
                   break;
               }
           }
       }
       return canPlay;
   }

   //places the appropriate token for player p in column c. returns true if successful, false otherwise.
  
   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<ROW; j++)
       {
           if(board[j][c-1] == \' \')
           {
               board[j][c-1] = token;
               isSuccessful = true;
               break;
           }
       }
       return isSuccessful;
   }

  
   //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
   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){
//creates boolean to act as flag
boolean flag = true;
//creates counter
int counter = 0;
while(flag){

//goes through board diagonally
for(int w = 0; w<COLUMN; w ++){
for(int h = 0; h < ROW; 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){
//creates boolean to act as flag
boolean flag = true;
//creates counter
int counter = 0;
while(flag){
//goes through board horizontally
for(int w = 0; w<COLUMN; w ++){
for(int h = 0; h < ROW; h++){

if(board[w][h] == playerToken){
counter += 1;
}else{
counter = 0;
}
if(counter >= 4){
flag = false;
}
}
}
break;
}
return !flag;
}
  
   public boolean Checkvertically(char playerToken){
//creates boolean to act as flag
boolean flag = true;
//creates counter
int counter = 0;
while(flag){
//goes through board vertically
for(int w = 0; w<ROW; w ++){
for(int h = 0; h < COLUMN; 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 !\");
   }

}

OUTPUT:

Both players win !

 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