This is the implementation of a method for a Connect 4 board in java. The origin
ID: 3573010 • Letter: T
Question
This is the implementation of a method for a Connect 4 board in java.
The original implementation of the board can be found here is as follows.
My question is how would you go about implementing the humanplayer.java based on the instructions below? (please annotate code with comments)
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.
Explanation / Answer
HumanPlayer.java
package board.players;
import board.baseGame.Board;
import board.baseGame.Controller;
import board.baseGame.Player;
public class HumanPlayer extends Player //public class HumanPlayer declared
{
Controller contllr;
public HumanPlayer(Board board, char mark, Controller contllr)
{
super(board, mark);
this.controller = contllr;
}
public void makeMove() // makemove() method
{
int position;
boolean legalMove = false;
while(!legalMove)
{
position = contllr.requestUserMove(mark);
if(board.isOccupied(position))
{
contllr.updateDisplay();
}
else
{
try
{
board.populate(mark, position);
legalMove = true;
}
catch(Exception e) //exception declared here
{
e.printStackTrace();
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.