import java.util.Map;
/**
+ * This enum stores every piece and the square related to that piece.
+ *
* @author gusarapo
*
*/
public enum Piece {
/*The tower piece*/
- TOWER(0) {
+ I(0) {
@Override
- void refill() {
+ void fill() {
size[1][0]=Tile.RED;
size[1][1]=Tile.RED;
size[1][2]=Tile.RED;
}
},
/*The box piece*/
- BOX(1) {
+ O(1) {
@Override
- void refill() {
+ void fill() {
size[1][1]=Tile.BLUE;
size[1][2]=Tile.BLUE;
size[2][1]=Tile.BLUE;
}
},
/*The pyramid piece*/
- PYRAMID(2) {
+ T(2) {
@Override
- void refill() {
- size[1][1]=Tile.CYAN;
- size[0][2]=Tile.CYAN;
- size[1][2]=Tile.CYAN;
- size[2][2]=Tile.CYAN;
+ void fill() {
+ size[1][1]=Tile.YELLOW;
+ size[0][2]=Tile.YELLOW;
+ size[1][2]=Tile.YELLOW;
+ size[2][2]=Tile.YELLOW;
}
},
/*The left leaner piece*/
- LEFTLEANER(3) {
+ Z(3) {
@Override
- void refill() {
- size[0][1]=Tile.YELLOW;
- size[1][1]=Tile.YELLOW;
- size[1][2]=Tile.YELLOW;
- size[2][2]=Tile.YELLOW;
+ void fill() {
+ size[0][1]=Tile.CYAN;
+ size[1][1]=Tile.CYAN;
+ size[1][2]=Tile.CYAN;
+ size[2][2]=Tile.CYAN;
}
},
/*The right leaner piece*/
- RIGHTLEANER(4) {
+ S(4) {
@Override
- void refill() {
+ void fill() {
size[2][1]=Tile.GREEN;
size[1][1]=Tile.GREEN;
size[1][2]=Tile.GREEN;
}
},
/*The left knight piece*/
- LEFTKNIGHT(5) {
+ L(5) {
@Override
- void refill() {
- size[1][1]=Tile.WHITE;
- size[2][1]=Tile.WHITE;
- size[2][2]=Tile.WHITE;
- size[2][3]=Tile.WHITE;
+ void fill() {
+ size[1][1]=Tile.MAGENTA;
+ size[2][1]=Tile.MAGENTA;
+ size[2][2]=Tile.MAGENTA;
+ size[2][3]=Tile.MAGENTA;
}
},
/*The right knight piece*/
- RIGHTKNIGHT(6) {
+ J(6) {
@Override
- void refill() {
- size[2][1]=Tile.MAGENTA;
- size[1][1]=Tile.MAGENTA;
- size[1][2]=Tile.MAGENTA;
- size[1][3]=Tile.MAGENTA;
+ void fill() {
+ size[2][1]=Tile.WHITE;
+ size[1][1]=Tile.WHITE;
+ size[1][2]=Tile.WHITE;
+ size[1][3]=Tile.WHITE;
}
};
+
+ //Every is contained in a square. This is the square's width.
private static final int WIDTH = 4;
+ //Every is contained in a square. This is the square's height.
private static final int HEIGHT = 4;
+ //Every piece is contained in a square.
public Tile[][] size = new Tile[WIDTH][HEIGHT];
- //Store the x coordinate (the position of this piece on the grid)
- public int x;
- //Store the y coordinate (the position of this piece on the grid)
- public int y;
+ //Stores the x coordinate (the position of this piece on the grid)
+ public int x = 0;
+ //Stores the y coordinate (the position of this piece on the grid)
+ public int y = 0;
+ //Stores the argument of the enum constant (passed to the constructor) JLS§8.9.1
private final int pieceNumber;
+ //Map with every enum constant. Class variable initializer. JLS§12.4.2 Executed in textual order.
private static final Map<Integer, Piece> pieceMap = new HashMap<Integer, Piece>();
+
+ //Static initializer. JLS§12.4.2 Executed in textual order.
static {
for (Piece piece : Piece.values())
{
}
}
- private Piece (int pieceNumber)
+
+ /**
+ * Because we have enum constants with arguments we have to create this constructor.
+ * It initializes the piece with the right values.
+ *
+ * @param pieceNumber It is the argument of the enum constant
+ */
+ private Piece (final int pieceNumber)
{
this.pieceNumber = pieceNumber;
- this.y = 10;
- this.x = 10;
//Pre-Initialization of matrix size
for (int i=0; i< WIDTH; i++)
for (int j=0; j< WIDTH; j++)
size[i][j]= Tile.NOCOLOR;
- this.refill();
+ //It depends on what kind of piece, we have to fill the square in the right way.
+ this.fill();
}
- public static final Piece getPiece (int pieceNumber)
+
+ /**
+ * This method is used to retrieve the enum piece related to its number
+ *
+ * @param pieceNumber The piece number is associated to the argument of the enum constant.
+ * @return the enum whose argument of the enum constant matches the pieceNumber.
+ */
+ public static final Piece getPiece (final int pieceNumber)
{
return pieceMap.get(pieceNumber);
}
- abstract void refill();
+
+ /**
+ * This method is intended to be overridden by every piece to fill the square which contains the piece's shape.
+ */
+ abstract void fill();
}
import android.graphics.Color;
/**
+ * This enum stores every tile and its associated RGBA color.
+ *
* @author gusarapo
*
*/
public enum Tile {
+ /*The red color*/
RED(0) {
@Override
int getColorRGBA() {
return Color.RED;
}
},
+ /*The blue color*/
BLUE(1) {
@Override
int getColorRGBA() {
return Color.BLUE;
}
},
+ /*The cyan color*/
CYAN(2) {
@Override
int getColorRGBA() {
return Color.CYAN;
}
},
+ /*The green color*/
GREEN(3) {
@Override
int getColorRGBA() {
return Color.GREEN;
}
},
+ /*The yellow color*/
YELLOW(4) {
@Override
int getColorRGBA() {
return Color.YELLOW;
}
},
+ /*The white color*/
WHITE(5) {
@Override
int getColorRGBA() {
return Color.WHITE;
}
},
+ /*The magenta color*/
MAGENTA(6) {
@Override
int getColorRGBA() {
return Color.MAGENTA;
}
},
+ /*The gray color*/
GRAY(7) {
@Override
int getColorRGBA() {
return Color.GRAY;
}
},
+ /*The black color*/
BLACK(8) {
@Override
int getColorRGBA() {
return Color.BLACK;
}
},
+ /*No color. It is used in pieces in order to create the piece's shape filling squares with color and no color*/
NOCOLOR(9) {
@Override
int getColorRGBA() {
}
};
+
+ //Stores the argument of the enum constant (passed to the constructor) JLS§8.9.1
private int color;
+
+ /**
+ * Because we have enum constants with arguments we have to create this constructor.
+ * It initializes the tile with the right values.
+ *
+ * @param color It is the argument of the enum constant
+ */
Tile (int color)
{
this.color = color;
}
+
+ /**
+ * This method retrieves the argument associated to the enum constant.
+ *
+ * @return integer value associated to the enum constant.
+ */
public int getColor()
{
return color;
}
+
+ /**
+ * This method is intended to be overridden by every tile to retrieve the
+ * RGBA color associated to that tile.
+ *
+ * @return RGBA color
+ */
abstract int getColorRGBA();
}
\ No newline at end of file