Repository navigation
Expand file tree
/
Copy pathConsoleController.java
More file actions
359 lines (336 loc) · 15.3 KB
/
Copy pathConsoleController.java
File metadata and controls
359 lines (336 loc) · 15.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import Game.*;
import Shared.Color;
import Game.Persistence;
import Shared.Value;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class ConsoleController implements Controller {
Game.Controller gameController = new Game.Controller();
boolean unicode;
private static final EnumMap<Color, String> squares = new EnumMap<>(Color.class);
static {
squares.put(Color.WHITE, "\u25FB");
squares.put(Color.BLACK, "\u25FC");
squares.put(Color.NONE, "\u0020"); // normal space
}
private static final EnumMap<Value, EnumMap<Color, Character>>
symbols = new EnumMap<>(Value.class);
//private static final String
static {
//White Black
char[][] s = new char[][]{
new char[]{'\u2659', '\u265F'}, //Pawn
new char[]{'\u2657', '\u265D'}, //Bishop
new char[]{'\u2658', '\u265E'}, //Knight
new char[]{'\u2656', '\u265C'}, //Rook
new char[]{'\u2655', '\u265B'}, //Queen
new char[]{'\u2654', '\u265A'} //King
};
EnumMap<Color, Character> putInto;
int i = 0;
for (Value value:
Value.values()) {
putInto = new EnumMap<>(Color.class);
putInto.put(Color.WHITE, s[i][0]);
putInto.put(Color.BLACK, s[i][1]);
symbols.put(value, putInto);
i ++;
}
}
private Path saveGamePath = Paths.get(new File("").toURI());
public ConsoleController() {
new ConsoleController(true);
}
@Override
public void setSavegamePath(Path path){
saveGamePath = path;
}
public ConsoleController(boolean unicode) {
this.unicode = unicode;
}
public void startGame() {
//doGameLoop();
//-----------------------------------------------------------------------------------
// provisorisch
Scanner input = new Scanner(System.in);
while (gameController.getCurrentPly()==0 ||
gameController.getLatestMove().state == null) {
printBoards();
System.out.println("Enter move String:");
String in = input.nextLine();
if (in.equals("save")){
//saveManager.saveMoves(gameController.getMoveHistoryStrings());
continue;
}
if (in.startsWith("load ")){
String[] moves = Persistence.loadMoves(in.substring(5));
Arrays.stream(moves).forEach(
s -> gameController.doMove(gameController.decodeMove(s)));
//s -> System.out.println(s));
continue;
}
gameController.doMove(gameController.decodeMove(in));
}
//-----------------------------------------------------------------------------------
}
private void doGameLoop(){
boolean valid;
Move move = null;
Color turnPlayer = Color.WHITE;
Scanner input = new Scanner(System.in);
gameLoop:
while (gameController.getCurrentPly()==0 || gameController.getLatestMove().state == null) {
valid = false;
printBoards();
System.out.println("Input <exit> to exit the application");
System.out.println("Input <Undo> or <U> to undo a move");
System.out.println("Input <Redo> or <R> to redo an undone move");
System.out.println("Input <Move> or <M> to move a piece");
System.out.println("Input <Exchange> or <E> to exchange a hostage");
System.out.println("Input <Drop> or <D> to drop a piece from your airfield");
String inString = input.nextLine();
if (inString.equals("exit")) break gameLoop;
switch (inString){
case "Move":
case "M":
printBoards();
System.out.println("Input <exit> to exit the application");
System.out.println("Input <cancel> to cancel this move");
System.out.println("Move piece from board: <A> or <B> or <C>");
inString = input.nextLine();
if (inString.equals("exit")) break gameLoop;
if (inString.equals("cancel")) break;
Board sourceBoard = gameController.getBoard(inString.charAt(0));
if (sourceBoard.getPieces(null, null).length == 0) break;
printBoards();
System.out.println("Input <exit> to exit the application");
System.out.println("Input <cancel> to cancel this move");
System.out.println("Input the name of the square " +
"you want to move from, e.g. <e4>");
inString = input.nextLine();
if (inString.equals("exit")) break gameLoop;
if (inString.equals("cancel")) {
System.out.println("That board is empty");
break;
}
Square sourceSquare = sourceBoard.getSquare(inString);
Piece sourcePiece = sourceBoard.getPiece(sourceSquare);
if (sourcePiece == null) {
System.out.println("There is no piece on that square");
break;
}
if (sourceBoard.canGoTo(sourcePiece).length == 0){
System.out.println("That piece cannot move");
break;
}
printBoards();
System.out.println("Input <exit> to exit the application");
System.out.println("Input <cancel> to cancel this move");
System.out.println("Input the name of the square " +
"you want to move to e.g. <a6>");
inString = input.nextLine();
if (inString.equals("exit")) break gameLoop;
if (inString.equals("cancel")) break;
Square destinationSquare = sourceBoard.getSquare(inString);
if (sourceBoard.getPiece(destinationSquare) != null &&
sourceBoard.getPiece(destinationSquare).getColor() ==
gameController.getCurrentPlayer()){
System.out.println("That Square is occupied");
break;
}
boolean contained = false;
for (Square square :
sourceBoard.canGoTo(sourcePiece)) {
if (square == destinationSquare) {
contained = true;
break;
}
}
if (!contained){
System.out.println("Can't move there");
break;
}
//-------------------
// find out move type
//-------------------
// Castle
int sourceRow = sourceSquare.row;
int sourceColumn = sourceSquare.column;
int destinationRow = destinationSquare.row;
int destinationColumn = destinationSquare.column;
int baseRow = 0;
if (sourceBoard.color == Color.BLACK) baseRow = 7;
if (sourcePiece.value == Value.KING &&
sourceRow == baseRow &&
sourceColumn == 4 &&
destinationRow == baseRow &&
Math.abs(sourceColumn-destinationColumn) == 2){
Value castleDirection = Value.QUEEN;
if (destinationColumn == 6) castleDirection = Value.KING;
contained = false;
for (Value dir: gameController.canCastleTo()) {
if (dir == castleDirection) {
contained = true;
break;
}
}
if (!contained) {
System.out.println("Can't castle (into that direction)");
}
move = new Move(
turnPlayer,
null,
null,
new Character[]{castleDirection.name},
new Character[]{},
new String[]{}
);
}
//en passant
int direction = 1;
Move latestMove = gameController.getLatestMove();
if (sourcePiece.getColor() == Color.BLACK) direction = -1;
if (sourcePiece.value == Value.PAWN &&
destinationRow == 7 - baseRow - 2*direction &&
latestMove.pieceNames[0] == 'P' //&&
)
// promote
if (sourcePiece.value == Value.PAWN &&
destinationRow == 7-baseRow &&
sourceBoard.color != Color.NONE
)
if (gameController.inCheck()){
move = new Move(
turnPlayer,
null,
null,
new Character[]{},
new Character[]{},
new String[]{}
);
}
// inCheck for pinned / king moving into inCheck
break;
case "Exchange":
case "E":
break;
case "Drop":
case "D":
break;
}
if (valid){
gameController.doMove(move);
}
}
}
private void printBoards(boolean printCoords){
Value[] whitePrison = gameController.getPrison(Color.WHITE).getPieces();
Value[] blackPrison = gameController.getPrison(Color.BLACK).getPieces();
Value[] whiteAirfield = gameController.getAirfield(Color.WHITE).getPieces();
Value[] blackAirfield = gameController.getAirfield(Color.BLACK).getPieces();
Board alpha = gameController.getBoard(Color.WHITE);
Board beta = gameController.getBoard(Color.BLACK);
Board gamma = gameController.getBoard(Color.NONE);
LinkedList<String> lines = new LinkedList<>();
String line;
int row;
int column;
for (row=0; row<18; row++){
line = "";
for (column=0; column<18; column++){
if (row <= 3) { // upper boards
if (column <= 3) { // white Prison
if (row * 4 + column < whitePrison.length) {
line += whitePrison[row * 4 + column];
} else {
line += squares.get(getSquareColor(row, column));
}
} else if (column == 4 || column == 13) { // margin
line += squares.get(Color.NONE);
} else if (column <= 12) { // gamma
line += getPiece(gamma, row, column-5);
} else { // black Prison
if (row * 4 + column < blackPrison.length) {
line += blackPrison[row * 4 + column];
} else {
line += squares.get(getSquareColor(row, column-14));
}
}
}else if (row == 4){ // line between Prisons and Airfields
if (column <= 4 || column >= 13){ // margin
line += squares.get(Color.NONE);
}else{ // gamma
line += getPiece(gamma, row, column-5);
}
}else if (row <= 7){ // until lower border of gamma
if (column <= 3) { // white Airfield
if ((row-5) * 4 + column < whiteAirfield.length) {
line += whiteAirfield[(row-5) * 4 + column];
} else {
line += squares.get(getSquareColor(row-5, column));
}
} else if (column == 4 || column == 13) { // margin
line += squares.get(Color.NONE);
} else if (column <= 12) { // gamma
line += getPiece(gamma, row-5, column-5);
} else { // black Airfield
if ((row-5) * 4 + column < blackAirfield.length) {
line += blackAirfield[(row-5) * 4 + column];
} else {
line += squares.get(getSquareColor(row-5, column-14));
}
}
}else if (row == 8){ // last bit of Prisons and Airfields
if (column <= 3) { // white Airfield
if ((row-6) * 4 + column < whiteAirfield.length) {
line += whiteAirfield[(row-6) * 4 + column];
} else {
line += squares.get(getSquareColor(row-6, column));
}
} else if (column <= 13) { // margin
line += squares.get(Color.NONE);
} else { // black Airfield
if ((row-6) * 4 + column < blackAirfield.length) {
line += blackAirfield[(row-6) * 4 + column];
} else {
line += squares.get(getSquareColor(row-6, column-14));
}
}
}else if (row == 9){ // margin
line += squares.get(Color.NONE);
}else{
if (column <= 7){ // alpha
line += getPiece(alpha, row-10, column);
} else if (column <= 9){ // margin
line += squares.get(Color.NONE);
} else{ // beta
line += getPiece(beta, row-10, column-10);
}
}
}
lines.add(line);
}
lines.stream().forEachOrdered(str -> System.out.println(str));
}
private Color getSquareColor(int row, int column){
int res = row%2 ^ column%2;
if (res == 0){
return Color.BLACK;
}
return Color.WHITE;
}
private Character getPiece(Board board, int row, int column){
Piece result = board.getPiece(7-row, column);
if (result == null){
return squares.get(getSquareColor(7-row, column)).charAt(0);
}
if (board.color==Color.BLACK){
}
return symbols.get(result.value).get(result.getColor());
}
private void printBoards(){
printBoards(false);
}
}