summaryrefslogtreecommitdiff
path: root/src/tetris/tests/board/TetrisBoardTest.java
blob: d14f13b036abf221e18203dbd4b36cf07d6e415c (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
/*
 * Jesse Morgan <jesterpm@u.washington.edu>
 * 
 * TCSS 305 � Autumn 2009
 * Tetris Project
 * 17 November 2009
 */

package tetris.tests.board;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import tetris.board.TetrisBoard;
import tetris.model.IntPoint;
import tetris.piece.IPiece;
import tetris.piece.JPiece;
import tetris.piece.LPiece;
import tetris.piece.OPiece;
import tetris.piece.SPiece;
import tetris.piece.TPiece;
import tetris.piece.TetrisPiece;
import tetris.piece.ZPiece;

/**
 * Test class for the tetris board.
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 30 November 2009.
 */
public class TetrisBoardTest {
  /**
   * Width and Height of a Tetris piece.
   */
  private static final int TETRIS_PIECE_SIZE = 4;
  
  /**
   * 
   */
  private static final String TEST1_FILENAME =
    "tetris.tests.board.TetrisBoardTest.expected-output.txt";
  
  /**
   * The test pieces.
   */
  private static final TetrisPiece[] TEST1 = {
    new TPiece(0, 0),
    new OPiece(2, 0),
    new IPiece(5, 0).rotateLeft().translate(-1, 0),
    new OPiece(5, 0),
    
    new IPiece(0, 0).rotateLeft().translate(-1, 0),
    new ZPiece(2, 0).rotateRight(),
    
    new OPiece(7, 0),
    
    // Bottom two lines have disappeared now.
    
    new OPiece(0, 0),
    new JPiece(1, 0).rotateLeft(),
    new IPiece(6, 0),
    new LPiece(4, 0).rotateRight(),
    new SPiece(7, 0),
    new TPiece(6, 0).rotateLeft().rotateLeft(),
    
    // Third (5th) row has now disappeared.
    
    new OPiece(3, 0),
    new SPiece(6, 0),
    new LPiece(1, 0),
    new TPiece(4, 0),
    new LPiece(4, 0).rotateLeft().rotateLeft(),
    new ZPiece(2, 0).rotateLeft(),
    new JPiece(0, 0).rotateRight(),
   
    new IPiece(6, 0).rotateLeft().translate(-1, 0),
    new LPiece(7, 0).rotateLeft().rotateLeft(),
    new JPiece(7, 0).rotateRight(),
    
    new OPiece(-1, 0), // 1 square buffer on the either side
    new JPiece(1, 0).rotateLeft(),
    new IPiece(3, 0),
    new IPiece(7, 0).rotateLeft().translate(-1, 0),
    new TPiece(7, 0).rotateLeft().rotateLeft(),
    new SPiece(5, 0),
    new ZPiece(5, 0).rotateLeft(),
    
    new TPiece(4, 0),
    new LPiece(4, 0).rotateLeft(),
    new LPiece(4, 0)
  };
  
  /**
   * The expected output of test 1.
   */
  private static String my_test1_output;
  
  /**
   * This method loads the expected output into a file.
   */
  @BeforeClass
  public static void loadExpectedOutput() {
    try {
      final BufferedReader reader =
          new BufferedReader(new FileReader(TEST1_FILENAME));
  
      final StringBuilder sb = new StringBuilder();
      String line;
  
      try {
        line = reader.readLine();
        while (line != null) {
          sb.append(line);
          sb.append("\n");
    
          line = reader.readLine();
        }
    
        my_test1_output = sb.toString();
        
        reader.close();
        
      } catch (final IOException the_exception) {
        fail("Could not load file: " + the_exception.getMessage());
      }
    
    } catch (final FileNotFoundException the_exception) {
      fail("Could not find file: " + the_exception.getMessage());
    }
    
  }
  
  
  /**
   * JUnit test to check that the board functions as expected.
   */
  @Test
  public void testTheBoard() {
    final TetrisBoard tb = new TetrisBoard(TetrisBoard.STANDARD_BOARD_WIDTH,
                                     TetrisBoard.STANDARD_BOARD_HEIGHT,
                                     Arrays.asList(TEST1));
    
    tb.newGame();
    
    // Loop through game play
    final int moves_per_piece = TetrisBoard.STANDARD_BOARD_HEIGHT * TETRIS_PIECE_SIZE;
    
    for (int i = 0; i <  TEST1.length * moves_per_piece && !tb.isGameOver(); i++) {
      tb.progressBoard();
    }

    
    assertEquals("Tetris board output not as expected.", my_test1_output, tb.toString());
  }
  
  /**
   * JUnit test for the moving functions.
   */
  @Test
  public void testMoves() {
    final List<TetrisPiece> pieces = new ArrayList<TetrisPiece>();
    
    pieces.add(new LPiece(-1, 0));
    pieces.add(new LPiece(-1, 0));
    
    final TetrisBoard tb = new TetrisBoard(TetrisBoard.STANDARD_BOARD_WIDTH,
                                           TetrisBoard.STANDARD_BOARD_HEIGHT,
                                           pieces);
    tb.newGame();
    
    final IntPoint[] points =
    {new IntPoint(0, 0), new IntPoint(0, 1), new IntPoint(0, 2), new IntPoint(1, 2)};
    
    // Test inital
    checkPoints(new IntPoint(0, 0), points, tb.getTetrisBlocks());
    
    // Test Right
    tb.moveRight();
    checkPoints(new IntPoint(1, 0), points, tb.getTetrisBlocks());

    // Test Left
    tb.moveLeft();
    checkPoints(new IntPoint(1 - 1, 0), points, tb.getTetrisBlocks());
    
    // Test Down
    tb.moveDown();
    checkPoints(new IntPoint(0, 1), points, tb.getTetrisBlocks());
  }
  
  /**
   * Checks that an array of expected points match a List actual points.
   * @param the_modifier A point to modify the other points by.
   * @param the_expected The expected points.
   * @param the_actual The actual points.  
   */
  private void checkPoints(final IntPoint the_modifier,
                           final IntPoint[] the_expected, final List<IntPoint> the_actual) {
    
    assertEquals("Extraneous points on the board.", the_expected.length, the_actual.size());
    
    for (int i = 0; i < the_expected.length; i++) {
      final IntPoint modded = new IntPoint(the_expected[i].getX() + the_modifier.getX(),
                                     the_expected[i].getY() + the_modifier.getY());
      
      assertTrue("Missing a point.", the_actual.contains(modded));
    }
  }
}