-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
357 lines (321 loc) · 7.66 KB
/
Player.java
File metadata and controls
357 lines (321 loc) · 7.66 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
import java.util.*;
public class Player implements Comparable<Player>{
private String name;
private int score;
private Tile[][] wall, coloredLines;
private ArrayList<Tile> floorLine;
private boolean isFirst;
private Bag bag;
private BoardReference reference;
private int ID;
private double rankingPoints;
//constructors
public Player(String name, Bag bag, int ID) {
this.name = name;
this.bag = bag;
this.ID = ID;
resetBoard();
resetFirst();
reference = new BoardReference();
}
//returns name
public String getName() {return name;}
//returns score
public int getScore() {return score;}
//adds a certain score to the score
public void addScore(int add) {score += add;}
// subtracts a certain score from the score
public void subtractScore(int subtract) {score-=subtract;}
//returns if the player is first
public boolean ifFirst() {return isFirst;}
//adds tiles to the colored lines of a specific row
public void addTile(ArrayList<Tile> newTiles, int r) {
for(int c = coloredLines[r].length-1; c>=0; c--) {
if(newTiles.size()>0 && coloredLines[r][c]==null) {
coloredLines[r][c] = newTiles.remove(0);
}
}
if(newTiles.size()>0) {
addFloorTiles(newTiles);
}
newTiles.clear();
}
public void addFloorTiles(ArrayList<Tile> newTiles) {
while(newTiles.size()>0 && floorLine.size()<7) {
floorLine.add(newTiles.remove(0));
}
if(newTiles.size()>0) {
bag.addDiscardedTiles(newTiles);
}
}
public void addFirstTile() {
floorLine.add(0, new Tile("First Tile"));
if(floorLine.size()>7) {
floorLine.remove(floorLine.size()-1);
}
}
//returns the colored line
public Tile[][] getColoredLine() {return coloredLines;}
//returns floor line
public ArrayList<Tile> getFloorLine(){return floorLine;}
//returns the wall
public Tile[][] getWall(){return wall;}
//sets the player as first player
public void setFirst() {isFirst = true;}
//resets the players score and board
public void resetBoard() {
score = 0;
wall = new Tile[5][5];
Tile[][] tempArr = {{null},{null, null},{null, null, null},{null, null, null, null},{null, null, null, null, null}};
coloredLines = tempArr;
floorLine = new ArrayList<Tile>();
}
//resets is first to false
public void resetFirst() {isFirst = false;}
public ArrayList<Integer> getEmptyColorLine(int row, String color){
ArrayList<Integer> emptyIndexes = new ArrayList<>();
for(int i = 0; i<coloredLines[row].length;i++) {
if(coloredLines[row][i]==null&&ifSameColorAsColorLine(row, color) && checkValidColorSpotByWall(row, color)) {
emptyIndexes.add(coloredLines[row].length-1-i);
}
}
return emptyIndexes;
}
public boolean ifSameColorAsColorLine(int row, String color) {
if(coloredLines[row][coloredLines[row].length-1]==null) {
return true;
}
if(coloredLines[row][coloredLines[row].length-1].getColor().equals(color)) {
return true;
}
return false;
}
public boolean ifSameColorAsColorLineExcludeNull(int row, String color) {
if(coloredLines[row][coloredLines[row].length-1]==null) {
return false;
}
if(coloredLines[row][coloredLines[row].length-1].getColor().equals(color)) {
return true;
}
return false;
}
public boolean checkValidColorSpotByWall(int r, String color) {
for(Tile t:wall[r]) {
if(t!=null) {
if(t.getColor().equals(color)) {
return false;
}
}
}
return true;
}
public void score() {
ArrayList<Tile> discardTiles = new ArrayList<>();
for(int i = 0; i<5; i++) {
if(ifColoredLineRowIsFilled(i)) {
int col = reference.getLocOfColorOnWall(i, coloredLines[i][coloredLines[i].length-1].getColor());
wall[i][col] = new Tile(coloredLines[i][coloredLines[i].length-1].getColor());
int up = scanUp(i-1, col);
int down = scanDown(i+1, col);
int right = scanRight(i, col+1);
int left = scanLeft(i,col-1);
score += up + down + right + left;
if(up>0 || down>0) {
score++;
}
if(right>0 || left>0) {
score++;
}
if(right <=0 && left<=0 && up <=0 && down <=0) {
score++;
}
for(int x = 0; x<coloredLines[i].length-1;x++) {
System.out.println("Filling up " + name + " 's discardTiles");
discardTiles.add(new Tile(coloredLines[i][coloredLines[i].length-1].getColor()));
}
for(int k = 0; k<coloredLines[i].length;k++) {
coloredLines[i][k] = null;
}
}
}
if(discardTiles.size()>0) {
bag.addDiscardedTiles(discardTiles);
}
for(int i = 0; i<floorLine.size();i++) {
score-=reference.getNegPoints(i);
if(score<0) {
score = 0;
}
}
if(floorLine.size()>0) {
bag.addDiscardedTiles(floorLine);
}
floorLine.clear();
discardTiles.clear();
}
public boolean ifColoredLineRowIsFilled(int r) {
for(Tile t:coloredLines[r]) {
if(t==null) {
return false;
}
}
return true;
}
public int scanUp(int r, int c) {
if(r<0) {
return 0;
}
if(wall[r][c] == null) {
return 0;
}
return 1+scanUp(r-1, c);
}
public int scanDown(int r, int c) {
if(r>=5) {
return 0;
}
if(wall[r][c] == null) {
return 0;
}
return 1+scanDown(r+1, c);
}
public int scanRight(int r, int c) {
if(c>=5) {
return 0;
}
if(wall[r][c] == null) {
return 0;
}
return 1+scanRight(r, c+1);
}
public int scanLeft(int r, int c) {
if(c<0) {
return 0;
}
if(wall[r][c] == null) {
return 0;
}
return 1+scanLeft(r, c-1);
}
public void printWall() {
System.out.println(name + "'s wall:");
for(Tile[] r:wall) {
for(Tile c:r) {
if(c!=null) {
System.out.print(c.getColor() + " ");
}
else {
System.out.print("null ");
}
}
System.out.println();
}
}
public boolean ifWallHasColor(int row, String color) {
for(Tile t:wall[row]) {
if(t!=null) {
if(t.getColor().equals(color)) {
return true;
}
}
}
return false;
}
public boolean ifWallRowIsFilled(int r) {
for(Tile t:wall[r]) {
if(t==null) {
return false;
}
}
return true;
}
public boolean ifColIsFilled(int c) {
for(int r = 0; r<wall.length;r++) {
if(wall[r][c] == null) {
return false;
}
}
return true;
}
public int getPointsFromAllColor() {
int numOfBlack = 0;
int numOfBlue = 0;
int numOfLightBlue = 0;
int numOfRed = 0;
int numOfYellow = 0;
for(Tile[] r:wall) {
for(Tile c:r) {
if(c!=null) {
if(c.getColor().equals("black")) {
numOfBlack ++;
}
else if(c.getColor().equals("blue")) {
numOfBlue++;
}
else if(c.getColor().equals("light blue")) {
numOfLightBlue++;
}
else if(c.getColor().equals("red")) {
numOfRed++;
}
else if(c.getColor().equals("yellow")) {
numOfYellow++;
}
}
}
}
return numOfBlack/5*10 + numOfBlue/5*10 + numOfLightBlue/5*10 + numOfRed/5*10 + numOfYellow/5*10;
}
public void finalScoring() {
for(int r = 0; r<wall.length;r++) {
if(ifWallRowIsFilled(r)) {
score+=2;
}
}
for(int c = 0; c<wall.length;c++) {
if(ifColIsFilled(c)) {
score+=7;
}
}
score+=getPointsFromAllColor();
}
public int getRowsFilled() {
int count = 0;
for(int r = 0; r<wall.length;r++) {
if(ifWallRowIsFilled(r)) {
count++;
}
}
return count;
}
public int getColsFilled() {
int count = 0;
for(int c = 0; c<wall[0].length;c++) {
if(ifColIsFilled(c)) {
count++;
}
}
return count;
}
public int getTilesOnWall() {
int count = 0;
for(Tile[] r: wall) {
for(Tile c: r) {
if(c!=null) {
count++;
}
}
}
return count;
}
public int getID() {
return ID;
}
public int compareTo(Player otherPlayer) {
return this.score - ((Player)otherPlayer).getScore();
}
public double getRankingPoints() {
rankingPoints = (double)score + .10 * (double)getRowsFilled();
return rankingPoints;
}
}