summaryrefslogtreecommitdiff
path: root/src/tetris/piece
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2012-12-13 22:14:24 -0800
committerJesse Morgan <jesse@jesterpm.net>2012-12-13 22:14:24 -0800
commit87e98e00e0ca5ac4229091aa9bb0e5c09093f50f (patch)
treeb3e73ba3a22d1e04a6dd87111db27ac7d698d5f3 /src/tetris/piece
Tetris project from TCSS305 class.
Original project was in SVN on the school servers, so the versioning history is gone.
Diffstat (limited to 'src/tetris/piece')
-rw-r--r--src/tetris/piece/IPiece.java43
-rw-r--r--src/tetris/piece/JPiece.java38
-rw-r--r--src/tetris/piece/LPiece.java39
-rw-r--r--src/tetris/piece/OPiece.java39
-rw-r--r--src/tetris/piece/SPiece.java39
-rw-r--r--src/tetris/piece/TPiece.java38
-rw-r--r--src/tetris/piece/TetrisPiece.java171
-rw-r--r--src/tetris/piece/TetrisPieces.java103
-rw-r--r--src/tetris/piece/ZPiece.java38
9 files changed, 548 insertions, 0 deletions
diff --git a/src/tetris/piece/IPiece.java b/src/tetris/piece/IPiece.java
new file mode 100644
index 0000000..592887f
--- /dev/null
+++ b/src/tetris/piece/IPiece.java
@@ -0,0 +1,43 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris I piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class IPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1.5;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 2;
+
+ /**
+ * Array of of points for the I Piece (since it contains a 3).
+ */
+ private static final double[] POINTS = new double[] {0, 2, 1, 2, 2, 2, 3, 2};
+
+ /**
+ * Setup the I Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public IPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, POINTS, ROTATION_X, ROTATION_Y);
+ }
+}
diff --git a/src/tetris/piece/JPiece.java b/src/tetris/piece/JPiece.java
new file mode 100644
index 0000000..711a492
--- /dev/null
+++ b/src/tetris/piece/JPiece.java
@@ -0,0 +1,38 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris J piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class JPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1;
+
+ /**
+ * Setup the J Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public JPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {1, 0, 1, 1, 1, 2, 0, 2}, ROTATION_X, ROTATION_Y);
+ }
+} \ No newline at end of file
diff --git a/src/tetris/piece/LPiece.java b/src/tetris/piece/LPiece.java
new file mode 100644
index 0000000..3c1861e
--- /dev/null
+++ b/src/tetris/piece/LPiece.java
@@ -0,0 +1,39 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris L piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class LPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1;
+
+
+ /**
+ * Setup the L Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public LPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {1, 0, 1, 1, 1, 2, 2, 2}, ROTATION_X, ROTATION_Y);
+ }
+}
diff --git a/src/tetris/piece/OPiece.java b/src/tetris/piece/OPiece.java
new file mode 100644
index 0000000..1934110
--- /dev/null
+++ b/src/tetris/piece/OPiece.java
@@ -0,0 +1,39 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris O piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class OPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1.5;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1.5;
+
+
+ /**
+ * Setup the O Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public OPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {1, 1, 1, 2, 2, 1, 2, 2}, ROTATION_X, ROTATION_Y);
+ }
+} \ No newline at end of file
diff --git a/src/tetris/piece/SPiece.java b/src/tetris/piece/SPiece.java
new file mode 100644
index 0000000..92e5901
--- /dev/null
+++ b/src/tetris/piece/SPiece.java
@@ -0,0 +1,39 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris S piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class SPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1.5;
+
+
+ /**
+ * Setup the S Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public SPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {1, 1, 2, 1, 0, 2, 1, 2}, ROTATION_X, ROTATION_Y);
+ }
+} \ No newline at end of file
diff --git a/src/tetris/piece/TPiece.java b/src/tetris/piece/TPiece.java
new file mode 100644
index 0000000..a3e5c30
--- /dev/null
+++ b/src/tetris/piece/TPiece.java
@@ -0,0 +1,38 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris T piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class TPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1.5;
+
+ /**
+ * Setup the T Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public TPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {1, 1, 0, 2, 1, 2, 2, 2}, ROTATION_X, ROTATION_Y);
+ }
+} \ No newline at end of file
diff --git a/src/tetris/piece/TetrisPiece.java b/src/tetris/piece/TetrisPiece.java
new file mode 100644
index 0000000..e518fc3
--- /dev/null
+++ b/src/tetris/piece/TetrisPiece.java
@@ -0,0 +1,171 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 - Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+import java.awt.geom.AffineTransform;
+
+import tetris.model.IntPoint;
+
+/**
+ * Base Class to Represent a tetris piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class TetrisPiece {
+ // Public constants
+ /**
+ * Width and Height of a Tetris piece.
+ */
+ public static final int TETRIS_PIECE_SIZE = 4;
+
+ // Private Constants
+ /**
+ * Number of characters in each toString line.
+ */
+ private static final int CHARS_PER_TOSTRING_LINE = 5;
+
+ // Private Instance Fields
+ /**
+ * Storage for the piece points.
+ */
+ private final double[] my_points;
+
+ /**
+ * Our X-coordinate.
+ */
+ private final int my_x;
+
+ /**
+ * Our Y-coordinate.
+ */
+ private final int my_y;
+
+ /**
+ * Our X-coordinate for the point of rotation.
+ */
+ private final double my_rotatex;
+
+ /**
+ * Our Y-coordinate for the point of rotation.
+ */
+ private final double my_rotatey;
+
+ /**
+ * Setup the AbstractTetrisPiece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ * @param the_points The points that make up this piece.
+ * @param the_rotatex X-Coordinate to rotate the piece about.
+ * @param the_rotatey Y-Coordinate to rotate the piece about.
+ */
+ protected TetrisPiece(final int the_x, final int the_y, final double[] the_points,
+ final double the_rotatex, final double the_rotatey) {
+ my_x = the_x;
+ my_y = the_y;
+
+ my_points = the_points.clone();
+ my_rotatex = the_rotatex;
+ my_rotatey = the_rotatey;
+ }
+
+ /**
+ * Rotates the tetris piece counter-clockwise 90 degrees.
+ *
+ * @return the rotated piece.
+ */
+ public TetrisPiece rotateLeft() {
+ return rotate(-Math.PI / 2);
+ }
+
+ /**
+ * Rotates the tetris piece clockwise 90 degrees.
+ *
+ * @return the rotated piece.
+ */
+ public TetrisPiece rotateRight() {
+ return rotate(Math.PI / 2);
+ }
+
+ /**
+ * Move the tetris piece.
+ *
+ * @param the_dx Movement change along the X-axis.
+ * @param the_dy Movement change along the Y-axis.
+ * @return the moved piece.
+ */
+ public TetrisPiece translate(final int the_dx, final int the_dy) {
+ return new TetrisPiece(my_x + the_dx, my_y + the_dy, my_points, my_rotatex, my_rotatey);
+ }
+
+ /**
+ * @return The X position.
+ */
+ public int getX() {
+ return my_x;
+ }
+
+ /**
+ * @return The Y position.
+ */
+ public int getY() {
+ return my_y;
+ }
+
+ /**
+ * @return An int[] containing the piece's points in board-space.
+ * Sorted x1, y1, x2, y2, ..., xN, yN (N = number of points).
+ */
+ public IntPoint[] getBoardCoordinates() {
+ final IntPoint[] result = new IntPoint[my_points.length / 2];
+
+ for (int i = 0; i < my_points.length - 1; i = i + 2) {
+ final int x = (int) my_points[i];
+ final int y = (int) my_points[i + 1];
+
+ // i is always even.
+ result[i / 2] = new IntPoint(x + my_x, y + my_y);
+ }
+
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String toString() {
+ final char[] output = "....\n....\n....\n....".toCharArray();
+
+ for (int i = 0; i < my_points.length - 1; i = i + 2) {
+ // For each point, mark the coresponding element true
+ final int x = (int) my_points[i];
+ final int y = (int) my_points[i + 1];
+ output[x + CHARS_PER_TOSTRING_LINE * y] = 'X';
+ }
+
+ return new String(output);
+ }
+
+ /**
+ * Rotates the tetris piece by the_degrees.
+ *
+ * @param the_degrees The degrees in radians to rotate the piece.
+ * @return the rotated piece.
+ */
+ private TetrisPiece rotate(final double the_degrees) {
+ final AffineTransform trans = AffineTransform.getRotateInstance(the_degrees,
+ my_rotatex, my_rotatey);
+ final double[] new_points = new double[my_points.length];
+
+ trans.transform(my_points, 0, new_points, 0, my_points.length / 2);
+
+ return new TetrisPiece(my_x, my_y, new_points, my_rotatex, my_rotatey);
+ }
+}
diff --git a/src/tetris/piece/TetrisPieces.java b/src/tetris/piece/TetrisPieces.java
new file mode 100644
index 0000000..642be97
--- /dev/null
+++ b/src/tetris/piece/TetrisPieces.java
@@ -0,0 +1,103 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+import java.util.Random;
+
+/**
+ * Enumeration of the various tetris pieces.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 23 November 2009
+ */
+public enum TetrisPieces {
+ // Enumeration definition.
+ /**
+ * The I Piece.
+ */
+ I_PIECE,
+
+ /**
+ * The J Piece.
+ */
+ J_PIECE,
+
+ /**
+ * The L Piece.
+ */
+ L_PIECE,
+
+ /**
+ * The O Piece.
+ */
+ O_PIECE,
+
+ /**
+ * The S Piece.
+ */
+ S_PIECE,
+
+ /**
+ * The T Piece.
+ */
+ T_PIECE,
+
+ /**
+ * The Z Piece.
+ */
+ Z_PIECE;
+
+ // Private Constants
+ /**
+ * A Random that we use for generating random pieces.
+ */
+ private static final Random RANDOM = new Random();
+
+ /**
+ * @param the_x X coordinate for this piece.
+ * @param the_y Y coordinate for this piece.
+ * @return a random tetris piece.
+ */
+ public static TetrisPiece getRandomPiece(final int the_x, final int the_y) {
+ TetrisPiece piece;
+
+ switch (values()[RANDOM.nextInt(values().length)]) {
+ case I_PIECE:
+ piece = new IPiece(the_x, the_y);
+ break;
+
+ case J_PIECE:
+ piece = new JPiece(the_x, the_y);
+ break;
+
+ case L_PIECE:
+ piece = new LPiece(the_x, the_y);
+ break;
+
+ case O_PIECE:
+ piece = new OPiece(the_x, the_y);
+ break;
+
+ case S_PIECE:
+ piece = new SPiece(the_x, the_y);
+ break;
+
+ case T_PIECE:
+ piece = new TPiece(the_x, the_y);
+ break;
+
+ case Z_PIECE:
+ default: // This is a fail-safe should something very bizarre happen.
+ piece = new ZPiece(the_x, the_y);
+ break;
+ }
+
+ return piece;
+ }
+}
diff --git a/src/tetris/piece/ZPiece.java b/src/tetris/piece/ZPiece.java
new file mode 100644
index 0000000..152d9c2
--- /dev/null
+++ b/src/tetris/piece/ZPiece.java
@@ -0,0 +1,38 @@
+/*
+ * Jesse Morgan <jesterpm@u.washington.edu>
+ *
+ * TCSS 305 Ð Autumn 2009
+ * Tetris Project
+ * 17 November 2009
+ */
+
+package tetris.piece;
+
+/**
+ * Class to represent a Tetris Z piece.
+ *
+ * @author Jesse Morgan <jesterpm@u.washington.edu>
+ * @version 1.0 17 November 2009
+ */
+public class ZPiece extends TetrisPiece {
+ // Private Constants
+ /**
+ * X Rotation Point.
+ */
+ private static final double ROTATION_X = 1;
+
+ /**
+ * Y Rotation Point.
+ */
+ private static final double ROTATION_Y = 1.5;
+
+ /**
+ * Setup the Z Piece.
+ *
+ * @param the_x Tetris piece X-coord.
+ * @param the_y Tetris piece Y-coord.
+ */
+ public ZPiece(final int the_x, final int the_y) {
+ super(the_x, the_y, new double[] {0, 1, 1, 1, 1, 2, 2, 2}, ROTATION_X, ROTATION_Y);
+ }
+} \ No newline at end of file