-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
258 lines (235 loc) · 8.48 KB
/
Player.java
File metadata and controls
258 lines (235 loc) · 8.48 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
import java.util.Arrays;
public class Player {
String playerName;
Tile[] playerTiles;
int numberOfTiles;
public Player(String name) {
setName(name);
playerTiles = new Tile[15]; // there are at most 15 tiles a player owns at any time
numberOfTiles = 0; // currently this player owns 0 tiles, will pick tiles at the beggining of the game
}
/*
* This method calculates the longest chain per tile to be used when checking the win condition
*/
public int[] calculateLongestChainPerTile() {
// keep a seperate copy of the tiles since findLongestChainOf sorts them
Tile[] tilesCopy = new Tile[numberOfTiles];
for (int i = 0; i < numberOfTiles; i++) {
tilesCopy[i] = playerTiles[i];
}
// make the calculations
int[] chainLengths = new int[numberOfTiles];
for (int i = 0; i < numberOfTiles; i++) {
chainLengths[i] = findLongestChainOf(tilesCopy[i]);
}
// revert the playerTiles to its original form
for (int i = 0; i < numberOfTiles; i++) {
playerTiles[i] = tilesCopy[i];
}
return chainLengths;
}
/*
* TODO: finds and returns the longest chain of tiles that can be formed
* using the given tile. a chain of tiles is either consecutive numbers
* that have the same color or the same number with different colors
* some chain examples are as follows:
* 1B 2B 3B
* 5Y 5B 5R 5K
* 4Y 5Y 6Y 7Y 8Y
* You can use canFormChainWith method in Tile class to check if two tiles can make a chain
* based on color order and value order. Use sortTilesColorFirst() and sortTilesValueFirst()
* methods to sort the tiles of this player then find the position of the given tile t.
* check how many adjacent tiles there are starting from the tile poisition.
* Note that if you start a chain with matching colors it should continue with the same type of match
* and if you start a chain with matching values it should continue with the same type of match
* use the different values canFormChainWith method returns.
*/
public int findLongestChainOf(Tile t) {
int tilePosition;
sortTilesColorFirst();
tilePosition = findPositionOfTile(t);
// TODO: find the longest chain starting from tilePosition going left and right
int longestChainColorFirst = 0;
int tempPosition = tilePosition;
boolean goFurther = true;
while(goFurther){
if((tilePosition > 0 && tilePosition < numberOfTiles-1)){
if(playerTiles[tilePosition+1].canFormChainWith(playerTiles[tilePosition]) == 1 ){
longestChainColorFirst++;
tilePosition++;
}
else{
goFurther = false;
}
}
else{
goFurther = false;
}
}
goFurther = true;
tilePosition = tempPosition;
while(goFurther){
if((tilePosition > 0 && tilePosition < numberOfTiles-1)){
if(playerTiles[tilePosition-1].canFormChainWith(playerTiles[tilePosition]) == 1){
longestChainColorFirst++;
tilePosition--;
}
else{
goFurther = false;
}
}
else{
goFurther = false;
}
}
goFurther = true;
tilePosition = tempPosition;
sortTilesValueFirst();
tilePosition = findPositionOfTile(t);
// TODO: find the longest chain starting from tilePosition going left and right
int longestChainValueFirst = 0;
while(goFurther){
if((tilePosition > 0 && tilePosition < numberOfTiles-1)){
if(playerTiles[tilePosition+1].canFormChainWith(playerTiles[tilePosition]) == 2){
longestChainValueFirst++;
tilePosition++;
}
else{
goFurther = false;
}
}
else{
goFurther = false;
}
}
goFurther = true;
tilePosition = tempPosition;
while(goFurther){
if((tilePosition > 0 && tilePosition < numberOfTiles-1)){
if(playerTiles[tilePosition-1].canFormChainWith(playerTiles[tilePosition]) == 2){
longestChainValueFirst++;
tilePosition--;
}
else{
goFurther = false;
}
}
else{
goFurther = false;
}
}
goFurther = true;
tilePosition = tempPosition;
if(longestChainColorFirst > longestChainValueFirst) {
return longestChainColorFirst;
}
else{
return longestChainValueFirst;
}
}
/*
* TODO: removes and returns the tile in given index
*/
public Tile getAndRemoveTile(int index) {
Tile tile = playerTiles[index];
playerTiles[index] = null;
for (int i = index; i < playerTiles.length - 1; i++) {
playerTiles[i] = playerTiles[i+1];
}
playerTiles[playerTiles.length - 1] = null;
numberOfTiles--;
System.out.println(Arrays.toString(playerTiles));
return tile;
}
/*
* TODO: adds the given tile at the end of playerTiles array, should also
* update numberOfTiles accordingly. Make sure the player does not try to
* have more than 15 tiles at a time
*/
public void addTile(Tile t) {
if (numberOfTiles == playerTiles.length)
{
System.out.println("You can not have more than 15 tiles");
}
else if (numberOfTiles <= playerTiles.length-1)
{
playerTiles[numberOfTiles] = t ;
numberOfTiles ++ ;
}
}
/*
* TODO: uses bubble sort to sort playerTiles in increasing color and value
* value order: 1 < 2 < ... < 12 < 13
* color order: Y < B < R < K
* color is more important in this ordering, a sorted example:
* 3Y 3Y 6Y 7Y 1B 2B 3B 3B 10R 11R 12R 2K 4K 5K
* you can use compareToColorFirst method in Tile class for comparing
* you are allowed to use Collections.sort method
*/
public void sortTilesColorFirst() {
for (int i = 0 ; i < numberOfTiles - 1; i++)
{
for (int j = 0 ; j < numberOfTiles - 1 - i ; j++)
{
if (playerTiles[j].compareToColorFirst(playerTiles[j+1]) > 0)
{
Tile temp = playerTiles[j] ;
playerTiles[j] = playerTiles[j+1] ;
playerTiles[j+1] = temp ;
}
}
}
}
/*
* TODO: uses bubble sort to sort playerTiles in increasing value and color
* value order: 1 < 2 < ... < 12 < 13
* color order: Y < B < R < K
* value is more important in this ordering, a sorted example:
* 1B 2B 2K 3Y 3Y 3B 3B 4K 5K 6Y 7Y 10R 11R 12R
* you can use compareToValueFirst method in Tile class for comparing
* you are allowed to use Collections.sort method
*/
public void sortTilesValueFirst() {
for (int i = 0 ; i < numberOfTiles - 1; i++)
{
for (int j = 0 ; j < numberOfTiles - i - 1 ; j++)
{
if (playerTiles[j].compareToValueFirst(playerTiles[j+1]) > 0)
{
Tile temp = playerTiles[j] ;
playerTiles[j] = playerTiles[j+1] ;
playerTiles[j+1] = temp ;
}
}
}
}
public int findPositionOfTile(Tile t) {
int tilePosition = -1;
for (int i = 0; i < numberOfTiles; i++) {
if(playerTiles[i].matchingTiles(t)) {
tilePosition = i;
}
}
return tilePosition;
}
public void displayTiles() {
System.out.println(playerName + "'s Tiles:");
for (int i = 0; i < numberOfTiles; i++) {
System.out.print(playerTiles[i].toString() + " ");
}
System.out.println();
}
public Tile[] getTiles() {
return playerTiles;
}
public void setName(String name) {
playerName = name;
}
public String getName() {
return playerName;
}
public int getNumTileMinus(){
numberOfTiles--;
return numberOfTiles;
}
}