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

My question is as followed. I am in the process of implementing a method for the

ID: 3574352 • Letter: M

Question

My question is as followed. I am in the process of implementing a method for the implementation of a connect 4 game on java. The method has the following instruction (please note that none of the method signatures should be modified) How would I write the method humanplayer method (please address the question directly and add notes the code for reference)?

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


For reference this is the board.java implementation (note the question is with regards to the humanplayer implementation not the board this is simply for reference) :

import java.util.Arrays;

public class Board {

public char[][] board;
public int col;
public int row;
public char playerOne;
public char playerTwo;

public final void spaceBoard() {
for (char[] x : this.board) {
Arrays.fill(x, ' ');
}
}
public Board() {
   this.col = 7;
   this.row = 6;
board = new char[this.row][this.col];
this.spaceBoard();
}

public Board(int row, int col) {
   this.col = col;
   this.row = row;
board = new char[row][col];
this.spaceBoard();
}

public int getNumRows() {
return this.row;
}

public int getNumCols() {
return this.col;
}

public char getPlayerOne() {
return this.playerOne;
}

public char getPlayerTwo() {
return this.playerTwo;
}

public void setPlayerOne(char o) {
this.playerOne = o;
}

public void setPlayerTwo(char t) {
this.playerTwo = t;
}

public char getToken(int row, int col) {
try {
return this.board[row][col];
} catch (ArrayIndexOutOfBoundsException ex) {
return '';
}
}

public boolean canPlay() {
for (int i = 0; i < this.row; i++) {
for (int j = 0; j < this.col; j++) {
if (' ' == this.board[0][0]) {
return true;
}
}
}
return false;
}

public boolean play(int p, int c) {
for (int i = this.row - 1; i >= 0; i--) {

if (this.board[i][c - 1] == ' ') {
if (p == 1) {

this.board[i][c - 1] = this.playerOne;
return true;
} else if (p == 2) {
this.board[i][c - 1] = this.playerTwo;
return true;
}
}
}
return false;
}

public int isFinished() {
int wonGame = this.horizontalChck();
if (wonGame != -1) {
return wonGame;
}
wonGame = this.verticalChck();
if (wonGame != -1) {
return wonGame;
}
wonGame = this.diagonalChck();
if (wonGame != -1) {
return wonGame;
}
for (int i = 0; i < this.row; ++i) {
for (int j = 0; j < this.col; ++j) {
if (this.board[i][j] == ' ') {
return 0;
}
}
}
return wonGame;
}

public int horizontalChck() {
for (int row = 0; row < this.row; ++row) {
int count = 0;
for (int column = 1; column < this.col; ++column) {

if (this.board[row][column] != ' ' && this.board[row][column] == this.board[row][column - 1]) {
++count;
} else {
count = 1;
}
if (count >= 4) {
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}
}
}
return -1;
}

public int verticalChck() {

for (int column = 0; column < this.col; column++) {
int count = 0;
for (int row = 1; row < this.row; row++) {
if (this.board[row][column] != ' ' && this.board[row][column] == this.board[row - 1][column]) {
count++;
} else {
count = 1;
}
if (count >= 4) {
  
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}
}
}
return -1;
}

public int diagonalChck() {
  
   for (int row = 0; row < this.row; ++row) {
int count = 0;
for (int column = 1; column < this.col; ++column) {

if (column + row >= this.row) {
break;
}
if (this.board[row + column][column] != ' ' && this.board[row + column - 1][column - 1] == this.board[row + column][column]) {
++count;
} else {
count = 1;
}
if (count >= 4) {
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}

}
}

for (int column = 0; column < this.col; column++) {
int count = 0;

for (int row = 1; row < this.row; row++) {

System.out.println("i:" + row + "j:" + column + " " + (row + column));
if (column + row >= this.col) {
break;
}
if (this.board[row][column + row] != ' ' && this.board[row - 1][column + row - 1] == this.board[row][column + row]) {
++count;
} else {
count = 1;
}
if (count >= 4) {
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}
}
}

for (int column = 0; column < this.col; ++column) {
int count = 0;
for (int row = 1; row < this.row; ++row) {
if (column - row < 0) {
break;
}
if (this.board[row][column - row] != ' ' && this.board[row - 1][column - row + 1] == this.board[row][column - row]) {
++count;
} else {
count = 1;
}
if (count >= 4) {
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}

}
}

for (int row = 0; row < this.row; ++row) {
int count = 0;

for (int column = this.col - 1; column >= 0; column--) {

if (row - column < 0) {
break;
}

if (this.board[column - row][column] != ' ' && this.board[column - row - 1][column + 1] == this.board[column - row][column]) {
++count;
} else {
count = 1;
}
if (count >= 4) {
if (this.board[row][column] == this.playerOne) {
return 1;
} else if (this.board[row][column] == this.playerTwo) {
return 2;
}
}

}
}
return -1;
}


public static void main(String[] args) {
Board b = new Board();
b.setPlayerOne('0');
b.setPlayerTwo('x');

for (int i = 0; i < b.col; i++) {
System.out.print(" __ ");
}
System.out.println("");
for (char[] x : b.board) {
String s = "|_";

for (char y : x)

s += y + "_|_";
System.out.println(s.substring(0, s.length() - 1));

}
}
}

Explanation / Answer

As the file which you mentioned in HumanPlayer.java description does not exist, so we will use 3 variables in HumanPlayer.java One for the Id, another for opponent moves and another for our own moves.

public class HumanPlayer implements Player {

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote