-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameStats.java
156 lines (133 loc) · 5.06 KB
/
GameStats.java
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
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
public class GameStats {
// Deals with all the stats in the game - Comperators, prints, etc.
private ArrayList<ConcretePiece> defendersList;
private ArrayList<ConcretePiece> attackersList;
private ArrayList<Position> positionsList;
private Player winner;
public GameStats() {
defendersList = new ArrayList<>();
attackersList = new ArrayList<>();
positionsList = new ArrayList<>();
this.winner = null;
}
public void addPieceStats(Player owner, ConcretePiece piece) {
if (owner.isPlayerOne()){
defendersList.add(piece);
}
else {
attackersList.add(piece);
}
}
public void setWinner (Player winner) {
this.winner = winner;
}
// parses a matrix of positions to an arraylist, only position with at least 1 visit get to the array
public void setPositionsList(Position[][] arr) {
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[0].length; j++){
if (arr[i][j].getNumberOfVisits() > 0)
positionsList.add(arr[i][j]);
}
}
}
// Gets a list of concretePiece and returns a list of the pawns in it (without the king)
private ArrayList<Pawn> getPawnsList(ArrayList<ConcretePiece> arr) {
ArrayList<Pawn> newArr = new ArrayList<>();
for (ConcretePiece piece : arr) {
if (piece.getClass().equals(Pawn.class))
newArr.add((Pawn) piece);
}
return newArr;
}
// Comperators
Comparator<Pawn> compareByKills = Comparator.comparing(Pawn::getKills);
Comparator<ConcretePiece> compareByMoves = Comparator.comparing(ConcretePiece::getNumberOfMoves);
// Compare by Squares
Comparator<ConcretePiece> compareBySteps = Comparator.comparing(ConcretePiece::getDistanceTraveled);
Comparator<ConcretePiece> compareByIdNum = new CompareByIdNum();
public static class CompareByIdNum implements Comparator<ConcretePiece> {
public int compare(ConcretePiece ps1, ConcretePiece ps2) {
if (Integer.parseInt(ps1.getId().substring(1)) < Integer.parseInt(ps2.getId().substring(1)))
return -1;
else if (Integer.parseInt(ps1.getId().substring(1)) > Integer.parseInt(ps2.getId().substring(1)))
return 1;
else
return 0;
}
}
// Compare by Position visits
Comparator<Position> compareByVisits = Comparator.comparing(Position::getNumberOfVisits);
Comparator<Position> compareByRow = Comparator.comparing(Position::getRow);
Comparator<Position> compareByCol = Comparator.comparing(Position::getCol);
// Prints
public void printMoves(){
ArrayList<ConcretePiece> winnerArr = attackersList,
loserArr = defendersList;
if (winner.isPlayerOne()) {
winnerArr = defendersList;
loserArr = attackersList;
}
printMoves(winnerArr);
printMoves(loserArr);
}
private void printMoves (ArrayList<ConcretePiece> arr){
arr.sort(compareByMoves);
for (ConcretePiece piece : arr) {
if (piece.getNumberOfMoves() > 1)
piece.printMoves();
}
}
public void printKills(){
ArrayList<Pawn> defenderPawns = getPawnsList(defendersList),
attackerPawns = getPawnsList(attackersList);
ArrayList<Pawn> mergedList;
if (winner.isPlayerOne()) {
mergedList = new ArrayList<Pawn>(defenderPawns);
mergedList.addAll(attackerPawns);
}
else {
mergedList = new ArrayList<Pawn>(attackerPawns);
mergedList.addAll(defenderPawns);
}
printKills(mergedList);
}
private void printKills (ArrayList<Pawn> arr){
arr.sort(compareByIdNum);
arr.sort(compareByKills.reversed());
for (Pawn pawn : arr) {
if (pawn.getKills() > 0)
pawn.printKills();
}
}
public void printSteps(){
ArrayList<ConcretePiece> winnerArr = attackersList,
loserArr = defendersList;
if (winner.isPlayerOne()) {
winnerArr = defendersList;
loserArr = attackersList;
}
ArrayList<ConcretePiece> mergedList = new ArrayList<>(winnerArr);
mergedList.addAll(loserArr);
printSteps(mergedList);
}
private void printSteps (ArrayList<ConcretePiece> arr){
arr.sort(compareByIdNum);
arr.sort(compareBySteps.reversed());
for (ConcretePiece pieceStats : arr) {
if (pieceStats.getDistanceTraveled() > 0)
pieceStats.printSteps();
}
}
public void printPositionVisits() {
positionsList.sort(compareByCol);
positionsList.sort(compareByRow);
positionsList.sort(compareByVisits.reversed());
for (Position pos : positionsList) {
if (pos.getNumberOfVisits() >= 2)
pos.printVisits();
}
}
}