From 87e98e00e0ca5ac4229091aa9bb0e5c09093f50f Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Thu, 13 Dec 2012 22:14:24 -0800 Subject: Tetris project from TCSS305 class. Original project was in SVN on the school servers, so the versioning history is gone. --- src/tetris/tests/board/TetrisBoardTest.java | 219 ++++++++++++++++++++++++++++ src/tetris/tests/piece/IPieceTest.java | 39 +++++ src/tetris/tests/piece/JPieceTest.java | 42 ++++++ src/tetris/tests/piece/LPieceTest.java | 43 ++++++ src/tetris/tests/piece/OPieceTest.java | 41 ++++++ src/tetris/tests/piece/SPieceTest.java | 42 ++++++ src/tetris/tests/piece/TPieceTest.java | 43 ++++++ src/tetris/tests/piece/TetrisPieceTest.java | 182 +++++++++++++++++++++++ src/tetris/tests/piece/ZPieceTest.java | 42 ++++++ 9 files changed, 693 insertions(+) create mode 100644 src/tetris/tests/board/TetrisBoardTest.java create mode 100644 src/tetris/tests/piece/IPieceTest.java create mode 100644 src/tetris/tests/piece/JPieceTest.java create mode 100644 src/tetris/tests/piece/LPieceTest.java create mode 100644 src/tetris/tests/piece/OPieceTest.java create mode 100644 src/tetris/tests/piece/SPieceTest.java create mode 100644 src/tetris/tests/piece/TPieceTest.java create mode 100644 src/tetris/tests/piece/TetrisPieceTest.java create mode 100644 src/tetris/tests/piece/ZPieceTest.java (limited to 'src/tetris/tests') diff --git a/src/tetris/tests/board/TetrisBoardTest.java b/src/tetris/tests/board/TetrisBoardTest.java new file mode 100644 index 0000000..d14f13b --- /dev/null +++ b/src/tetris/tests/board/TetrisBoardTest.java @@ -0,0 +1,219 @@ +/* + * Jesse Morgan + * + * 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 + * @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 pieces = new ArrayList(); + + 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 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)); + } + } +} diff --git a/src/tetris/tests/piece/IPieceTest.java b/src/tetris/tests/piece/IPieceTest.java new file mode 100644 index 0000000..031b1e8 --- /dev/null +++ b/src/tetris/tests/piece/IPieceTest.java @@ -0,0 +1,39 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 Tetris Project 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.IPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the I Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class IPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {"....\n....\nXXXX\n....", ".X..\n.X..\n.X..\n.X.."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {0, 2, 1, 2, 2, 2, 3, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new IPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } +} diff --git a/src/tetris/tests/piece/JPieceTest.java b/src/tetris/tests/piece/JPieceTest.java new file mode 100644 index 0000000..1b04781 --- /dev/null +++ b/src/tetris/tests/piece/JPieceTest.java @@ -0,0 +1,42 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.JPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the J Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class JPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {".X..\n.X..\nXX..\n....", "....\nXXX.\n..X.\n....", + ".XX.\n.X..\n.X..\n....", "X...\nXXX.\n....\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {1, 0, 1, 1, 1, 2, 0, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new JPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } +} diff --git a/src/tetris/tests/piece/LPieceTest.java b/src/tetris/tests/piece/LPieceTest.java new file mode 100644 index 0000000..f5e045d --- /dev/null +++ b/src/tetris/tests/piece/LPieceTest.java @@ -0,0 +1,43 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.LPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the L Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class LPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {".X..\n.X..\n.XX.\n....", "..X.\nXXX.\n....\n....", + "XX..\n.X..\n.X..\n....", "....\nXXX.\nX...\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {1, 0, 1, 1, 1, 2, 2, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new LPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } + +} diff --git a/src/tetris/tests/piece/OPieceTest.java b/src/tetris/tests/piece/OPieceTest.java new file mode 100644 index 0000000..67447c2 --- /dev/null +++ b/src/tetris/tests/piece/OPieceTest.java @@ -0,0 +1,41 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.OPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the O Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class OPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = new String[] {"....\n.XX.\n.XX.\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {1, 1, 1, 2, 2, 1, 2, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new OPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } + +} diff --git a/src/tetris/tests/piece/SPieceTest.java b/src/tetris/tests/piece/SPieceTest.java new file mode 100644 index 0000000..3125f61 --- /dev/null +++ b/src/tetris/tests/piece/SPieceTest.java @@ -0,0 +1,42 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.SPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the S Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class SPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {"....\n.XX.\nXX..\n....", "X...\nXX..\n.X..\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {1, 1, 2, 1, 0, 2, 1, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new SPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } + +} diff --git a/src/tetris/tests/piece/TPieceTest.java b/src/tetris/tests/piece/TPieceTest.java new file mode 100644 index 0000000..ff68a44 --- /dev/null +++ b/src/tetris/tests/piece/TPieceTest.java @@ -0,0 +1,43 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.TPiece; +import tetris.piece.TetrisPiece; + +/** + * JUnit Test for the T Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class TPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {"....\n.X..\nXXX.\n....", ".X..\nXX..\n.X..\n....", + "....\nXXX.\n.X..\n....", "X...\nXX..\nX...\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {1, 1, 0, 2, 1, 2, 2, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new TPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } + +} diff --git a/src/tetris/tests/piece/TetrisPieceTest.java b/src/tetris/tests/piece/TetrisPieceTest.java new file mode 100644 index 0000000..f085207 --- /dev/null +++ b/src/tetris/tests/piece/TetrisPieceTest.java @@ -0,0 +1,182 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 Tetris Project 17 November 2009 + */ + +package tetris.tests.piece; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import tetris.model.IntPoint; +import tetris.piece.TetrisPiece; + +/** + * Parent JUnit test class for Tetris Pieces. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public abstract class TetrisPieceTest { + // Private Constants + /** + * Random X position we use to test. + */ + private static final int X_POSITION = 5; + + /** + * Random Y position we use to test. + */ + private static final int Y_POSITION = 3; + + /** + * Number of rotations require to rotate a piece 360 degrees. + */ + private static final int NUMBER_OF_ROTATIONS = 4; + + /** + * Test that a piece is created properly. + */ + @Test + public void testPieceConstruction() { + final TetrisPiece piece = getPiece(5, 3); + + // Test the X and Y coords. + assertEquals("Inital X", X_POSITION, piece.getX()); + assertEquals("Inital Y", Y_POSITION, piece.getY()); + + // Test Board Position + assertTrue("Initial Board Positon Fails", + testBoardPosition(piece, X_POSITION, Y_POSITION)); + } + + /** + * Test that a piece is the same after four rotations. + */ + @Test + public void testPieceAfterRotations() { + final TetrisPiece before = getPiece(5, 3); + TetrisPiece after; + + // Left rotations + after = getPiece(X_POSITION, Y_POSITION); + for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) { + after = after.rotateLeft(); + } + + assertEquals("Pieces differ after 4 left rotations", before.toString(), after.toString()); + + // Right rotations + after = getPiece(X_POSITION, Y_POSITION); + for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) { + after = after.rotateRight(); + } + + assertEquals("Pieces differ after 4 right rotations", before.toString(), after.toString()); + } + + /** + * Test Translations. + */ + @Test + public void testTranslations() { + TetrisPiece piece = getPiece(X_POSITION, Y_POSITION); + + // Negative Translation + piece = piece.translate(-1, -1); + assertTrue("Negative Translations Fail", + testBoardPosition(piece, X_POSITION - 1 , Y_POSITION - 1)); + + // Positive Translation + piece = piece.translate(1, 1); + assertTrue("Positive Translations Fail", + testBoardPosition(piece, X_POSITION - 1 + 1, Y_POSITION - 1 + 1)); + } + + /** + * Test each left rotation String. + */ + @Test + public void testLeftRotations() { + TetrisPiece piece = getPiece(1, 0); + + final String[] rotations = getRotationStrings(); + + for (int i = 0; i <= NUMBER_OF_ROTATIONS; i++) { + assertEquals(i + "th Left Rotation Failed", + rotations[i % rotations.length], piece.toString()); + + piece = piece.rotateLeft(); + } + } + + /** + * Test each right rotation String. + */ + @Test + public void testRightRotations() { + TetrisPiece piece = getPiece(1, 0); + + final String[] rotations = getRotationStrings(); + + for (int i = NUMBER_OF_ROTATIONS; i >= 0; i--) { + assertEquals((i - NUMBER_OF_ROTATIONS) + "th Right Rotation Failed", + rotations[i % rotations.length], piece.toString()); + + piece = piece.rotateRight(); + } + } + + /** + * Tests the board position. + * @param the_piece The tetris piece. + * @param the_x Board X. + * @param the_y Board y. + * @return true if all points are correct, false otherwise. + */ + protected boolean testBoardPosition(final TetrisPiece the_piece, + final int the_x, final int the_y) { + boolean result = true; + + final int[] orig_positions = getOriginalPoints(); + final IntPoint[] cur_positions = the_piece.getBoardCoordinates(); + + // Both sets should contain the same number of points. + assertSame("Original and Current point counts differ.", + orig_positions.length, 2 * cur_positions.length); + + for (int i = 0; i < orig_positions.length - 1; i = i + 2) { + // Check that each X and Y was shifted properly. + if (orig_positions[i] + the_x != cur_positions[i / 2].getX()) { + result = false; + } + + if (orig_positions[i + 1] + the_y != cur_positions[i / 2].getY()) { + result = false; + } + } + + return result; + } + + /** + * @param the_x X-coord. + * @param the_y X-coord. + * @return Return a tetris piece of the type we're testing. + */ + protected abstract TetrisPiece getPiece(final int the_x, final int the_y); + + /** + * @return int[] containing the orignal points for the piece. + */ + protected abstract int[] getOriginalPoints(); + + /** + * @return int[] containing the string representation of various rotation states + */ + protected abstract String[] getRotationStrings(); +} diff --git a/src/tetris/tests/piece/ZPieceTest.java b/src/tetris/tests/piece/ZPieceTest.java new file mode 100644 index 0000000..106dc00 --- /dev/null +++ b/src/tetris/tests/piece/ZPieceTest.java @@ -0,0 +1,42 @@ +/* + * Jesse Morgan + * + * TCSS 305 РAutumn 2009 + * Tetris Project + * 17 November 2009 + */ + +package tetris.tests.piece; + +import tetris.piece.TetrisPiece; +import tetris.piece.ZPiece; + +/** + * JUnit Test for the Z Piece. + * + * @author Jesse Morgan + * @version 1.0 17 November 2009 + */ +public class ZPieceTest extends TetrisPieceTest { + /** + * Array of the string representations of various rotations. + */ + private static final String[] ROTATION_STRINGS = + new String[] {"....\nXX..\n.XX.\n....", ".X..\nXX..\nX...\n...."}; + + @Override + protected int[] getOriginalPoints() { + return new int[] {0, 1, 1, 1, 1, 2, 2, 2}; // Copied from the class. + } + + @Override + protected TetrisPiece getPiece(final int the_x, final int the_y) { + return new ZPiece(the_x, the_y); + } + + @Override + protected String[] getRotationStrings() { + return ROTATION_STRINGS.clone(); + } + +} -- cgit v1.2.3