- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleController.java
More file actions
Latest commit
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
importGame.*;
importShared.Color;
importGame.Persistence;
importShared.Value;
importjava.io.File;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importjava.util.*;
publicclassConsoleControllerimplementsController {
Game.ControllergameController = newGame.Controller();
booleanunicode;
privatestaticfinalEnumMap<Color, String> squares = newEnumMap<>(Color.class);
static {
squares.put(Color.WHITE, "\u25FB");
squares.put(Color.BLACK, "\u25FC");
squares.put(Color.NONE, "\u0020"); // normal space
}
privatestaticfinalEnumMap<Value, EnumMap<Color, Character>>
symbols = newEnumMap<>(Value.class);
//private static final String
static {
//White Black
char[][] s = newchar[][]{
newchar[]{'\u2659', '\u265F'}, //Pawn
newchar[]{'\u2657', '\u265D'}, //Bishop
newchar[]{'\u2658', '\u265E'}, //Knight
newchar[]{'\u2656', '\u265C'}, //Rook
newchar[]{'\u2655', '\u265B'}, //Queen
newchar[]{'\u2654', '\u265A'} //King
};
EnumMap<Color, Character> putInto;
inti = 0;
for (Valuevalue:
Value.values()) {
putInto = newEnumMap<>(Color.class);
putInto.put(Color.WHITE, s[i][0]);
putInto.put(Color.BLACK, s[i][1]);
symbols.put(value, putInto);
i ++;
}
}
privatePathsaveGamePath = Paths.get(newFile("").toURI());
publicConsoleController() {
newConsoleController(true);
}
@Override
publicvoidsetSavegamePath(Pathpath){
saveGamePath = path;
}
publicConsoleController(booleanunicode) {
this.unicode = unicode;
}
publicvoidstartGame() {
//doGameLoop();
//-----------------------------------------------------------------------------------
// provisorisch
Scannerinput = newScanner(System.in);
while (gameController.getCurrentPly()==0 ||
gameController.getLatestMove().state == null) {
printBoards();
System.out.println("Enter move String:");
Stringin = 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));
}
//-----------------------------------------------------------------------------------
}
privatevoiddoGameLoop(){
booleanvalid;
Movemove = null;
ColorturnPlayer = Color.WHITE;
Scannerinput = newScanner(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");
StringinString = input.nextLine();
if (inString.equals("exit")) breakgameLoop;
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")) breakgameLoop;
if (inString.equals("cancel")) break;
BoardsourceBoard = 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")) breakgameLoop;
if (inString.equals("cancel")) {
System.out.println("That board is empty");
break;
}
SquaresourceSquare = sourceBoard.getSquare(inString);
PiecesourcePiece = 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")) breakgameLoop;
if (inString.equals("cancel")) break;
SquaredestinationSquare = sourceBoard.getSquare(inString);
if (sourceBoard.getPiece(destinationSquare) != null &&
sourceBoard.getPiece(destinationSquare).getColor() ==
gameController.getCurrentPlayer()){
System.out.println("That Square is occupied");
break;
}
booleancontained = false;
for (Squaresquare :
sourceBoard.canGoTo(sourcePiece)) {
if (square == destinationSquare) {
contained = true;
break;
}
}
if (!contained){
System.out.println("Can't move there");
break;
}
//-------------------
// find out move type
//-------------------
// Castle
intsourceRow = sourceSquare.row;
intsourceColumn = sourceSquare.column;
intdestinationRow = destinationSquare.row;
intdestinationColumn = destinationSquare.column;
intbaseRow = 0;
if (sourceBoard.color == Color.BLACK) baseRow = 7;
if (sourcePiece.value == Value.KING &&
sourceRow == baseRow &&
sourceColumn == 4 &&
destinationRow == baseRow &&
Math.abs(sourceColumn-destinationColumn) == 2){
ValuecastleDirection = Value.QUEEN;
if (destinationColumn == 6) castleDirection = Value.KING;
contained = false;
for (Valuedir: gameController.canCastleTo()) {
if (dir == castleDirection) {
contained = true;
break;
}
}
if (!contained) {
System.out.println("Can't castle (into that direction)");
}
move = newMove(
turnPlayer,
null,
null,
newCharacter[]{castleDirection.name},
newCharacter[]{},
newString[]{}
);
}
//en passant
intdirection = 1;
MovelatestMove = 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 = newMove(
turnPlayer,
null,
null,
newCharacter[]{},
newCharacter[]{},
newString[]{}
);
}
// inCheck for pinned / king moving into inCheck
break;
case"Exchange":
case"E":
break;
case"Drop":
case"D":
break;
}
if (valid){
gameController.doMove(move);
}
}
}
privatevoidprintBoards(booleanprintCoords){
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();
Boardalpha = gameController.getBoard(Color.WHITE);
Boardbeta = gameController.getBoard(Color.BLACK);
Boardgamma = gameController.getBoard(Color.NONE);
LinkedList<String> lines = newLinkedList<>();
Stringline;
introw;
intcolumn;
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));
}
} elseif (column == 4 || column == 13) { // margin
line += squares.get(Color.NONE);
} elseif (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));
}
}
}elseif (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);
}
}elseif (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));
}
} elseif (column == 4 || column == 13) { // margin
line += squares.get(Color.NONE);
} elseif (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));
}
}
}elseif (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));
}
} elseif (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));
}
}
}elseif (row == 9){ // margin
line += squares.get(Color.NONE);
}else{
if (column <= 7){ // alpha
line += getPiece(alpha, row-10, column);
} elseif (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));
}
privateColorgetSquareColor(introw, intcolumn){
intres = row%2 ^ column%2;
if (res == 0){
returnColor.BLACK;
}
returnColor.WHITE;
}
privateCharactergetPiece(Boardboard, introw, intcolumn){
Pieceresult = board.getPiece(7-row, column);
if (result == null){
returnsquares.get(getSquareColor(7-row, column)).charAt(0);
}
if (board.color==Color.BLACK){
}
returnsymbols.get(result.value).get(result.getColor());
}
privatevoidprintBoards(){
printBoards(false);
}
}