-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
421 lines (419 loc) · 12.8 KB
/
Board.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
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.awt.Point;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.NotificationFilterSupport;
public class Board extends JPanel implements NotificationListener, KeyListener{
//216x468, each space is 36x36
/*to do:
* create contiguous blocks
* implement countergemset drops
* make block attacks exponential and not linear, make combos worth more
* draw message images (win, lose), implement menus
* implement and display scoring (combos = more points, as do larger blocks)
* sometimes rotation makes block disappear
*/
public static final int BLOCK_VALUE=10, PIXELS_DOWN=4, BLOCK_SIZE=32, NUM_ROWS=14, NUM_COLS=6;
public static final double BLOCK_DROP_NUM=1.0, BLOCK_COUNTER_NUM=.8, DEFAULT_FACTOR=1.0, COUNTERGEM_FACTOR=.67, DIAMOND_FACTOR=.75;
public static OverallInterface OI;
private Block[][] board;
private int score, listIndex;
private double numBlocks, tempNumBlocks;
private Point offset;
private BlockList list;
private NotificationFilterSupport filter;
private BlockPair currentPair;
private Board otherBoard;
private Diamond curDiamond;
private CounterGemSet counterSet;
private int[] legalKeys;
private boolean waiting, beganDrop, duringDrop;
private BlinkText text;
private String currentFilter;
public Board(Point cornerOffset, int[] keys){
board=new Block[NUM_ROWS][NUM_COLS];
offset=cornerOffset;
legalKeys=keys;
}
public void setList(BlockList b){
list=b;
}
public void setOtherBoard(Board b){
otherBoard=b;
}
public void setFilter(NotificationFilterSupport f){
filter=f;
}
public void setBlinkText(BlinkText b){
text=b;
}
public void setCounter(CounterGemSet c){
counterSet=c;
}
//accessors
public static Point pixelsToPoint(int x, int y, Point off){
Point p=new Point((int)(x-off.getX())/BLOCK_SIZE, (int)(y-off.getY())/BLOCK_SIZE);
if((x-off.getX())%BLOCK_SIZE!=0)
p.setLocation((int)p.getX()+1, (int)p.getY());
if((y-off.getY())%BLOCK_SIZE!=0)
p.setLocation((int)p.getX(), (int)p.getY()+1);
return p;
}
public static int pixelsToCoord(int px, int offset){
int coord=(px-offset)/BLOCK_SIZE;
if((px-offset)%BLOCK_SIZE!=0)
return coord+1;
return coord;
}
private static int[] randSeq(int length){
//returns a random sequence, where every integer 0<=i<length appears exactly once in random order
ArrayList<Integer> temp=new ArrayList<Integer>(length);
int[] result=new int[length];
Random r=new Random();
for(int i=0; i<length; ++i)
temp.add(i);
int size=temp.size(), i=0;
while(size>0){
result[i]=temp.remove(r.nextInt(size));
++i;
size=temp.size();
}
return result;
}
private static Block[] toArray(ArrayList<Block> blocks){
Block[] newArray=new Block[blocks.size()];
for(int i=0; i<newArray.length; ++i)
newArray[i]=blocks.get(i);
return newArray;
}
public void handleNotification(Notification n, Object o){
if(currentPair!=null && !moveDownPair()){
//if the pair has landed...
waiting=true;
currentPair=null;
decrementAll();
toggleFilter("fastest");
beganDrop=false;
}
if(currentPair!=null)
return;
if(beganDrop)
dropBlocks();
if(!moveDown()){
createBigGems();
if(!destroyBlocks()){
if(!beganDrop){
/*beforehand, a set of blocks would drop, then the other player could destroy more and the player being attacked
had to wait as each set fell separately. with this if, extra sets fall as soon as they are sent by the other player*/
beganDrop=duringDrop=true;
if(dropBlocks())
return;
}
duringDrop=false;
//if all possible actions have been performed before the next pair...
if(board[1][3]!=null){
//and the game isnt over...
String s="Game over.";
if(OI.getBoard(0)==this)
s+=" Player two wins!";
else
s+=" Player one wins!";
OI.writeString(s, new Point(150, 300));
OI.t.stop();
stop();
otherBoard.stop();
text.hide();
otherBoard.getBlinkText().hide();
return;
}
//...then add the next pair
numBlocks=tempNumBlocks;
tempNumBlocks=0;
if(numBlocks>1){
text.findWarningLevel(this);
text.show();
}
waiting=false;
addNextPair();
toggleFilter("normal");
}
}
}
public void keyPressed(KeyEvent e){
keyTyped(e);
}
public void keyReleased(KeyEvent e){
if(!waiting && e.getKeyCode()==legalKeys[0])
toggleFilter("normal");
}
public void keyTyped(KeyEvent e){
if(waiting)
return;
//order of keys: down, left, right, upleft, upright
int key=e.getKeyCode();
if(key==legalKeys[0])
toggleFilter("fast");
else if(key==legalKeys[1])
currentPair.shift(-1);
else if(key==legalKeys[2])
currentPair.shift(1);
else if(key==legalKeys[3])
currentPair.rotate(true);
else if(key==legalKeys[4])
currentPair.rotate(false);
}
public BlockPair getCurrentPair(){
return currentPair;
}
public int getWidth(){
return board[0].length*BLOCK_SIZE;
}
public int getHeight(){
return board.length*BLOCK_SIZE;
}
public double getNumBlocks(){
return numBlocks;
}
public NotificationFilterSupport getFilter(){
return filter;
}
public Block getSpace(int row, int col){
return board[row][col];
}
public Block getSpace(Point p){
return getSpace((int)p.getX(), (int)p.getY());
}
public boolean spaceEmpty(int row, int col){
if(row>NUM_ROWS-1 || col>NUM_COLS-1 || row<0 || col<0)
return false;
return board[row][col]==null;
}
public boolean spaceEmpty(Point p){
return spaceEmpty((int)p.getX(), (int)p.getY());
}
public Point getOffset(){
return offset;
}
public int getIndex(){
return listIndex;
}
public void stop(){
waiting=true;
}
private boolean topRowClear(){
for(int i=0; i<NUM_COLS; ++i){
if(board[0][i]!=null || (board[1][i]!=null && board[1][i].getPixelsY()%BLOCK_SIZE!=0))
return false;
}
return true;
}
private ArrayList<Block> getNeighborsInternal(int row, int col){
ArrayList<Block> neighbors=new ArrayList<Block>(4);
if(row<NUM_ROWS-1 && !spaceEmpty(row+1, col))
neighbors.add(board[row+1][col]);
if(col<NUM_COLS-1 && !spaceEmpty(row, col+1))
neighbors.add(board[row][col+1]);
if(row>0 && !spaceEmpty(row-1, col))
neighbors.add(board[row-1][col]);
if(col>0 && !spaceEmpty(row, col-1))
neighbors.add(board[row][col-1]);
return neighbors;
}
public Block[] getNeighbors(int row, int col){
return toArray(getNeighborsInternal(row, col));
}
public Block[] getNeighborsSameColor(int row, int col){
if(spaceEmpty(row, col))
//instead of throwing a null pointer, which previously caused errors due to the interruption, it simply fails quietly
return new Block[] {};
ArrayList<Block> neighbors=getNeighborsInternal(row, col);
Color c=board[row][col].getColor();
for(Iterator i=neighbors.iterator(); i.hasNext();){
if(!(((Block) i.next()).getColor().equals(c)))
i.remove();
}
return toArray(neighbors);
}
public Block[] getLegalDestroyTargets(int row, int col){
if(spaceEmpty(row, col))
return new Block[] {};
ArrayList<Block> neighbors=getNeighborsInternal(row, col);
Block b=board[row][col], temp;
for(Iterator i=neighbors.iterator(); i.hasNext();){
temp=(Block) i.next();
if(!(temp.getColor().equals(b.getColor()) || temp instanceof CounterGem))
i.remove();
}
return toArray(neighbors);
}
public Block[] removeCounterGems(Block[] blocks){
ArrayList<Block> blockList=new ArrayList<Block>(blocks.length);
for(Block b : blocks){
if(!(b instanceof CounterGem))
blockList.add(b);
}
return toArray(blockList);
}
//mutators
public void addIndex(int toAdd){
listIndex+=toAdd;
}
public void addNumBlocks(double toAdd){
if(duringDrop)
tempNumBlocks+=toAdd;
else{
text.show();
numBlocks+=toAdd;
}
}
public void setSpace(int row, int col, Block b){
if(row>=0 && col>=0 && row<NUM_ROWS && col<NUM_COLS)
board[row][col]=b;
}
public void setSpace(Point p, Block b){
setSpace((int)p.getX(),(int)p.getY(),b);
}
private void addNextPair(){
currentPair=list.getNext(this);
if(currentPair.getPivot() instanceof Diamond)
curDiamond=(Diamond)currentPair.getPivot();
else if(currentPair.getOther() instanceof Diamond)
curDiamond=(Diamond)currentPair.getOther();
OI.nextPreviewPair(this);
}
private void decrementAll(){
Block temp;
for(int row=0; row<NUM_ROWS; ++row){
for(int col=0; col<NUM_COLS; ++col){
temp=board[row][col];
if(temp instanceof CounterGem)
((CounterGem)temp).decrement();
}
}
}
public void createBigGems(){
for(int row=0; row<NUM_ROWS; ++row){
for(int col=0; col<NUM_COLS; ++col){
if(board[row][col] instanceof Gem){
if(removeCounterGems(getNeighborsSameColor(row, col)).length>0)
((Gem)board[row][col]).setSize("large");
else
((Gem)board[row][col]).setSize("small");
}
}
}
}
public void toggleFilter(String prefix){
filter.disableAllTypes();
filter.enableType(prefix);
currentFilter=prefix;
}
public BlinkText getBlinkText(){
return text;
}
private boolean moveDownPair(){
Block p=currentPair.getPivot(), o=currentPair.getOther();
board[p.getRow()][p.getCol()]=board[o.getRow()][o.getCol()]=null;
boolean free=false;
if((p.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(p.getRow()+1, p.getCol())) && (o.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(o.getRow()+1, o.getCol()))){
p.shiftPixels(0, PIXELS_DOWN);
o.shiftPixels(0, PIXELS_DOWN);
}
board[p.getRow()][p.getCol()]=board[o.getRow()][o.getCol()]=null;
if((p.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(p.getRow()+1, p.getCol())) && (o.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(o.getRow()+1, o.getCol())))
free=true;
board[o.getRow()][o.getCol()]=o;
board[p.getRow()][p.getCol()]=p;
return free;
}
private boolean moveDown(){
boolean free=false;
Block temp;
for(int row=NUM_ROWS-1; row>=0; --row){
for(int col=0; col<NUM_COLS; ++col){
temp=board[row][col];
if(temp==null)
continue;
if(temp.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(temp.getRow()+1, temp.getCol())){
temp.shiftPixels(0, PIXELS_DOWN);
}
if(free)
continue;
if(temp.getPixelsY()%BLOCK_SIZE!=0 || spaceEmpty(temp.getRow()+1, temp.getCol()))
//same test: this checks if this block would change with another run of moveDown(), meaning it is freefloating
free=true;
}
}
return free;
}
private void removeBlock(Block b){
setSpace(b.getRow(), b.getCol(), null);
OI.removeBlock(b);
}
public void destroyBlock(Block b, double factor, int points){
//called by the block in question so the board knows to remove it
if(numBlocks>=1){
numBlocks-=BLOCK_COUNTER_NUM*factor;
if(numBlocks<1)
text.hide();
}
else
otherBoard.addNumBlocks(BLOCK_DROP_NUM*factor);
score+=points;
removeBlock(b);
}
public void destroyBlock(Block b, double factor){
destroyBlock(b, factor, BLOCK_VALUE);
}
private boolean destroyBlocks(){
//to be run ONLY after all blocks have settled (moveDown() returns false)
boolean destroyed=false;
Block temp;
if(curDiamond!=null){
curDiamond.destroyNeighbors();
destroyed=true;
curDiamond=null;
}
for(int row=0; row<NUM_ROWS; ++row){
for(int col=0; col<NUM_COLS; ++col){
temp=board[row][col];
if(temp instanceof CrashGem && ((CrashGem) temp).destroyNeighbors())
destroyed=true;
}
}
if(destroyed && otherBoard.getNumBlocks()>1){
otherBoard.getBlinkText().findWarningLevel(otherBoard);
otherBoard.getBlinkText().show();
}
return destroyed;
}
private boolean dropBlocks(){
CounterGem temp;
if(numBlocks>=1){
if(topRowClear()){
Random r=new Random();
int[] order=Board.randSeq(NUM_COLS);
for(int i=0; i<NUM_COLS && numBlocks-->=1;++i){
temp=new CounterGem(order[i]*BLOCK_SIZE, 0, BlockPair.allColors[r.nextInt(4)], CounterGem.DEFAULT_VALUE, this);
board[0][order[i]]=temp;
OI.addBlock(temp);
}
if(++numBlocks<1){//because the final test decrements it once more than the number of blocks dropped
text.hide();
numBlocks=0;
}
}
//blocks either dropped or were waiting to drop, meaning the creation of the blocks isnt done yet and moveDown should be called more
return true;
}
else
return false;
}
}