Milestone 3 HumanPlayer 2 Non penalized submissions allowed (1 pt deduction for
ID: 3574538 • Letter: M
Question
Milestone 3 HumanPlayer
2 Non penalized submissions allowed (1 pt deduction for every submission thereafter)
We have provided you a set of Java files to help you get started. As usual you should not change any class or method signatures we have given you. You may add methods to either of your implementations. Below is a description of the files and what your tasks are for MS3. While this MS is not totally dependent on a working Board.java (e.g. you can get a full score without a functional Board.java), it is much more difficult without one. You should ensure that your Board.java passes most of the testcases before you begin (we will open up submissions to test your Board.java after the deadline).
Before you begin work, please read all of the instructions.
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.
Scoring:
HumanPlayer.java : Asks for user input, does error checking, always plays valid token.
The files that they have given us are here : https://www.dropbox.com/s/mfmsl5u17gia417/C4Stubs.zip?dl=0
Explanation / Answer
ConnectionFour.java
public class ConnectFour{
public static void main(String[] args){
//Create new board object
Board board = new Board();
//Set player tokens for player 1 and player 2
//Create Player objects
//Note, the code below works because of the interface Player. All classes that "implement" Player can be
// typed as Player. Makes switching from Human to AI Players really easy. For a challenge, you might
// consider:
// 1. asking the user whether he/she wants to play against a human or a computer
// 2. implementing multiple AI players (easy, med, hard) that the user can choose to play against
Player p1 = new HumanPlayer(1,6,7);
//Player p2 = new HumanPlayer(1,6,7); //comment this line when testing AIPlayer
Player p2 = new AIPlayer(2,6,7); //uncomment this line when testing AIPlayer
board.setPlayerTokens(p1);
board.setPlayerTokens(p2);
//Print your empty board
board.print();
int player = 1;
while(board.playable()){
if(player == 1){
int col = p1.playToken();
board.play(col,pl);
board.print();
if(board.checkWin()){
break;
}
player == 2;
}
else if(player == 2){
int col = p2.playToken();
board.play(col,p2);
board.print();
if(board.checkWin()){
break;
}
player == 1;
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
HumanPlayer.java
public class HumanPlayer implements Player{
//do not change the line above
//Define your fields here
public int playerID;
public int row;
public int col;
//constructor takes the player id for this player, and the number of
// rows and columns of the board we're playing on
public HumanPlayer(int playerID, int row, int col){
this.playerID = playerID;
this.row = row;
this.col = col;
}
//used to notify your AI player of the other players last move
public void lastMove(int c) {
System.out.println("Last move: "+c);
}
//returns column of where to play a token
public int playToken(){
reurn col;
}
//get this player's id
public int getPlayerID(){
return playerID;
}
//resets the state of the player in preparation for a new game
public void reset(){
this.row = 0;
this.col = 0;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Player.java
public interface Player{
//DO NOT CHANGE THIS FILE
public int playToken();
public int getPlayerID();
public void lastMove(int c);
public void reset();
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
AIPlayer.java
public class AIPlayer implements Player{
//do not change the line above
//Define your fields here
public int playerID;
public int row;
public int col;
//constructor takes the player id for this player, and the number of
// rows and columns of the board we're playing on
public AIPlayer(int playerID, int row, int col){
this.playerID = playerID;
this.row = row;
this.col = col;
}
//used to notify your AI player of the other players last move
public void lastMove(int c) {
System.out.println("Last move: "+c);
}
//returns column of where to play a token
public int playToken(){
return col;
}
//get this player's id
public int getPlayerID(){
return playerID;
}
//resets the state of the player in preparation for a new game
public void reset(){
this.row = 0;
this.col = 0;
}
}