-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessPlayer.java
More file actions
174 lines (152 loc) · 6.6 KB
/
ChessPlayer.java
File metadata and controls
174 lines (152 loc) · 6.6 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
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.Scanner;
/**
*
* @author wcaine
*/
public class ChessPlayer {
private static final Scanner stdin = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
newGame();
// FIXME: implement chess board visualization + move numbering *
// FIXME: implement FEN changes ***
// FIXME: implement piece movements **
// FIXME: implement attackedsquares hashsets
// FIXME: implement check and checkmate
// FIXME: implement basic e4d4 notation
// FIXME: implement timed games
}
private static void newGame() {
System.out.print("Enter starting position using FEN notation or click enter for standard position: ");
String inputFEN = stdin.nextLine();
// create chessboard based on FEN or standard game if none entered
ChessBoard board;
if (inputFEN.isBlank()) {
board = new ChessBoard();
} else {
board = new ChessBoard(inputFEN.trim());
}
// reprompt user for FEN if ChessBoard construction fails
if (!board.isGameState()) {
System.out.println();
newGame();
};
// inititate game upon user acceptance of rules
System.out.println();
System.out.println("""
Know all moves must be expressed in expanded algebraic notation.
If unsure about how to express a move, input "help" at any time.
In case of exit, enter "stop" at any time to pause the game and
receive the FEN to pick up the game at another time.""");
System.out.println("Enter \"Y\" to begin game.");
System.out.print(" ");
String inputData = stdin.nextLine();
inputData = inputData.trim();
switch (inputData) {
case "Y", "y" -> progressGame(board);
default -> System.out.println("Unrecognized input. Terminating session.");
}
}
/**
* Displays the board, prompts a move to be made, sends it to the board,
* and while the gameState = true, recurses
* @see ChessBoard#display()
* @see #movePrompt(String colorToMove)
* @see ChessBoard#doMove(String inputData)
*
* @param board chessboard to perform the actions on
*/
public static void progressGame(ChessBoard board) {
// display board and generate move prompt by color to move (FEN6[1])
board.display();
String inputData = movePrompt(board.getBoardState()[1]);
inputData = inputData.trim();
// end the game if the gameState is false or the user inputs "stop"
if (!board.isGameState() || inputData.equals("stop") || inputData.equals("Stop")) {
endGame(board, board.isGameState());
return;
}
// update board with inputted move and recurse
board.doMove(inputData);
progressGame(board);
}
private static void endGame(ChessBoard board, boolean gameNotOver) {
// convert the FEN6 into a FEN
String endFEN = "";
for (String s : board.getBoardState()) {
endFEN += s;
endFEN += " ";
}
// print the FEN and, if the game is still going on, instruct to save for
// reentry; if the game is over, prompt to play again
System.out.println("Your FEN is:");
System.out.println(endFEN.trim());
if (gameNotOver) {
System.out.println(" But the game's not over! Save the FEN and "
+ "input it when you come back to keep playing!");
} else {
System.out.println("That was a great game! Play again? Y/N");
System.out.print(" ");
String inputData = stdin.next();
inputData = inputData.trim();
// begin a new game on Y, terminate on N or other character
switch (inputData) {
case "Y", "y" -> newGame();
case "N", "n" -> System.out.println("Okay, terminating session.");
default -> System.out.println("Unrecognized input. Terminating session.");
}
}
}
/**
* Prompts the player to input a move in
* <a href="https://en.wikipedia.org/wiki/Algebraic_notation_(chess)#Long_algebraic_notation">expanded algebraic notation</a>.
* Trims and reads to open {@link #helpMenu() help menu} if necessary.
* @see #stdin
*
* @param colorToMove from a FEN6, {@linkplain FEN#getFEN6 FEN6}[1]; <b>must be 'w' or 'b'</b>
*
* @return move (user input, unvalidated)
* @see #progressGame(ChessBoard board)
*/
public static String movePrompt(String colorToMove) {
// moves must be in expanded algebraic notation
if (colorToMove.equals("w")) {
System.out.println("White to move.");
} else {
System.out.println("Black to move.");
}
System.out.print("Please enter your move in expanded algebraic notation: ");
String inputData = stdin.nextLine();
inputData = inputData.trim();
// bring up the help menu and recurse on input "help")
if (inputData.equals("help") || inputData.equals("Help")) {
helpMenu();
return movePrompt(colorToMove);
}
return inputData;
}
private static void helpMenu() {
System.out.println("""
-------------------------------------------------------
Expanded algebraic notation requires that you write
start and end position of every piece moved; if the
piece is not a pawn, the piece letter (K, Q, R, B, N)
should also be included before the move. If a move is
a capture, include "x" in between the start and end
positions. If a move gives check, add "+" to the very
end of the notation (after special moves); if
checkmate, add "#".
See https://en.wikipedia.org/wiki/Chess_notation for
examples, under "Long algebraic."
Special moves:
For castling, simply write "O-O" or "0-0" for kingside
and "O-O-O" or "0-0-0".
In the case of pawn promotion, add "=X" on the end of
the notation, where X is the promoted piece letter.
En passant should be recorded as a standard capture.
-------------------------------------------------------
""");
}
}