From: gumartinm Date: Sun, 6 Nov 2011 23:13:39 +0000 (+0100) Subject: Some nice comments in Piece and Tile classes X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d36e2a50c7c6de790cf1e7718303afbdc8ff3061;p=JavaForFun Some nice comments in Piece and Tile classes --- diff --git a/AndroidTetris/src/de/android/androidtetris/Piece.java b/AndroidTetris/src/de/android/androidtetris/Piece.java index af0b343..a0c103a 100644 --- a/AndroidTetris/src/de/android/androidtetris/Piece.java +++ b/AndroidTetris/src/de/android/androidtetris/Piece.java @@ -7,14 +7,16 @@ import java.util.HashMap; 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; @@ -22,9 +24,9 @@ public enum Piece { } }, /*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; @@ -32,29 +34,29 @@ public enum Piece { } }, /*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; @@ -62,36 +64,44 @@ public enum Piece { } }, /*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 pieceMap = new HashMap(); + + //Static initializer. JLS§12.4.2 Executed in textual order. static { for (Piece piece : Piece.values()) { @@ -99,25 +109,42 @@ public enum Piece { } } - 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(); } diff --git a/AndroidTetris/src/de/android/androidtetris/Tile.java b/AndroidTetris/src/de/android/androidtetris/Tile.java index b8d07c1..b1e162d 100644 --- a/AndroidTetris/src/de/android/androidtetris/Tile.java +++ b/AndroidTetris/src/de/android/androidtetris/Tile.java @@ -3,64 +3,76 @@ package de.android.androidtetris; 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() { @@ -68,17 +80,39 @@ public enum Tile { } }; + + //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