-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGameDisplay.java
More file actions
162 lines (145 loc) · 5.41 KB
/
Copy pathTextGameDisplay.java
File metadata and controls
162 lines (145 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* TextGameDisplay.java
*
* @author Deep Patel
*
* REMARKS: This class implements the game display and user input
* in order to play the game
*/
import java.util.Scanner;
class TextGameDisplay implements GameDisplay {
private Scanner scanner;
//Constructor
TextGameDisplay() {
this.scanner = new Scanner(System.in);
}
/**
* promptForOpponentDifficulty will be called at the beginning of each match. The method will be
* given the maximum difficulty of the AI (a positive integer greater than or equal to two). The
* method will return a value between 1 and maxDifficulty, indicating the human player’s choice of
* which game AI to play against.
*
* Parameters:
* int maxDifficulty
*
* Returns the difficulty selected
*/
public int promptForOpponentDifficulty(int maxDifficulty) {
System.out.println("Please enter the desired opponent difficulty, between 0 and " + maxDifficulty +
", where 0 is easiest opponent and " + maxDifficulty + " is hardest opponent.");
int difficulty = scanner.nextInt();
if (difficulty < 0 || difficulty > maxDifficulty) {
difficulty = promptForOpponentDifficulty(maxDifficulty);
}
return difficulty;
}
/**
* This method is used to ask the player for a move
*
* Returns the move player wants to make
*/
public Move promptForMove() {
Move move = null;
System.out.println("Please enter the row of the piece you would like to move. Enter 0 to forfeit game.");
int fromRow = scanner.nextInt();
if (fromRow == 0) {
return new Move(0, 0, 0, 0, true, true);
}
System.out.println("Please enter the column of the piece you would like to move.");
int fromCol = scanner.nextInt();
System.out.println("Please enter the row of the destination.");
int toRow = scanner.nextInt();
System.out.println("Please enter the column of the destination.");
int toCol = scanner.nextInt();
if (toCol>8||toCol<0||fromCol>8||fromCol<0||toRow>8||toRow<0||fromRow>8||fromRow<0){
move = promptForMove();
}else{
move = new Move(fromRow, fromCol, toRow, toCol, true, false);
}
return move;
}
//Displays the current state of the game board with all the pieces
//Is passed in the board of the game
public void displayBoard(Board board) {
System.out.println(" 1 2 3 4 5 6 7 8");
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(board.getSymbol(i, j) + "|");
}
System.out.println("\n------------------");
}
}
/**
* This method is used to summarize the move that currently took place
*
* Parameters:
* Board board - The method take in the current state of the board
* Move move - Move that is being summarized
*
*/
public void summarizeMove(Move move, Board board) {
if (move.getForfeit()) {
System.out.println("You forfeit. Game over!");
return;
}
if (move.getValid()) {
Piece p = move.getPiece();
String piece = getPieceString(p);
if (move.getPrevPiece() != null) {
String prevPiece = getPieceString(move.getPrevPiece());
System.out.println(piece + " moved from (" + move.getFromRow() + ", " + move.getFromCol() + ") to (" + move.getToRow()
+ ", " + move.getToCol() + ")." + prevPiece + " captured.");
} else {
System.out.println(piece + " moved from (" + move.getFromRow() + ", " + move.getFromCol() + ") to (" + move.getToRow()
+ ", " + move.getToCol() + "). No capture made.");
}
} else {
System.out.println("The move you have entered is invalid. Please enter another move.");
}
}
//Used to identify who the winner is
public void gameOver(int winner) {
if (winner == 0) {
System.out.println("You win! Congratulations!");
} else if (winner == 1) {
System.out.println("You lose! Better luck next time!");
} else {
System.out.println("You forfeit. Game Over!");
}
}
//Asks the user if they want to play again
//Returns boolean on whether they want to play
public boolean promptPlayAgain() {
boolean play;
System.out.println("Would you like to play again? Please enter 0 for yes or 1 for no.");
int choice = scanner.nextInt();
if (choice == 0) {
play = true;
} else if (choice == 1) {
play = false;
} else {
play = promptPlayAgain();
}
return play;
}
//Used to get the word for each piece in order to print it
private String getPieceString(Piece p) {
String piece = "";
if (p instanceof Pawn) {
piece = "Pawn";
} else if (p instanceof Rook) {
piece = "Rook";
} else if (p instanceof Knight) {
piece = "Knight";
} else if (p instanceof Bishop) {
piece = "Bishop";
} else if (p instanceof King) {
piece = "King";
} else if (p instanceof Queen) {
piece = "Queen";
}
return piece;
}
}