-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameLogic.java
More file actions
650 lines (575 loc) · 26 KB
/
Copy pathGameLogic.java
File metadata and controls
650 lines (575 loc) · 26 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import java.util.ArrayList;
import java.util.Arrays;
public class GameLogic implements PlayableLogic {
/**
* variable that represents the position of the king for to know if the game finished
*/
private Position posKing;
/**
* variable that represents the blue Player
*/
private final ConcretePlayer player1;
/**
* variable that represents the red Player
*/
private final ConcretePlayer player2;
/**
* variable that represents the board with pieces as in GUI
*/
private final ConcretePiece[][] board;
/**
* variable that save all the positions by "board"
*/
private final Position[][] boardOfPosition;
/**
* variable that indicates whether the game is over
*/
private boolean gameFinish;
/**
* variable that indicates Who won this turn?
*/
private boolean playerOneWin;
/**
* variable that indicate who player won
*/
private Player winner;
/**
* class variable that help us to know whose turn it is?,
* At first It is initialized true because is the turn of player 2
*/
private boolean isSecondPlayerTurn;
/**
* all the pieces on the board in array that help us to print a statistic when the game done
*/
private final ConcretePiece[] pieces = new ConcretePiece[37];
public GameLogic() {
this.player1 = new ConcretePlayer(true);
this.player2 = new ConcretePlayer(false);
this.board = new ConcretePiece[11][11];
this.posKing = new Position(5, 5);
this.boardOfPosition = new Position[11][11];
gameFinish = false;
isSecondPlayerTurn = true;
startOfGame();
initBoardOfPos(board);
}
/**
* Check if it's legal step - piece moves on straight lines (up, down, left, right)
* piece move only to free location
* piece can't pass over another piece
*/
private boolean isLegalStep(Position a, Position b) {
if (a.equals(b))
return false;
if (!a.isInside() || !b.isInside())
return false;
// check if the move is a piece, and not null
if (getPieceAtPosition(a) == null)
return false;
// if the b cell is not empty, return false
if (getPieceAtPosition(b) != null)
return false;
// if we move with "pawn" we can't pass to the corner
if (getPieceAtPosition(a) instanceof Pawn &&
(b.getX() == 10 && b.getY() == 0 ||
b.getX() == 0 && b.getY() == 10
|| b.getX() == 10 && b.getY() == 10
|| b.getX() == 0 && b.getY() == 0)) {
return false;
}
// check if he tries to move in diagonal
if ((a.getY() != b.getY() && a.getX() != b.getX()))
return false;
for (int i = a.getX() + 1; i < b.getX(); i++) {
if (board[b.getY()][i] != null)
return false;
}
for (int i = a.getX() - 1; i > b.getX(); i--) {
if (board[b.getY()][i] != null)
return false;
}
for (int i = a.getY() + 1; i < b.getY(); i++) {
if (board[i][b.getX()] != null)
return false;
}
for (int i = a.getY() - 1; i > b.getY(); i--) {
if (board[i][b.getX()] != null)
return false;
}
return true;
}
/**
* "eatOptions" is function that realizes eats according to the rules of the game
*
* @param b the position
*/
private void eatOptions(Position b) {
ConcretePiece current = board[b.getY()][b.getX()];
//eat to right side
if (b.getX() + 1 < 11 && board[b.getY()][b.getX() + 1] != null &&
(board[b.getY()][b.getX() + 1].getOwner() != current.getOwner()) &&
(board[b.getY()][b.getX() + 1] instanceof Pawn)) {
// check if the neighbour is not king and in the edge, if it is - eat
if (b.getX() + 1 == 10) {
if (!(board[b.getY()][b.getX() + 1] instanceof King)) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() + 1] = null;
}
//if neighbour right x+1 and x+2 are not null
} else if (b.getX() + 2 < 11 && board[b.getY()][b.getX() + 2] != null &&
(board[b.getY()][b.getX() + 2].getOwner() == current.getOwner()) &&
(board[b.getY()][b.getX() + 2] instanceof Pawn)) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() + 1] = null;
// eating with the green square when moving right
} else if ((b.getX() + 2 == 10 && b.getY() == 10) || ((b.getX() + 2 == 10) && (b.getY() == 0))) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() + 1] = null;
}
}
//eat to left side
if (b.getX() - 1 >= 0 && board[b.getY()][b.getX() - 1] != null &&
(board[b.getY()][b.getX() - 1].getOwner() != current.getOwner()) &&
(board[b.getY()][b.getX() - 1] instanceof Pawn)) {
// check if the neighbour is not king and in the edge, if it is - eat
if (b.getX() - 1 == 0) {
if (!(board[b.getY()][b.getX() - 1] instanceof King))
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() - 1] = null;
//if neighbour left x-1 and x-2 are not null
} else if (b.getX() - 2 >= 0 && board[b.getY()][b.getX() - 2] != null &&
(board[b.getY()][b.getX() - 2].getOwner() == current.getOwner()) &&
(board[b.getY()][b.getX() - 2] instanceof Pawn)) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() - 1] = null;
// eating with the green square when moving left
} else if ((b.getX() - 2 == 0 && b.getY() == 10) || ((b.getX() - 2 == 0) && (b.getY() == 0))) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY()][b.getX() - 1] = null;
}
}
//eat to upside
if (b.getY() - 1 >= 0 && board[b.getY() - 1][b.getX()] != null &&
(board[b.getY() - 1][b.getX()].getOwner() != current.getOwner()) &&
(board[b.getY() - 1][b.getX()] instanceof Pawn)) {
// check if the neighbour is not king and in the edge, if it is - eat
if (b.getY() - 1 == 0) {
if (!(board[b.getY() - 1][b.getX()] instanceof King))
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() - 1][b.getX()] = null;
//if neighbour up y-1 and y-2 are not null
} else if (b.getY() - 2 >= 0 && board[b.getY() - 2][b.getX()] != null &&
(board[b.getY() - 2][b.getX()].getOwner() == current.getOwner()) &&
(board[b.getY() - 2][b.getX()] instanceof Pawn)) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() - 1][b.getX()] = null;
// eating with the green square when moving UP
} else if ((b.getY() - 2 == 0 && b.getX() == 10) || ((b.getY() - 2 == 0) && (b.getX() == 0))) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() - 1][b.getX()] = null;
}
}
//eat to downside
if (b.getY() + 1 < 11 && board[b.getY() + 1][b.getX()] != null &&
board[b.getY() + 1][b.getX()].getOwner() != current.getOwner() &&
(board[b.getY() + 1][b.getX()] instanceof Pawn)) {
// check if the neighbour is not king and in the edge, if it is - eat
if (b.getY() + 1 == 10) {
if (!(board[b.getY() + 1][b.getX()] instanceof King))
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() + 1][b.getX()] = null;
//if neighbour down y+1 and y+2 are not null
} else if (b.getY() + 2 < 11 && board[b.getY() + 2][b.getX()] != null &&
(board[b.getY() + 2][b.getX()].getOwner() == current.getOwner()) &&
(board[b.getY() + 2][b.getX()] instanceof Pawn)) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() + 1][b.getX()] = null;
// eating with the green square when moving UP
} else if ((b.getY() + 2 == 10 && b.getX() == 10) || ((b.getY() + 2 == 10) && (b.getX() == 0))) {
((Pawn) current).addKill();//add kill to current pawn
board[b.getY() + 1][b.getX()] = null;
}
}
}
@Override
public boolean move(Position a, Position b) {//TODO add bad location in corner
if (!isLegalStep(a, b)) {//check if the move is illegal
return false;
}
ConcretePlayer currentPlayer = isSecondPlayerTurn() ? player2 : player1;
// if he tries to move piece not in his color
if (board[a.getY()][a.getX()].getOwner() != currentPlayer)
return false;
if (getPieceAtPosition(a) instanceof King) {
this.posKing = new Position(b.getX(), b.getY());
}
ConcretePiece conPe = board[a.getY()][a.getX()];
conPe.addSteps(b);
Position PosTo = boardOfPosition[b.getY()][b.getX()];
PosTo.addWhoWasIn(conPe.getID());
board[b.getY()][b.getX()] = board[a.getY()][a.getX()];
board[a.getY()][a.getX()] = null;
isGameFinishHelp();
// only pawn can eat
if (board[b.getY()][b.getX()] instanceof Pawn) {
eatOptions(b);
}
// switch the turns
this.isSecondPlayerTurn = !this.isSecondPlayerTurn;
return true;
}
//getters{
@Override
public Piece getPieceAtPosition(Position position) {
if (!position.isInside()) return null;
return board[position.getY()][position.getX()];
}
@Override
public Player getFirstPlayer() {
return this.player1;
}
@Override
public Player getSecondPlayer() {
return this.player2;
}
@Override
public boolean isGameFinished() {
return gameFinish;
}
@Override
public int getBoardSize() {//11X11
return 11;
}
/**
* This function check if we have a winner team
* in addition the function initialized parameters such "gameFinish" that we return at the end of game
*/
private void isGameFinishHelp() {
//if the king found in these positions player1 win!
Position rightUP = new Position(10, 0);
Position leftUP = new Position(0, 0);
Position rightDown = new Position(10, 10);
Position leftDown = new Position(0, 10);
if (posKing.equals(rightUP) || posKing.equals(leftUP) || posKing.equals(rightDown)
|| posKing.equals(leftDown)) {
winner = player1;//update the winner
playerOneWin = true;//player1 win
player1.addWins();//add win to player1
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
// check if the king is in the extreme ranks
Piece upEnemy = getPieceAtPosition(new Position(posKing.getX(), posKing.getY() - 1));
Piece downEnemy = getPieceAtPosition(new Position(posKing.getX(), posKing.getY() + 1));
Piece leftEnemy = getPieceAtPosition(new Position(posKing.getX() - 1, posKing.getY()));
Piece rightEnemy = getPieceAtPosition(new Position(posKing.getX() + 1, posKing.getY()));
// check if the king is in the down column
if (posKing.getY() == 10 &&
upEnemy != null && upEnemy.getOwner() == player2 &&
rightEnemy != null && rightEnemy.getOwner() == player2 &&
leftEnemy != null && leftEnemy.getOwner() == player2) {
winner = player2;//update the winner
playerOneWin = false;//player2 win
player2.addWins();//add win to player2
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
// check if the king is in the right row
if (posKing.getX() == 10 &&
leftEnemy != null && leftEnemy.getOwner() == player2 &&
downEnemy != null && downEnemy.getOwner() == player2 &&
upEnemy != null && upEnemy.getOwner() == player2) {
winner = player2;//update the winner
playerOneWin = false;//player2 win
player2.addWins();//add win to player2
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
// check if the king is in the up column
if (posKing.getY() == 0 &&
leftEnemy != null && leftEnemy.getOwner() == player2 &&
downEnemy != null && downEnemy.getOwner() == player2 &&
rightEnemy != null && rightEnemy.getOwner() == player2) {
winner = player2;//update the winner
playerOneWin = false;//player2 win
player2.addWins();//add win to player2
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
// check if the king is in the left row
if (posKing.getX() == 0 &&
rightEnemy != null && rightEnemy.getOwner() == player2 &&
downEnemy != null && downEnemy.getOwner() == player2 &&
upEnemy != null && upEnemy.getOwner() == player2) {
winner = player2;//update the winner
playerOneWin = false;//player2 win
player2.addWins();//add win to player2
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
//in the case the king in center and the enemy's around
Position upEnemyE = new Position(posKing.getX(), posKing.getY() - 1);
Position downEnemyE = new Position(posKing.getX(), posKing.getY() + 1);
Position leftEnemyE = new Position(posKing.getX() - 1, posKing.getY());
Position rightEnemyE = new Position(posKing.getX() + 1, posKing.getY());
if ((getPieceAtPosition(upEnemyE) != null && !getPieceAtPosition(upEnemyE).getOwner().isPlayerOne()) &&
(getPieceAtPosition(downEnemyE) != null && !getPieceAtPosition(downEnemyE).getOwner().isPlayerOne()) &&
(getPieceAtPosition(leftEnemyE) != null && !getPieceAtPosition(leftEnemyE).getOwner().isPlayerOne()) &&
(getPieceAtPosition(rightEnemyE) != null && !getPieceAtPosition(rightEnemyE).getOwner().isPlayerOne())) {
winner = player2;//update the winner
playerOneWin = false;//player2 win
player2.addWins();//add win to player2
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
//in the case that all the red pieces eaten
else {
boolean allRedPawnsEaten = true;//flag for check if there are any player2 on the board
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (this.board[j][i] != null) {
if (this.board[j][i].getOwner() == player2) {
allRedPawnsEaten = false;
}
}
}
}
if (allRedPawnsEaten) {
winner = player1;//update the winner
playerOneWin = true;//player1 win
player1.addWins();//add win to player1
gameFinish = true;//the game finish
printStatistics();//Print the all statistics
}
}
}
@Override
public boolean isSecondPlayerTurn() {
return this.isSecondPlayerTurn;
}
@Override
public void reset() {
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
this.board[j][i] = null;//clear all the board
}
}
posKing = new Position(5, 5);//returning the king to his original place
this.isSecondPlayerTurn = true;//returning the turn to the player2 like in beginning
startOfGame();//putting all the tools on the board again
}
@Override
public void undoLastMove() {//Bonus we didn't have time to do
//TODO-----------------------------------------
}
/**
* Initialized the pieces on the game board
*/
private void startOfGame() {
//king position
board[5][5] = new King(player1, "K7", new Position(5, 5));
//player 1 pawns position
board[3][5] = new Pawn(player1, "D1", new Position(5, 3));
board[4][4] = new Pawn(player1, "D2", new Position(4, 4));
board[4][5] = new Pawn(player1, "D3", new Position(5, 4));
board[4][6] = new Pawn(player1, "D4", new Position(6, 4));
board[5][3] = new Pawn(player1, "D5", new Position(3, 5));
board[5][4] = new Pawn(player1, "D6", new Position(4, 5));
board[5][6] = new Pawn(player1, "D8", new Position(6, 5));
board[5][7] = new Pawn(player1, "D9", new Position(7, 5));
board[6][4] = new Pawn(player1, "D10", new Position(4, 6));
board[6][5] = new Pawn(player1, "D11", new Position(5, 6));
board[6][6] = new Pawn(player1, "D12", new Position(6, 6));
board[7][5] = new Pawn(player1, "D13", new Position(5, 7));
//player 2 pawns position
//up
board[0][3] = new Pawn(player2, "A1", new Position(3, 0));
board[0][4] = new Pawn(player2, "A2", new Position(4, 0));
board[0][5] = new Pawn(player2, "A3", new Position(5, 0));
board[0][6] = new Pawn(player2, "A4", new Position(6, 0));
board[0][7] = new Pawn(player2, "A5", new Position(7, 0));
board[1][5] = new Pawn(player2, "A6", new Position(5, 1));
//left
board[3][0] = new Pawn(player2, "A7", new Position(0, 3));
board[4][0] = new Pawn(player2, "A9", new Position(0, 4));
board[5][0] = new Pawn(player2, "A11", new Position(0, 5));
board[6][0] = new Pawn(player2, "A15", new Position(0, 6));
board[7][0] = new Pawn(player2, "A17", new Position(0, 7));
board[5][1] = new Pawn(player2, "A12", new Position(1, 5));
//right
board[3][10] = new Pawn(player2, "A8", new Position(10, 3));
board[4][10] = new Pawn(player2, "A10", new Position(10, 4));
board[5][10] = new Pawn(player2, "A14", new Position(10, 5));
board[6][10] = new Pawn(player2, "A16", new Position(10, 6));
board[7][10] = new Pawn(player2, "A18", new Position(10, 7));
board[5][9] = new Pawn(player2, "A13", new Position(9, 5));
//down
board[10][3] = new Pawn(player2, "A20", new Position(3, 10));
board[10][4] = new Pawn(player2, "A21", new Position(4, 10));
board[10][5] = new Pawn(player2, "A22", new Position(5, 10));
board[10][6] = new Pawn(player2, "A23", new Position(6, 10));
board[10][7] = new Pawn(player2, "A24", new Position(7, 10));
board[9][5] = new Pawn(player2, "A19", new Position(5, 9));
//Initializing the pieces array
//king
pieces[0] = board[5][5];
//defence
pieces[1] = board[3][5];
pieces[2] = board[4][4];
pieces[3] = board[4][5];
pieces[4] = board[4][6];
pieces[5] = board[5][3];
pieces[6] = board[5][4];
pieces[7] = board[5][6];
pieces[8] = board[5][7];
pieces[9] = board[6][4];
pieces[10] = board[6][5];
pieces[11] = board[6][6];
pieces[12] = board[7][5];
//attack
//up
pieces[13] = board[0][3];
pieces[14] = board[0][4];
pieces[15] = board[0][5];
pieces[16] = board[0][6];
pieces[17] = board[0][7];
pieces[18] = board[1][5];
//left
pieces[19] = board[3][0];
pieces[20] = board[4][0];
pieces[21] = board[5][0];
pieces[22] = board[6][0];
pieces[23] = board[7][0];
pieces[24] = board[5][1];
//right
pieces[25] = board[3][10];
pieces[26] = board[4][10];
pieces[27] = board[5][10];
pieces[28] = board[6][10];
pieces[29] = board[7][10];
pieces[30] = board[5][9];
//down
pieces[31] = board[10][3];
pieces[32] = board[10][4];
pieces[33] = board[10][5];
pieces[34] = board[10][6];
pieces[35] = board[10][7];
pieces[36] = board[9][5];
}
/**
* The above 5 functions:"printKillData" "printLengthData" "printDestData" "printPositionData" "printStatistics"
* use to help us print the date in the required manner
*/
private void printKillData(ConcretePiece[] pieces) {//prints the statistics associated with the kill
ArrayList<Pawn> list = new ArrayList<>();
for (int i = 1; i < pieces.length; i++) {//without the king in pieces[0]
if ((pieces[i]).getKills() > 0) {
list.add(((Pawn) pieces[i]));
}
}
list.sort(new PieceKillsCompare(winner));
for (Pawn current : list) {
System.out.println(current.getID() + ": " + current.getKills() + " kills");
}
}
private void printLengthData(ConcretePiece[] pieces) {//prints the statistics associated with all the steps of piece
ArrayList<ConcretePiece> listPlayer1 = new ArrayList<>();
ArrayList<ConcretePiece> listPlayer2 = new ArrayList<>();
for (int i = 0; i < 13; i++) {
if (pieces[i].getNumOfSteps() > 1) {
listPlayer1.add(pieces[i]);
}
}
listPlayer1.sort(new PieceLengthCompare());//piece in array sorted by length steps in defended set
for (int i = 13; i < 37; i++) {
if (pieces[i].getNumOfSteps() > 1) {
listPlayer2.add(pieces[i]);
}
}
listPlayer2.sort(new PieceLengthCompare());//piece in array sorted by length steps in attack set
if (playerOneWin) {
for (ConcretePiece current : listPlayer1) {
System.out.println(current.getID() + ": " + Arrays.toString(current.getStepsArr()));
}
for (ConcretePiece current : listPlayer2) {
System.out.println(current.getID() + ": " + Arrays.toString(current.getStepsArr()));
}
} else {
for (ConcretePiece current : listPlayer2) {
System.out.println(current.getID() + ": " + Arrays.toString(current.getStepsArr()));
}
for (ConcretePiece current : listPlayer1) {
System.out.println(current.getID() + ": " + Arrays.toString(current.getStepsArr()));
}
}
}
private void printDestData(ConcretePiece[] pieces) {//prints the statistics associated with the length of piece steps
for (ConcretePiece current : pieces) {
for (int j = 0; j < current.steps.size() - 1; j++) {
Position pos = current.steps.get(j);
Position poseCurrent = current.steps.get(j + 1);
if (pos.getX() == poseCurrent.getX()) {
int distance = Math.abs(pos.getY() - poseCurrent.getY());
current.addDest(distance);
} else if (pos.getY() == poseCurrent.getY()) {
int distance = Math.abs(pos.getX() - poseCurrent.getX());
current.addDest(distance);
}
}
}
ArrayList<ConcretePiece> listPlayers = new ArrayList<>();
for (ConcretePiece piece : pieces) {
if (piece.getDistanceTraveled() > 0) {
listPlayers.add(piece);
}
}
listPlayers.sort(new PieceSquaresCompare(winner));
for (ConcretePiece current : listPlayers) {
System.out.println(current.getID() + ": " + current.getDistanceTraveled() + " squares");
}
}
private void printPositionData(Position[][] pos) {//prints the statistics associated with the num steps on positions
ArrayList<Position> posComp = new ArrayList<>();
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (pos[j][i] != null) {
posComp.add(pos[j][i]);
}
}
}
posComp.sort(new PiecePositionCompare());
for (Position position : posComp) {
if (position.getWhoWasIn() > 1) {
System.out.println(position.toString() + position.getWhoWasIn() + " pieces");
}
}
}
private void printStatistics() {//function responsible for print all statistics
printLengthData(pieces);
System.out.println("***************************************************************************");
printKillData(pieces);
System.out.println("***************************************************************************");
printDestData(pieces);
System.out.println("***************************************************************************");
printPositionData(boardOfPosition);
System.out.println("***************************************************************************");
}
/**
* This function initialized a board of position use to manage the position of each piece
*/
private void initBoardOfPos(ConcretePiece[][] board) {//initialization matrix of position
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
boardOfPosition[j][i] = new Position(i, j);
}
}
for (int i = 0; i < 11; i++) {//add to positions in beaning the pieceID of beginning that stand on them
for (int j = 0; j < 11; j++) {
if (board[j][i] != null) {//there is any piece
Position temp = boardOfPosition[j][i];
temp.addWhoWasIn(board[j][i].getID());
}
}
}
}
}