From: gumartinm Date: Sun, 6 Nov 2011 01:11:10 +0000 (+0100) Subject: Enum classes for Tiles and Pieces. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=e5a9c77fdab671eb4e4cd2c494d8b7e4bf1aabb9;p=JavaForFun Enum classes for Tiles and Pieces. They could be useful. --- diff --git a/AndroidTetris/src/de/android/androidtetris/DrawView.java b/AndroidTetris/src/de/android/androidtetris/DrawView.java index bc6a7b9..26829e1 100644 --- a/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -91,18 +91,14 @@ public class DrawView extends SurfaceView { { super(context); this.resetTiles(8); - this.loadTile(0, Color.RED); - this.loadTile(1, Color.BLUE); - this.loadTile(2, Color.CYAN); - this.loadTile(3, Color.GREEN); - this.loadTile(4, Color.YELLOW); - this.loadTile(5, Color.WHITE); - this.loadTile(6, Color.MAGENTA); - this.loadTile(7, Color.GRAY); - - // register our interest in hearing about changes to our surface - //SurfaceHolder holder = getHolder(); - //holder.addCallback(this); + for (Tile color : Tile.values() ) + { + this.loadTile(color.getColor(), color.getColorRGBA()); + } + + // register our interest in hearing about changes to our surface + //SurfaceHolder holder = getHolder(); + //holder.addCallback(this); gameLoopThread = new AndroidTetrisThread(this); holder = getHolder(); holder.addCallback(new SurfaceHolder.Callback() { diff --git a/AndroidTetris/src/de/android/androidtetris/Piece.java b/AndroidTetris/src/de/android/androidtetris/Piece.java new file mode 100644 index 0000000..8ecb1bd --- /dev/null +++ b/AndroidTetris/src/de/android/androidtetris/Piece.java @@ -0,0 +1,112 @@ +/** + * + */ +package de.android.androidtetris; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author gusarapo + * + */ +public enum Piece { + /*The tower piece*/ + TOWER(0) { + @Override + void refill() { + size[1][0]=Tile.RED; + size[1][1]=Tile.RED; + size[1][2]=Tile.RED; + size[1][3]=Tile.RED; + } + }, + /*The box piece*/ + BOX(1) { + @Override + void refill() { + size[1][1]=Tile.BLUE; + size[1][2]=Tile.BLUE; + size[2][1]=Tile.BLUE; + size[2][2]=Tile.BLUE; + } + }, + /*The pyramid piece*/ + PYRAMID(2) { + @Override + void refill() { + size[1][1]=Tile.CYAN; + size[0][2]=Tile.CYAN; + size[1][2]=Tile.CYAN; + size[2][2]=Tile.CYAN; + } + }, + /*The left leaner piece*/ + LEFTLEANER(3) { + @Override + void refill() { + size[0][1]=Tile.YELLOW; + size[1][1]=Tile.YELLOW; + size[1][2]=Tile.YELLOW; + size[2][2]=Tile.YELLOW; + } + }, + /*The right leaner piece*/ + RIGHTLEANER(4) { + @Override + void refill() { + size[2][1]=Tile.GREEN; + size[1][1]=Tile.GREEN; + size[1][2]=Tile.GREEN; + size[0][2]=Tile.GREEN; + } + }, + /*The left knight piece*/ + LEFTKNIGHT(5) { + @Override + void refill() { + size[1][1]=Tile.WHITE; + size[2][1]=Tile.WHITE; + size[2][2]=Tile.WHITE; + size[2][3]=Tile.WHITE; + } + }, + /*The right knight piece*/ + RIGHTKNIGHT(6) { + @Override + void refill() { + size[2][1]=Tile.MAGENTA; + size[1][1]=Tile.MAGENTA; + size[1][2]=Tile.MAGENTA; + size[1][3]=Tile.MAGENTA; + } + }; + + private static Tile[][] size = new Tile[4][4]; + //Store the x coordinate (the position of this piece on the grid) + private int x; + //Store the y coordinate (the position of this piece on the grid) + private int y; + private final int pieceNumber; + private static final Map pieceMap = new HashMap(); + + static { + for (Piece piece : Piece.values()) + { + pieceMap.put(piece.pieceNumber, piece); + } + } + + private Piece (int pieceNumber) + { + this.pieceNumber = pieceNumber; + } + + public Piece getPiece (int pieceNumber) + { + return pieceMap.get(pieceNumber); + } + + abstract void refill(); + +} diff --git a/AndroidTetris/src/de/android/androidtetris/Tile.java b/AndroidTetris/src/de/android/androidtetris/Tile.java new file mode 100644 index 0000000..a0e2b3e --- /dev/null +++ b/AndroidTetris/src/de/android/androidtetris/Tile.java @@ -0,0 +1,72 @@ +package de.android.androidtetris; + +import android.graphics.Color; + +/** + * @author gusarapo + * + */ +public enum Tile { + RED(0) { + @Override + int getColorRGBA() { + return Color.RED; + } + }, + BLUE(1) { + @Override + int getColorRGBA() { + return Color.BLUE; + } + }, + CYAN(2) { + @Override + int getColorRGBA() { + return Color.CYAN; + } + }, + GREEN(3) { + @Override + int getColorRGBA() { + return Color.GREEN; + } + }, + YELLOW(4) { + @Override + int getColorRGBA() { + return Color.YELLOW; + } + }, + WHITE(5) { + @Override + int getColorRGBA() { + return Color.WHITE; + } + }, + MAGENTA(6) { + @Override + int getColorRGBA() { + return Color.MAGENTA; + } + }, + GRAY(7) { + @Override + int getColorRGBA() { + return Color.GRAY; + } + }; + + private int color; + + Tile (int color) + { + this.color = color; + } + + public int getColor() + { + return color; + } + + abstract int getColorRGBA(); +} \ No newline at end of file