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

Connect Four Java Player.java DO NOT MODIFY. This is the Interface that must be

ID: 3574124 • Letter: C

Question

Connect Four Java

Player.java DO NOT MODIFY. This is the Interface that must be implemented by all Players. We did not discuss interfaces in class, but think of it as a compile time check to ensure that all players conform to a certain “interface”. In this case, all Players MUST implement the methods contained here in. The methods that must be implement by all types of players are:

playToken() – returns a column in which to play a token. This must be a valid column (e.g column must exist, and column must not be full)

lastMove(int c) – receives the most recent move from the other player

getPlayerID() – returns the player ID of this player

reset() – cleans up all state and allows the player to begin a new game




HumanPlayer.java This is the representation of a human player. The constructor takes parameters as described in the file. For the HumanPlayer, the methods should do the following:

playToken() – Asks the user for input, returns the column received from the user. In cases of error, re-ask the user for input.

lastMove(int c) – entirely up to you how you want to use this information (hint: you should keep track of where the other player has gone and where you have gone so that you can do adequate error checking in playToken())

getPlayerID() – returns the player ID of this player

reset() – clean up your state and prepare for a new game

ConnectFour.java – FOR YOUR USE ONLY (not graded). Here , we’ve given you the main game loop algorithm. Just fill it in with calls to your Player/Board objects. Create first with two instances of human players, and play the game against yourself/a friend. Ensure things seem to be working correctly. Test, Test, Test.

AIPlayer.java This is the representation of an Artificial Intelligence Player (computer player). The AI player, at the very least must play valid moves throughout the entire game. Decide what strategy you want your computer player to use when deciding where to place a token. The constructor takes parameters as described in the file. Implement the methods as follows.

playToken() – Decides using whatever strategy you decide, the optimal column to place the token. You should probably make use of all information available to your player… There should never be an instance where you decide to place a token in an invalid column (whether full or out of bounds). You might find Math.random() useful here. Returns the column your AI has chosen.

lastMove(int c) – receives the last move made by the other player. You may want to store this somewhere as it might be useful to you in implementing (a). Hint: sometimes your AI will be the first player. i.e. sometimes, lastMove will be called AFTER playToken() and sometimes before.

getPlayerID() – returns the player ID of this player

reset() – clean up all the state you’ve accumulated this game. Set the AI player up to begin a new game.

If anyone can figure out how I can implement the HumanPlayer.java class and the AIPlayer.java class for this board

Board.java is as follows

public class Board {

   private char[][] field;

  

   private int line;

   private int column;

  

   private char Player1;

   private char Player2;

  

   private int count;

   private int checkTie;

  

   private String output;

  

  

public Board() {

   int linew = 6;

   int col = 7;

  

   field = new char[linew][col];

  

   for(int i = 0; i < linew;i++) {

       for(int j = 0;j < col;j++) {

           field[i][j]=' ';

       }

      

       String output = new String(field[i]);

       System.out.println(output);

   }

      

   checkTie = 0;

   line = field.length;

   column = field[0].length;

}

public Board(int linew, int col) {

   field = new char[linew][col];

   for(int i = 0; i < linew;i++) {

       for(int j = 0;j < col;j++) {

           field[i][j] = ' ';

       }

       String output=new String(field[i]);

       System.out.println(output);

   }

      

   checkTie = 0;

   line=field.length;

   column = field[0].length;

  

}

public int getNumRows() {

   return field.length;

   //return line;

}

public int getNumCols() {

   return field[0].length;

   //return column;

}

public char getPlayerOne() {

   return Player1;

}

public char getPlayerTwo() {

   return Player2;

}

public void setPlayerOne(char o)

{

   Player1 = o;

}

public void setPlayerTwo(char t)

{

   Player2 = t;

}

public char getToken(int row, int col)

{

   if(row >= line || col >= column || row < 0||col < 0)

   {

       return '';

   }

   return field[row][col];

}

public boolean canPlay() {

   for(int i = 0; i < line;i++) {

       for(int j = 0;j < column; j++) {

           if(field[i][j]!= ' '){

               checkTie++;

           }

       }

   }

   if (checkTie == field.length * field[0].length) {

       checkTie = 0;

       return false;

   } else {

       checkTie = 0;

       return true;

   }

}

public boolean play (int p, int c) {

   if(c >= column || c < 0|| p <1 || p > 2) {

       return false;

   } else {

       for (int i = line-1; i >= 0;i--){

           if(field[i][c] == ' '){

               if(p==1){

                   field[i][c]=Player1;

                   return true;

               }

               else if(p==2){

                   field[i][c]=Player2;

                   return true;

               }              

           }

       }

   }

   return false;      

}

public int isFinished(){

   count = 0;

  

   for (int j = 0;j < line;j++){

       for (int k = 0;k < column - 1;k++){

           if(field[j][k] != ' ' && field[j][k] == field[j][k+1]){

               count++;

           } else{

               count=0;

           }

           if (count==3) {

               if(field[j][k]==Player1) {

                   return 1;

               } else if(field[j][k]==Player2) {

                   return 2;

               }

           }

       }

   }

  

   count=0;

  

   for (int k = 0;k < column;k++){

       for (int j = 0;j < line - 1;j++){

           if(field[j][k] != ' ' && field[j][k] == field[j+1][k]){

           count++;

           } else {

               count=0;

           }

           if (count==3) {

               if(field[j][k]==Player1) {

                   return 1;

               } else if(field[j][k]==Player2) {

                   return 2;

               }

           }

       }

   }

  

   count=0;

  

   for( int i = 0; i <= line - 4; i++){

       count = 0;

       for(int j = i, k = 0; j

           if(field[j][k] == field[j+1][k+1]){

               count++;

           } else {

               count = 0;

           }

           if (count==3){

               if(field[j][k]==Player1)

               {

                   return 1;

               } else if(field[j][k]==Player2) {

                   return 2;

               }

           }

          

       }

   }

  

   count=0;

  

   for( int i = 0; i <= column - 4; i++) {

       count = 0;

       for(int j = line-1, k = i; j > 0 && k < column - 1; j--, k++){

           if(field[j][k] == field[j-1][k+1]){

               count++;

           } else {

               count = 0;

           }

           if (count == 3){

               if(field[j][k] == Player1)

               {

                   return 1;

               } else if(field[j][k] == Player2) {

                   return 2;

               }

           }

          

       }

   }

   if (canPlay() == false) {

       return 0;

   }

  

   return -1;

  

   }

  

}

Explanation / Answer

class definition missing in the question.

public class HumanPlayer implements Player

   
   }
     public int getPlayerID(){

int position;

        boolean legalMove = false;

        while(!legalMove)

        {

            position = controller.requestUserMove(mark);

           

            if(board.isOccupied(position))

            {

                controller.updateDisplay();

            }

            else

            {

                try

                {

                    board.populate(mark, position);

                    legalMove = true;

                }

                catch(Exception e)

                {

                    e.printStackTrace();

                }

            }

        }      
   }
     public void reset(){
      
   }

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