Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Intro class. Need to code a connect four using the following methods. Implement

ID: 3778185 • Letter: I

Question

Intro class. Need to code a connect four using the following methods.

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 ‘ ‘.


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)

Explanation / Answer

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, '' if invalid indices
   public char getToken(int row, int col)
   {
       char token = '';
       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, 3);
       board.play(1, 1);
       board.play(2, 3);
       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:

Player 1 wins !

Here is a driver program I'm attaching with the results.

BoardDriver.java

package sample;

public class BoardDriver {

   /**

   * @param args

   */

       public static void isFinished(int value)

       {

           if(value == 0)

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

       else if(value == -1)

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

       else

       System.out.println("Player " + value + " wins !");

       }

  

   public static void main(String[] args) {

   //default board

       System.out.println("Default board");

       Board board = new Board();

   isFinished(board.isFinished());

   System.out.println(board.canPlay());

   board.setPlayerOne('O');

   board.setPlayerTwo('X');

   isFinished(board.isFinished());

   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, 3);

   board.play(3, 2);

   System.out.println(board.canPlay());

   isFinished(board.isFinished());

  

   //Custom board

   System.out.println("Custom board");

   Board customBoard = new Board(8,8);

   isFinished(customBoard.isFinished());

   System.out.println(customBoard.canPlay());

   customBoard.setPlayerOne('O');

   customBoard.setPlayerTwo('X');

  

   }

   }

OUTPUT:

Default board

Game is not yet finished !

true

Game is not yet finished !

true

Both players win !

Custom board

Game is not yet finished !

true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote