-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGemManager.java
324 lines (279 loc) · 11.6 KB
/
GemManager.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
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
/*************************************/
/* Tyler Esterly */
/* CS251 */
/* This class describes a GemManager */
/* object, which essentially keeps */
/* track of the various bookkeeping */
/* required for a class "Bejewled" */
/* style game */
/*************************************/
import java.awt.Color;
import java.util.*;
public class GemManager {
Random randGem = new Random();
//Boolean that tells checkMatch() not to clear and cascade gems
//at the beginning of the game, and instead jsut replaces them
//with a non-match.
boolean startGame;
int rows, columns, numGems, score, gemRemoval, clearedIndex;
// 2D array that represents the gameBoard
Gem[][] gameBoard;
/*arrays that keep track of the horizontal and vertical
* coordinates that are being removes (mostly used to fit
* Brooke's formatting for the GemManagerTest)*/
int[] tempHorizontalCoords = new int[100];
int[] tempVerticalCoords = new int[100];
/*Arrays that keep track of the coordinates of gems
* that need to be cleared and the ones that
* need to be cascaded downward*/
int[] clearedCoords = new int[500];
// List containing all possible gems
LinkedList<Gem> gemSet = new LinkedList<>();
LinkedList<Gem> playSet = new LinkedList<>();
/* List that holds Lists that contain the x and y coordinates
* of all the areas that might contain a match*/
LinkedList <LinkedList<Integer> > matchCoords = new LinkedList<>();
public GemManager(int columns, int rows){
score = 0;
this.rows = rows;
this.columns = columns;
gameBoard = new Gem[rows][columns];
/*TODO
make more gems and have them added to the playlist as the game goes on*/
Gem redGem = new Gem('@', Color.red);
Gem orangeGem = new Gem('.', Color.orange);
Gem pinkGem = new Gem('!', Color.PINK);
Gem cyanGem = new Gem('*', Color.cyan);
Gem whiteGem = new Gem(',', Color.white);
Gem greenGem = new Gem('?', Color.green);
Gem magentaGem = new Gem('O', Color.MAGENTA);
numGems = 4;
fillBoard();
}
/*Class that contains the character representation and color of a gem
* TODO
* if I have time to add special gems with special powers this class might
* act as a parent class for them*/
public class Gem {
char gemChar;
Color gemColor;
public Gem(char character, Color color){
gemChar = character;
gemColor = color;
playSet.add(this);
}
}
/*Swaps two gems, adds their coordinate to the list of coordinates to
* check for matches, and then actually checks for a match*/
void swapGems(int rowOne, int columnOne, int rowTwo, int columnTwo){
int[] gemCoords = new int[]{rowOne, columnOne, rowTwo,columnTwo};
// put the two coordinates in a list and add them to the list of coordinates
// to check for matches
LinkedList<Integer> matchCoordsOne = new LinkedList<>();
LinkedList<Integer> matchCoordsTwo = new LinkedList<>();
matchCoordsOne.add(rowOne);
matchCoordsOne.add(columnOne);
matchCoordsTwo.add(rowTwo);
matchCoordsTwo.add(columnTwo);
//actually swap the two gems now
if (isLegal(gemCoords)) {
Gem tempGem = gameBoard[rowOne][columnOne];
gameBoard[rowOne][columnOne] = gameBoard[rowTwo][columnTwo];
gameBoard[rowTwo][columnTwo] = tempGem;
matchCoords.add(matchCoordsOne);
matchCoords.add(matchCoordsTwo);
}
}
/*isLegal was meant to be used to make sure a swap was legal, but for the purposes
* of testing the manager it was never used so I commented it out.*/
boolean isLegal(int[] gemCoords){
//Makes sure the two rows are within one square of eachother
if ((gemCoords[0] - gemCoords[2]) == 1 || (gemCoords[0] - gemCoords[2]) == -1 ||
( gemCoords[0] - gemCoords[2]) == 0){
if ((gemCoords[1] - gemCoords[3]) == 1 || (gemCoords[1] - gemCoords[3]) == -1 ||
(gemCoords[1] - gemCoords[3]) == 0){
return true;
}
}
//Makes sure the two collumns are within one square of eachother
return false;
}
/*Fills board with gems and makes sure it doesn't automatically generate a match*/
void fillBoard() {
//checkMatch() will know not to clear and cascade gems if it
//detects a match at the start of the game
startGame = true;
int gemNum;
int i = 0;
int j = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
LinkedList<Integer> checkCoords = new LinkedList<>();
checkCoords.add(0,i);
checkCoords.add(1,j);
matchCoords.add(checkCoords);
gemNum = randGem.nextInt(numGems);
gameBoard[i][j] = playSet.get(gemNum);
while (checkMatch()) {
gemNum = randGem.nextInt(numGems);
gameBoard[i][j] = playSet.get(gemNum);
}
}
}
startGame = false;
}
public String toString(){
String boardString = "";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if (gameBoard[i][j] == null){
boardString += " ";
}
else
boardString += (gameBoard[i][j].gemChar);
}
boardString += "\n";
}
return boardString;
}
/*Method loops through the master list of coordinates
* of possible matches and checks to see if a match exists.*/
/*TODO
* possibly make it illegal to make a swap that does not result in a match*/
boolean checkMatch() {
boolean matchFlag = false;
int x;
int y;
int tempHorizontalCoordsIndex = 0;
int tempVerticalCoordsIndex = 0;
clearedIndex = 0;
gemRemoval = 0;
LinkedList<LinkedList<Integer>> tempMatchCoords = (LinkedList) matchCoords.clone();
Arrays.fill(tempHorizontalCoords, -1);
Arrays.fill(tempVerticalCoords, -1);
Arrays.fill(clearedCoords, -1);
for (LinkedList<Integer> coordList: tempMatchCoords ){
int horizontalCount = 0;
int verticalCount = 0;
x = coordList.get(1);
y = coordList.get(0);
// Check to the right of the swapped gem
for (int i = x; i < columns; i++){
if (gameBoard[y][i] != null &&
(gameBoard[y][i].gemColor.equals(gameBoard[y][x].gemColor)) ){
horizontalCount++;
tempHorizontalCoords[tempHorizontalCoordsIndex] = i;
tempHorizontalCoords[tempHorizontalCoordsIndex + 1] = y;
tempHorizontalCoordsIndex += 2;
}
else break;
}
// Check to the left
for (int i = (x - 1); i >= 0; i--){
if (gameBoard[y][i] != null &&
(gameBoard[y][i].gemColor.equals(gameBoard[y][x].gemColor))){
horizontalCount++;
tempHorizontalCoords[tempHorizontalCoordsIndex] = i;
tempHorizontalCoords[tempHorizontalCoordsIndex + 1] = y;
tempHorizontalCoordsIndex += 2;
}
else{
break;
}
}
// Check downwards vertically
for (int i = (y); i < rows; i++){
if (gameBoard[i][x] != null &&
(gameBoard[i][x].gemColor.equals(gameBoard[y][x].gemColor)) ){
verticalCount++;
tempVerticalCoords[tempVerticalCoordsIndex] = x;
tempVerticalCoords[tempVerticalCoordsIndex + 1] = i;
tempVerticalCoordsIndex += 2;
}
else break;
}
//check upwards;
for (int i = (y - 1); i >= 0; i--){
if (gameBoard[i][x] != null &&
gameBoard[i][x].gemColor.equals(gameBoard[y][x].gemColor)){
verticalCount++;
tempVerticalCoords[tempVerticalCoordsIndex] = x;
tempVerticalCoords[tempVerticalCoordsIndex + 1] = i;
tempVerticalCoordsIndex += 2;
}
else break;
}
// Check if there was a horizontal match
if (horizontalCount >= 3){
// if we are building the starting board, we don't need to clear matches
if (!startGame) {
for (int i = 0; tempHorizontalCoords[i] != -1; i++){
clearedCoords[clearedIndex] = tempHorizontalCoords[i];
clearedIndex++;
}
}
gemRemoval += horizontalCount;
matchFlag = true;
}
// Check if there was a vertical match
else if (verticalCount >= 3){
// if we are building the starting board, we don't need to clear matches
if (!startGame) {
for (int i = 0; tempVerticalCoords[i] != -1; i++){
clearedCoords[clearedIndex] = tempVerticalCoords[i];
clearedIndex++;
}
}
gemRemoval += verticalCount;
matchFlag = true;
}
// There was no match, remove these coordinates from the master list.
else
matchCoords.remove(coordList);
//Reset bookkeeping variables for the next coordinates
tempHorizontalCoordsIndex = 0;
tempVerticalCoordsIndex = 0;
Arrays.fill(tempHorizontalCoords, -1);
Arrays.fill(tempVerticalCoords, -1);
}
return matchFlag;
}
/*Clears matched gems and cascades gems downwards.
* Returns: Total gems cleared in this specific match*/
void clear() {
// Clear the matched gems
for (int i = 0; clearedCoords[i] != -1; i += 2) {
int clearX = clearedCoords[i];
int clearY = clearedCoords[i + 1];
gameBoard[clearY][clearX] = null;
score++;
}
}
void cascade () {
int pullDownCoord;
for (int i = (columns - 1); i >= 0; i--){
for (int j = (rows - 1); j >= 0; j--){
if (gameBoard[j][i] == null){
pullDownCoord = j;
while (gameBoard[pullDownCoord][i] == null && pullDownCoord > 0){
pullDownCoord--;
}
if (pullDownCoord == 0 && gameBoard[pullDownCoord][i] == null){
int gemNum = randGem.nextInt(numGems);
gameBoard[j][i] = playSet.get(gemNum);
}
else {
gameBoard[j][i] = gameBoard[pullDownCoord][i];
gameBoard[pullDownCoord][i] = null;
}
LinkedList<Integer> newCoords = new LinkedList<>();
newCoords.add(j);
newCoords.add(i);
LinkedList<Integer> tempNewCoords = (LinkedList) newCoords.clone();
matchCoords.add(tempNewCoords);
newCoords.clear();
}
}
}
}
}