Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tetris;
- import static org.junit.Assert.*;
- import org.junit.*;
- public class BoardTest {
- Board b;
- Piece pyr1, pyr2, pyr3, pyr4, s, sRotated;
- Piece pieces[];
- // This shows how to build things in setUp() to re-use
- // across tests.
- // In this case, setUp() makes shapes,
- // and also a 3X6 board, with pyr placed at the bottom,
- // ready to be used by tests.
- @Before
- public void setUp() throws Exception {
- b = new Board(3, 6);
- pyr1 = new Piece(Piece.PYRAMID_STR);
- pyr2 = pyr1.computeNextRotation();
- pyr3 = pyr2.computeNextRotation();
- pyr4 = pyr3.computeNextRotation();
- s = new Piece(Piece.S1_STR);
- sRotated = s.computeNextRotation();
- pieces = Piece.getPieces();
- b.place(pyr1, 0, 0);
- }
- // Check the basic width/height/max after the one placement
- @Test
- public void testSample1() {
- assertEquals(1, b.getColumnHeight(0));
- assertEquals(2, b.getColumnHeight(1));
- assertEquals(2, b.getMaxHeight());
- assertEquals(3, b.getRowWidth(0));
- assertEquals(1, b.getRowWidth(1));
- assertEquals(0, b.getRowWidth(2));
- }
- // Place sRotated into the board, then check some measures
- @Test
- public void testSample2() {
- b.commit();
- int result = b.place(sRotated, 1, 1);
- assertEquals(Board.PLACE_OK, result);
- assertEquals(1, b.getColumnHeight(0));
- assertEquals(4, b.getColumnHeight(1));
- assertEquals(3, b.getColumnHeight(2));
- assertEquals(4, b.getMaxHeight());
- }
- // Make more tests, by putting together longer series of
- // place, clearRows, undo, place ... checking a few col/row/max
- // numbers that the board looks right after the operations.
- @Test
- public void testSample3() {
- b = new Board(3, 6);
- assertEquals(b.getHeight(), 6);
- assertEquals(b.getWidth(), 3);
- b.commit();
- assertEquals(b.place(pieces[Piece.S1], 0, 0), Board.PLACE_OK);
- b.commit();
- assertEquals(b.place(pieces[Piece.S2], 0, 2), Board.PLACE_OK);
- b.commit();
- assertEquals(b.place(pieces[Piece.S1], 0, 4), Board.PLACE_OK);
- b.clearRows();
- b.clearRows();
- assertEquals(b.getColumnHeight(0), 5);
- assertEquals(b.getColumnHeight(1), 6);
- assertEquals(b.getColumnHeight(2), 6);
- assertEquals(b.getRowWidth(0), 2);
- assertEquals(b.getRowWidth(1), 2);
- assertEquals(b.getRowWidth(2), 2);
- assertEquals(b.getRowWidth(4), 2);
- assertEquals(b.getRowWidth(5), 2);
- assertEquals(b.getMaxHeight(), 6);
- }
- @Test
- public void testPlace() {
- b = new Board(3,7);
- b.commit();
- assertEquals(b.place(pieces[Piece.L1], 0, 0), Board.PLACE_OK);
- assertEquals(b.getGrid(0, 0), true);
- assertEquals(b.getGrid(1, 0), true);
- assertEquals(b.getGrid(0, 1), true);
- assertEquals(b.getGrid(1, 1), true);
- b.commit();
- assertEquals(b.place(pieces[Piece.L1].fastRotation(), 0, 3), Board.PLACE_ROW_FILLED);
- b.commit();
- assertEquals(b.place(pieces[Piece.STICK], 0, 0), Board.PLACE_BAD);
- b.commit();
- assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_OUT_BOUNDS);
- b.commit();
- assertEquals(b.place(pieces[Piece.SQUARE], 0, 6), Board.PLACE_OUT_BOUNDS);
- }
- @Test
- public void testClearRow() {
- b = new Board(3, 6);
- b.place(pieces[Piece.PYRAMID], 0, 0);
- b.commit();
- b.place(pieces[Piece.L2].fastRotation(), 0, 1);
- b.commit();
- b.place(pieces[Piece.S1], 0, 3);
- b.clearRows();
- b.commit();
- assertEquals(3, b.getMaxHeight());
- assertEquals(2, b.getColumnHeight(0));
- assertEquals(3, b.getColumnHeight(1));
- assertEquals(3, b.getColumnHeight(2));
- assertEquals(2, b.getRowWidth(0));
- assertEquals(2, b.getRowWidth(1));
- assertEquals(2, b.getRowWidth(2));
- assertEquals(0, b.getRowWidth(4));
- assertEquals(false, b.getGrid(0, 0));
- assertEquals(false, b.getGrid(2, 1));
- }
- @Test
- public void testClearRow2() {
- b = new Board(4, 8);
- assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 0), Board.PLACE_ROW_FILLED);
- b.commit();
- assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_ROW_FILLED);
- assertEquals(b.getMaxHeight(), 6);
- b.clearRows();
- b.commit();
- assertEquals(b.getMaxHeight(), 0);
- assertEquals(b.getColumnHeight(0), 0);
- assertEquals(b.getRowWidth(0), 0);
- b.place(pieces[Piece.PYRAMID], 0, 0);
- b.commit();
- b.place(pieces[Piece.PYRAMID].fastRotation(), 2, 0);
- b.commit();
- b.place(pieces[Piece.L1].fastRotation().fastRotation().fastRotation(), 0, 1);
- b.commit();
- b.place(pieces[Piece.S1], 0, 3);
- b.clearRows();
- b.commit();
- assertEquals(b.getMaxHeight(), 2);
- assertEquals(b.getRowWidth(0), 2);
- assertEquals(b.getRowWidth(1), 2);
- assertEquals(b.getRowWidth(2), 0);
- assertEquals(b.getColumnHeight(0), 1);
- assertEquals(b.getColumnHeight(1), 2);
- assertEquals(b.getColumnHeight(2), 2);
- assertEquals(b.getColumnHeight(3), 0);
- }
- @Test
- public void testDropHeight() {
- b = new Board(3, 6);
- assertEquals(b.dropHeight(pieces[Piece.L1], 0), 0);
- b.place(pieces[Piece.PYRAMID], 0, 0);
- b.commit();
- assertEquals(b.dropHeight(pieces[Piece.L1], 0), 2);
- b.place(pieces[Piece.S2], 0, 3);
- assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation(), 0), 5);
- assertEquals(b.dropHeight(pieces[Piece.STICK], 2), 4);
- assertEquals(b.dropHeight(pieces[Piece.STICK], 1), 5);
- }
- @Test
- public void testDropheight2() {
- b = new Board(4, 8);
- b.place(pieces[Piece.L1].fastRotation(), 0, 0);
- assertEquals(b.dropHeight(pieces[Piece.PYRAMID], 0), 2);
- assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 0), 1);
- assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 2), 1);
- b.commit();
- assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation().fastRotation(), 2), 0);
- b.place(pieces[Piece.L1].fastRotation().fastRotation(), 2, 0);
- assertEquals(b.dropHeight(pieces[Piece.STICK].fastRotation(), 0), 3);
- assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 1);
- b.commit();
- b.place(pieces[Piece.STICK].fastRotation(), 0, 4);
- assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 5);
- }
- // @Test
- // public void testUndo() {
- // b = new Board(4, 8);
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement