Needs to be in Java program The project assignment is to create an interactive t
ID: 3578078 • Letter: N
Question
Needs to be in Java program
The project assignment is to create an interactive two-player Reversi game.
Reversi rules may be obtained from the instructor or the Reversi wikipedia page.
The players will be represented as Java objects implementing combinations of human and computer players.
All interactions may be done via the console
An interactive human-computer game where the human moves first
The updated 8x8 board should be displayed after each player's move - a simple text based display is sufficient
A prompt should be provided for the human's move
An error message should be displayed if the human enters an illegal move and reprompt for another try.
The computer's choice of move can be any algorithm that results in a legal move, a random choice is sufficient.
Add a prompt to let the user choose whether the human or computer goes first, and implement the choice
Add code that displays the score after each move (i.e. a current count of each players tokens)
Add code to determine if a player must pass (i.e. has no legal move). Display a message to that effect and proceed with the game.
Add code to determine if the game is over (i.e. no legal moves remain), shows the final score and declares the winner
Add a hint option when it is the human's turn, that redisplays the board showing all legal moves.
Add a menu to let the user choose among the following:
A two human game - the program should prompt for player names and prompt each player for a move.
A game between human and computer player algorithm1 - runs the basic game described above
40 points
Implement a computer - computer game, both computer players may use the same algorithm
Show the final board and declare a winner
Run this game when the third menu item above is selected
Add code to the computercomputer game to prompt for the following options:
Number of games to play - display a tally at end of how many each player wins
Whether to display the board after each turn, and if so, prompt for a pause value between each turn.
Implement a second computer player algorithm that plays a corner square if available.
Use this player to implement the last three menu choices.
Improve the second computer player with at least one additional criterion for choosing a move.
If you have already implemented a non-random player for the basic game, you can fulfill the two requirements above with a random algorithm, or any other move-choosing algorithm that you want to try.
Explanation / Answer
Reversi.java
public class Reversi{
public static void twoPlayers(Board b){
Scanner scan = new Scanner(System.in);
Board.Point move = b.new Point(-1, -1);
System.out.println("Black Moves first");
int result;
Boolean skip;
String input;
while(true){
skip = false;
HashSet<Board.Point> blackPlaceableLocations = b.getPlaceableLocations('B', 'W');
HashSet<Board.Point> whitePlaceableLocations = b.getPlaceableLocations('W', 'B');
b.showPlaceableLocations(blackPlaceableLocations, 'B', 'W');
result = b.gameResult(whitePlaceableLocations, blackPlaceableLocations);
if(result == 0){System.out.println("It is a draw.");break;}
else if(result==1){System.out.println("White wins: "+b.WScore+":"+b.BScore);break;}
else if(result==-1){System.out.println("Black wins: "+b.BScore+":"+b.WScore);break;}
if(blackPlaceableLocations.isEmpty()){
System.out.println("Black needs to skip... Passing to white");
skip = true;
}
if(!skip){
System.out.println("Place move (Black): ");
input = scan.next();
move.y = b.coordinateX(input.charAt(0));
move.x = (Integer.parseInt(input.charAt(1)+"")-1);
while(!blackPlaceableLocations.contains(move)){
System.out.println("Invalid move! Place move (Black): ");
input = scan.next();
move.y = b.coordinateX(input.charAt(0));
move.x = Integer.parseInt((input.charAt(1)+""))-1;
}
b.placeMove(move, 'B', 'W');
b.updateScores();
System.out.println(" Black: "+b.BScore+" White: "+b.WScore);
}
skip = false;
whitePlaceableLocations = b.getPlaceableLocations('W', 'B');
blackPlaceableLocations = b.getPlaceableLocations('B', 'W');
b.showPlaceableLocations(whitePlaceableLocations, 'W', 'B');
result = b.gameResult(whitePlaceableLocations, blackPlaceableLocations);
if(result==0){System.out.println("It is a draw.");break;}
else if(result==1){System.out.println("White wins: "+b.WScore+":"+b.BScore);break;}
else if(result==-1){System.out.println("Black wins: "+b.BScore+":"+b.WScore);break;}
if(whitePlaceableLocations.isEmpty()){
System.out.println("White needs to skip... Passing to Black");
skip = true;
}
if(!skip){
System.out.println("Place move (White): ");
input = scan.next();
move.y = b.coordinateX(input.charAt(0));
move.x = (Integer.parseInt(input.charAt(1)+"")-1);
while(!whitePlaceableLocations.contains(move)){
System.out.println("Invalid move! Place move (White): ");
input = scan.next();
move.y = b.coordinateX(input.charAt(0));
move.x = (Integer.parseInt(input.charAt(1)+"")-1);
}
b.placeMove(move, 'W', 'B');
b.updateScores();
System.out.println(" White: "+b.WScore+" Black: "+b.BScore);
}
}
}
public static void main(String[] args){
Board b = new Board();
twoPlayers(b);
}
}
Board.java
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Board{
public class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString(){
return "["+x+", "+y+"]";
}
@Override
public boolean equals(Object o){
return o.hashCode()==this.hashCode();
}
@Override
public int hashCode() {
return Integer.parseInt(x+""+y);
}
}
private final char[][] board;
int WScore, BScore, remaining;
private final char boardX[] = new char[]{'A','B','C','D','E','F','G','H'};
public Board(){
board = new char[][]{
{'_','_','_','_','_','_','_','_',},
{'_','_','_','_','_','_','_','_',},
{'_','_','_','_','_','_','_','_',},
{'_','_','_','W','B','_','_','_',},
{'_','_','_','B','W','_','_','_',},
{'_','_','_','_','_','_','_','_',},
{'_','_','_','_','_','_','_','_',},
{'_','_','_','_','_','_','_','_',},
};
}
private void findPlaceableLocations(char player, char opponent, HashSet<Point> placeablePositions){
for(int i=0;i<8;++i){
for(int j=0;j<8;++j){
if(board[i][j] == opponent){
int I = i, J = j;
if(i-1>=0 && j-1>=0 && board[i-1][j-1] == '_'){
i = i+1; j = j+1;
while(i<7 && j<7 && board[i][j] == opponent){i++;j++;}
if(i<=7 && j<=7 && board[i][j] == player) placeablePositions.add(new Point(I-1, J-1));
}
i=I;j=J;
if(i-1>=0 && board[i-1][j] == '_'){
i = i+1;
while(i<7 && board[i][j] == opponent) i++;
if(i<=7 && board[i][j] == player) placeablePositions.add(new Point(I-1, J));
}
i=I;
if(i-1>=0 && j+1<=7 && board[i-1][j+1] == '_'){
i = i+1; j = j-1;
while(i<7 && j>0 && board[i][j] == opponent){i++;j--;}
if(i<=7 && j>=0 && board[i][j] == player) placeablePositions.add(new Point(I-1, J+1));
}
i=I;j=J;
if(j-1>=0 && board[i][j-1] == '_'){
j = j+1;
while(j<7 && board[i][j] == opponent)j++;
if(j<=7 && board[i][j] == player) placeablePositions.add(new Point(I, J-1));
}
j=J;
if(j+1<=7 && board[i][j+1] == '_'){
j=j-1;
while(j>0 && board[i][j] == opponent)j--;
if(j>=0 && board[i][j] == player) placeablePositions.add(new Point(I, J+1));
}
j=J;
if(i+1<=7 && j-1>=0 && board[i+1][j-1] == '_'){
i=i-1;j=j+1;
while(i>0 && j<7 && board[i][j] == opponent){i--;j++;}
if(i>=0 && j<=7 && board[i][j] == player) placeablePositions.add(new Point(I+1, J-1));
}
i=I;j=J;
if(i+1 <= 7 && board[i+1][j] == '_'){
i=i-1;
while(i>0 && board[i][j] == opponent) i--;
if(i>=0 && board[i][j] == player) placeablePositions.add(new Point(I+1, J));
}
i=I;
if(i+1 <= 7 && j+1 <=7 && board[i+1][j+1] == '_'){
i=i-1;j=j-1;
while(i>0 && j>0 && board[i][j] == opponent){i--;j--;}
if(i>=0 && j>=0 && board[i][j] == player)placeablePositions.add(new Point(I+1, J+1));
}
i=I;j=J;
}
}
}
}
public void displayBoard(Board b){
System.out.print(" ");
for(int i=0;i<8;++i)System.out.print(boardX[i]+" ");
System.out.println();
for(int i=0;i<8;++i){
System.out.print((i+1)+" ");
for(int j=0;j<8;++j)
System.out.print(b.board[i][j]+" ");
System.out.println();
}
System.out.println();
}
public int gameResult(Set<Point> whitePlaceableLocations, Set<Point> blackPlaceableLocations){
updateScores();
if(remaining == 0){
if(WScore > BScore) return 1;
else if(BScore > WScore) return -1;
else return 0; //Draw
}
if(WScore==0 || BScore == 0){
if(WScore > 0) return 1;
else if(BScore > 0) return -1;
}
if(whitePlaceableLocations.isEmpty() && blackPlaceableLocations.isEmpty()){
if(WScore > BScore) return 1;
else if(BScore > WScore) return -1;
else return 0; //Draw
}
return -2;
}
public HashSet<Point> getPlaceableLocations(char player, char opponent){
HashSet<Point> placeablePositions = new HashSet<>();
findPlaceableLocations(player, opponent, placeablePositions);
return placeablePositions;
}
public void showPlaceableLocations(HashSet<Point> locations, char player, char opponent){
for(Point p:locations)
board[p.x][p.y]='*';
displayBoard(this);
for(Point p:locations)
board[p.x][p.y]='_';
}
//Although we know that if W is player, O will be the opponent but still...
public void placeMove(Point p, char player, char opponent){
int i = p.x, j = p.y;
board[i][j] = player;
int I = i, J = j;
if(i-1>=0 && j-1>=0 && board[i-1][j-1] == opponent){
i = i-1; j = j-1;
while(i>0 && j>0 && board[i][j] == opponent){i--;j--;}
if(i>=0 && j>=0 && board[i][j] == player) {while(i!=I-1 && j!=J-1)board[++i][++j]=player;}
}
i=I;j=J;
if(i-1>=0 && board[i-1][j] == opponent){
i = i-1;
while(i>0 && board[i][j] == opponent) i--;
if(i>=0 && board[i][j] == player) {while(i!=I-1)board[++i][j]=player;}
}
i=I;
if(i-1>=0 && j+1<=7 && board[i-1][j+1] == opponent){
i = i-1; j = j+1;
while(i>0 && j<7 && board[i][j] == opponent){i--;j++;}
if(i>=0 && j<=7 && board[i][j] == player) {while(i!=I-1 && j!=J+1)board[++i][--j] = player;}
}
i=I;j=J;
if(j-1>=0 && board[i][j-1] == opponent){
j = j-1;
while(j>0 && board[i][j] == opponent)j--;
if(j>=0 && board[i][j] == player) {while(j!=J-1)board[i][++j] = player;}
}
j=J;
if(j+1<=7 && board[i][j+1] == opponent){
j=j+1;
while(j<7 && board[i][j] == opponent)j++;
if(j<=7 && board[i][j] == player) {while(j!=J+1)board[i][--j] = player;}
}
j=J;
if(i+1<=7 && j-1>=0 && board[i+1][j-1] == opponent){
i=i+1;j=j-1;
while(i<7 && j>0 && board[i][j] == opponent){i++;j--;}
if(i<=7 && j>=0 && board[i][j] == player) {while(i!=I+1 && j!=J-1)board[--i][++j] = player;}
}
i=I;j=J;
if(i+1 <= 7 && board[i+1][j] == opponent){
i=i+1;
while(i<7 && board[i][j] == opponent) i++;
if(i<=7 && board[i][j] == player) {while(i!=I+1)board[--i][j] = player;}
}
i=I;
if(i+1 <= 7 && j+1 <=7 && board[i+1][j+1] == opponent){
i=i+1;j=j+1;
while(i<7 && j<7 && board[i][j] == opponent){i++;j++;}
if(i<=7 && j<=7 && board[i][j] == player)while(i!=I+1 && j!=J+1)board[--i][--j] = player;}
}
public void updateScores(){
WScore = 0; BScore = 0; remaining = 0;
for(int i=0;i<8;++i){
for(int j=0;j<8;++j){
if(board[i][j]=='W')WScore++;
else if(board[i][j]=='B')BScore++;
else remaining++;
}
}
}
public int coordinateX(char x){
for(int i=0;i<8;++i)if(boardX[i]==Character.toLowerCase(x)||boardX[i]==Character.toUpperCase(x))return i;
return -1; // Illegal move received
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.