summaryrefslogtreecommitdiff
path: root/src/tetris/board/TetrisBoard.java
blob: d662b3836fbee3beffeed2c8ca4702637c0042d4 (plain)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 * Jesse Morgan <jesterpm@u.washington.edu>
 * 
 * TCSS 305 - Autumn 2009
 * Tetris Project
 * 17 November 2009
 */

package tetris.board;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;

import tetris.model.IntPoint;
import tetris.piece.TetrisPiece;
import tetris.piece.TetrisPieces;

/**
 * Class to represent the tetris board.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 23 November 2009
 */
public class TetrisBoard extends Observable {
  // Public Constants
  /**
   * Standard tetris board width.
   */
  public static final int STANDARD_BOARD_WIDTH = 10;

  /**
   * Standard tetris board height.
   */
  public static final int STANDARD_BOARD_HEIGHT = 20;
  
  /**
   * Size of the above board buffer.
   */
  public static final int NEW_PIECE_BUFFER = TetrisPiece.TETRIS_PIECE_SIZE;

  // Private Constants
  /**
   * Initial progression speed in milliseconds.
   */
  private static final int GAME_SPEED = 1000;
  
  /**
   * Speed change in ms per level.
   */
  private static final int DSPEED_PER_LEVEL = 100;
  
  /**
   * Number of points per line clear.
   */
  private static final int POINTS_PER_LINE = 100;
  
  /**
   * Number of points per line that the piece was hard dropped.
   */
  private static final int POINTS_PER_DROPPED_LINE = 10;
  
  /**
   * Points per level.
   */
  private static final int POINTS_PER_LEVEL = 2000;
  
  /**
   * Y position to start new pieces.
   */
  private static final int INITIAL_Y_COORDINATE = 0;

  // Private Fields
  /**
   * Flag to indicate the game is over.
   */
  private boolean my_game_over;
  
  /**
   * Flag to store if a game is paused.
   */
  private boolean my_game_paused;
  
  /**
   * Current game score.
   */
  private int my_score;

  /**
   * Storage for the board.
   */
  private final List<List<Boolean>> my_board;

  /**
   * Board width.
   */
  private final int my_board_width;

  /**
   * Board height.
   */
  private final int my_board_height;

  /**
   * Future Tetris Pieces.
   */
  private List<TetrisPiece> my_future_pieces;
  
  /**
   * Testing Pieces.
   */
  private List<TetrisPiece> my_testing_pieces;

  /**
   * Current Piece.
   */
  private TetrisPiece my_current_piece;

  /**
   * Create a new tetris board.
   * 
   * @param the_width Board width.
   * @param the_height Board height.
   */
  public TetrisBoard(final int the_width, final int the_height) {
    super();

    // Mark the game as over, since we haven't called newGame() yet
    my_game_over = true;
    my_game_paused = false;
    my_score = 0;

    // Setup the board.
    my_board = new ArrayList<List<Boolean>>();
    my_board_width = the_width;
    my_board_height = the_height;

    final int center_pos = my_board_width / 2 - TetrisPiece.TETRIS_PIECE_SIZE / 2;
    
    // Set a current piece just to keep my_current_piece from being null.
    my_current_piece = TetrisPieces.getRandomPiece(center_pos, INITIAL_Y_COORDINATE);

    // And setup the future piece list.
    my_future_pieces = new ArrayList<TetrisPiece>();
    my_testing_pieces = new ArrayList<TetrisPiece>();
  }

  /**
   * Create a tetris board with from a set of pieces.
   * 
   * @param the_width Board width.
   * @param the_height Board height.
   * @param the_pieces List of pieces to place on board. the_pieces.size() > 1
   */
  public TetrisBoard(final int the_width, final int the_height,
                     final List<TetrisPiece> the_pieces) {
    this(the_width, the_height);

    // Set the current piece
    my_current_piece = the_pieces.get(0);

    // Set the testing pieces.
    my_testing_pieces.addAll(the_pieces);
  }

  /**
   * @return true if the game is over.
   */
  public boolean isGameOver() {
    return my_game_over;
  }
  
  /**
   * @param the_state True if the game is to be paused, false otherwise.
   */
  public void setPaused(final boolean the_state) {
    if (!my_game_over && my_game_paused != the_state) {
      my_game_paused = the_state;
    
      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.PAUSE_CHANGED));
    }
  }
  
  /**
   * @return True if the game is paused, false otherwise.
   */
  public boolean isPaused() {
    return my_game_paused;
  }
  
  /**
   * @return the current score.
   */
  public int getScore() {
    return my_score;
  }
  
  /**
   * @return the current level.
   */
  public int getLevel() {
    return getScore() / POINTS_PER_LEVEL + 1;
  }
  
  /**
   * @return the game speed in ms based off the level.
   */
  public int getSpeed() {
    return Math.max(GAME_SPEED - DSPEED_PER_LEVEL * (getLevel() - 1), DSPEED_PER_LEVEL);
  }
  
  /**
   * Reset the board to the new game state.
   */
  public void newGame() {
    // The game is not over
    my_game_over = false;
    my_game_paused = false;
    my_score = 0;
    
    // Clear the board and recreate it
    my_board.clear();
    for (int y = 0; y < my_board_height + NEW_PIECE_BUFFER; y++) {
      my_board.add(generateTetrisRow());
    }

    final int center_pos = my_board_width / 2 - TetrisPiece.TETRIS_PIECE_SIZE / 2;
    
    // Setup the future pieces
    my_future_pieces.clear();
    my_future_pieces.addAll(my_testing_pieces);
    
    // If we have none...
    if (my_future_pieces.isEmpty()) {
      // Piece the current piece.
      my_current_piece = TetrisPieces.getRandomPiece(center_pos, INITIAL_Y_COORDINATE);
      
      // And the next piece. 
      my_future_pieces.add(TetrisPieces.getRandomPiece(center_pos, INITIAL_Y_COORDINATE));
    
    } else {
      // Set the current piece to the first one...
      my_current_piece = my_future_pieces.get(0);
      my_future_pieces.remove(1);
    }
    
    // Something changed, if you couldn't tell....
    setChanged();
    notifyObservers(new TetrisBoardEvent(EventTypes.NEW_GAME));
  }

  /**
   * Progress the board.
   */
  public void progressBoard() {
    boolean freeze = false;

    // If we're not playing, don't move.
    if (my_game_over || my_game_paused) {
      return;
    }

    // Move the current piece down one.
    final TetrisPiece new_piece = my_current_piece.translate(0, 1);

    if (validatePosition(new_piece)) {
      // If it's a valid move, do it.
      my_current_piece = new_piece;

    } else {
      // Otherwise, we'll freeze the piece here shortly.
      freeze = true;
    }

    // Do we need to freeze the current piece?
    if (freeze) {
      // Check for end of game conditions
      if (my_current_piece.getY() < INITIAL_Y_COORDINATE + NEW_PIECE_BUFFER) {
        // If the piece is even partially above the board, end the game.
        my_game_over = true;
        setChanged();
        notifyObservers(new TetrisBoardEvent(EventTypes.GAME_OVER));
        
        // Game over... nothing else to do here.
        return;
      }

      // Save the starting row of the current piece.
      final int starting_row = my_current_piece.getY();

      // Freeze the current piece.
      freezeCurrentPiece();

      // Check for filled lines
      checkLines(starting_row);
    }

    // Notify the observers
    setChanged();
    notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
  }

  /**
   * Move the current piece left.
   */
  public void moveLeft() {
    final TetrisPiece new_piece = my_current_piece.translate(-1, 0);

    if (!my_game_paused && !my_game_over && validatePosition(new_piece)) {
      my_current_piece = new_piece;

      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
    }
  }

  /**
   * Move the current piece right.
   */
  public void moveRight() {
    final TetrisPiece new_piece = my_current_piece.translate(1, 0);

    if (!my_game_paused && !my_game_over && validatePosition(new_piece)) {
      my_current_piece = new_piece;

      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
    }
  }

  /**
   * Move the current piece down.
   */
  public void moveDown() {
    final TetrisPiece new_piece = my_current_piece.translate(0, 1);

    if (!my_game_paused && !my_game_over && validatePosition(new_piece)) {
      my_current_piece = new_piece;

      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
    }
  }
  
  /**
   * Drop the piece.
   */
  public void dropPiece() {
    TetrisPiece new_piece = my_current_piece;
    int number_of_moves = 0;
    
    // Don't drop if the game is paused
    if (my_game_paused || my_game_over) {
      return;
    }
    
    // Move the piece until it can't move no more.
    do {
      new_piece = new_piece.translate(0, 1);
      number_of_moves++;
      
    } while (validatePosition(new_piece));
    
    // That last move didn't work out so well, remove it.
    number_of_moves = number_of_moves - 1;
    
    // If the piece could move, move it.
    if (number_of_moves > 0) {
      // Update the piece
      my_current_piece = my_current_piece.translate(0, number_of_moves);
      
      // Update the score based off the number of lines dropped
      incrementScore(number_of_moves * POINTS_PER_DROPPED_LINE);
      
      // Tell the world
      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
      
    }
  }

  /**
   * Rotate the current piece left.
   */
  public void rotateLeft() {
    final TetrisPiece new_piece = my_current_piece.rotateLeft();

    if (!my_game_paused && !my_game_over && validatePosition(new_piece)) {
      my_current_piece = new_piece;

      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
    }
  }

  /**
   * Rotate the current piece right.
   */
  public void rotateRight() {
    final TetrisPiece new_piece = my_current_piece.rotateRight();

    if (!my_game_paused && !my_game_over && validatePosition(new_piece)) {
      my_current_piece = new_piece;

      setChanged();
      notifyObservers(new TetrisBoardEvent(EventTypes.CURRENT_PIECE_UPDATE));
    }
  }

  /**
   * @return the curent tetris piece.
   */
  public TetrisPiece getCurrentPiece() {
    return my_current_piece;
  }

  /**
   * @return the next tetris piece.
   */
  public TetrisPiece getNextPiece() {
    return my_future_pieces.get(0);
  }

  /**
   * @return An array of Point2Ds for each tetris block;
   */
  public List<IntPoint> getTetrisBlocks() {
    final List<IntPoint> points = new ArrayList<IntPoint>();

    // Add the current piece
    final IntPoint[] block_points = my_current_piece.getBoardCoordinates();

    points.addAll(Arrays.asList(block_points));

    // Add the rest of the board pieces.
    for (int y = 0; y < my_board.size(); y++) {
      final List<Boolean> row = my_board.get(y);

      for (int x = 0; x < row.size(); x++) {
        if (row.get(x).booleanValue()) {
          points.add(new IntPoint(x, y));
        }
      }
    }

    return points;
  }

  /**
   * {@inheritDoc}
   */
  public String toString() {
    final StringBuilder sb = new StringBuilder();

    // Iterate through all the rows
    for (int y = 0; y < my_board_height + NEW_PIECE_BUFFER; y++) {
      // Iterate through each column
      for (int x = 0; x < my_board_width; x++) {

        if (my_board.get(y).get(x).booleanValue()) {
          sb.append('X');

        } else {
          sb.append('.');
        }
      }

      // Add a new line at the end of each row.
      sb.append('\n');
    }

    // Add the current piece
    final IntPoint[] points = my_current_piece.getBoardCoordinates();

    for (int i = 0; i < points.length; i++) {
      final int pos = (my_board_width + 1) * points[i].getY() + points[i].getX();
      sb.replace(pos, pos + 1, "O");
    }

    // Now we add a dashed line after the 4th row
    final StringBuilder dashes = new StringBuilder();
    for (int x = 0; x < my_board_width; x++) {
      dashes.append('-');
    }
    dashes.append('\n');

    sb.insert((my_board_width + 1) * NEW_PIECE_BUFFER, dashes);

    // Is the game over?
    if (my_game_over) {
      sb.append("Game Over\n");
    }

    return sb.toString();
  }
  
  /**
   * Increment the tetris score.
   * 
   * @param the_points to add to the score.
   */
  private void incrementScore(final int the_points) { 
    if (getLevel() > GAME_SPEED / DSPEED_PER_LEVEL) {
      /*
       * Multipler: For any level above the one which results in speed 1,
       * multiply the points to add by the number of levels above that
       * threshold.
       */
      final int multiplier = getLevel() - GAME_SPEED / DSPEED_PER_LEVEL + 1;
      
      my_score += the_points * multiplier;
      
    } else {
      my_score += the_points;
    }
    
    setChanged();
    notifyObservers(new TetrisBoardEvent(EventTypes.SCORE_CHANGED));
  }

  /**
   * Creates a row for the tetris piece list.
   * 
   * @return a list containing an empty row.
   */
  private List<Boolean> generateTetrisRow() {
    final List<Boolean> row = new ArrayList<Boolean>();

    // Loop through the columns.
    for (int x = 0; x < my_board_width; x++) {
      row.add(Boolean.FALSE);
    }

    return row;
  }

  /**
   * Checks if a piece can actually occupy the space it's trying to occupy.
   * 
   * @param the_piece The piece in question.
   * @return true if it's cool, false if it's not.
   */
  private boolean validatePosition(final TetrisPiece the_piece) {
    final IntPoint[] points = the_piece.getBoardCoordinates();

    for (int i = 0; i < points.length; i++) {
      // Check that it's on the board
      if (points[i].getX() < 0 || points[i].getX() >= my_board_width || points[i].getY() < 0 ||
          points[i].getY() >= my_board_height + NEW_PIECE_BUFFER) {
        return false;
      }

      // Check that it doesn't hit another piece.
      if (my_board.get(points[i].getY()).get(points[i].getX()).booleanValue()) {
        return false;
      }
    }

    // Each point passed all tests.
    return true;
  }

  /**
   * Freeze the current piece and set a new one.
   */
  private void freezeCurrentPiece() {
    final IntPoint[] points = my_current_piece.getBoardCoordinates();

    for (int i = 0; i < points.length; i++) {
      my_board.get(points[i].getY()).set(points[i].getX(), Boolean.TRUE);
    }

    // Get a new piece.
    my_current_piece = my_future_pieces.get(0);
    my_future_pieces.remove(0);
    if (my_future_pieces.isEmpty()) {
      final int xpos = (int) (my_board_width / 2 - TetrisPiece.TETRIS_PIECE_SIZE / 2);
      my_future_pieces.add(TetrisPieces.getRandomPiece(xpos, INITIAL_Y_COORDINATE));
    }
  }

  /**
   * Check the TETRIS_PIECE_SIZE lines after a certain row.
   * 
   * @param the_starting_row The row to start at.
   */
  private void checkLines(final int the_starting_row) {
    int last_row;
    int lines_cleared = 0;

    if (the_starting_row + TetrisPiece.TETRIS_PIECE_SIZE > my_board.size()) {
      last_row = my_board.size();

    } else {
      last_row = the_starting_row + TetrisPiece.TETRIS_PIECE_SIZE;
    }

    for (int y = the_starting_row; y < last_row; y++) {
      if (!my_board.get(y).contains(Boolean.FALSE)) {
        my_board.remove(y);

        // Add a new row to the top, after the buffer space.
        my_board.add(TetrisPiece.TETRIS_PIECE_SIZE, generateTetrisRow());

        // Increment lines cleared
        lines_cleared++;
        
        setChanged();
      }
    }
    
    // Notify about the cleared lines (has to come before incrementScore)
    notifyObservers(new TetrisBoardEvent(EventTypes.LINES_CLEARED));
    
    // Update Score
    incrementScore(lines_cleared * POINTS_PER_LINE * lines_cleared);
  }
}