Back to project page SeniorDesign.
The source code is released under:
GNU General Public License
If you think the Android project SeniorDesign listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package fakesetgame.seniordesign.model; //w ww .ja v a 2 s . c o m import android.content.Context; import android.graphics.drawable.Drawable; import java.util.Random; /** * A class modeling a single tile, it's attributes and visual representation. */ public class Tile { private static final Random random = new Random(); private Shape shape; private Shading shading; private Color color; private int shapeCount; public Tile(Shape shape, Shading shading, Color color, int shapeCount) { if (shape == null) { throw new IllegalArgumentException("Shape value cannot be null."); } if (shading == null) { throw new IllegalArgumentException("Shading value cannot be null."); } if (color == null) { throw new IllegalArgumentException("Color value cannot be null."); } if (shapeCount < 1 || shapeCount > 3) { throw new IllegalArgumentException("Value of shapeCount must be between 1 and 3."); } this.shape = shape; this.shading = shading; this.color = color; this.shapeCount = shapeCount; } public static Tile fromString(String tile){ if(!tile.matches("[123]{4}")) throw new IllegalArgumentException(String.format("Tile string must be like [123]{4}. Provided string (%s) does not match.", tile)); return new Tile( Shape.valueOf(Integer.valueOf(String.valueOf(tile.charAt(1)))), Shading.valueOf(Integer.valueOf(String.valueOf(tile.charAt(3)))), Color.valueOf(Integer.valueOf(String.valueOf(tile.charAt(2)))), Integer.valueOf(String.valueOf(tile.charAt(0))) ); } public static Tile generateRandom(){ Shape shape = Shape.valueOf(random.nextInt(3) + 1); Shading shading = Shading.valueOf(random.nextInt(3) + 1); Color color = Color.valueOf(random.nextInt(3) + 1); int shapeNum = random.nextInt(3) + 1; return new Tile(shape, shading, color, shapeNum); } public Shape getShape() { return this.shape; } public Shading getShading() { return this.shading; } public Color getColor() { return this.color; } public int getShapeCount() { return this.shapeCount; } public Drawable getDrawable(Context context) { Drawable drawable = context.getResources().getDrawable( context.getResources().getIdentifier( "tile_" + Integer.valueOf(getShapeCount()).toString() + Integer.valueOf(getShape().getNumVal()).toString() + Integer.valueOf(getColor().getNumVal()).toString() + Integer.valueOf(getShading().getNumVal()).toString(), "drawable", context.getPackageName() )); return drawable; } public Drawable getSmallDrawable(Context context) { Drawable drawable = context.getResources().getDrawable( context.getResources().getIdentifier( "tile_small_" + Integer.valueOf(getShapeCount()).toString() + Integer.valueOf(getShape().getNumVal()).toString() + Integer.valueOf(getColor().getNumVal()).toString() + Integer.valueOf(getShading().getNumVal()).toString(), "drawable", context.getPackageName() )); return drawable; } @Override public String toString() { return Integer.valueOf(getShapeCount()).toString() + Integer.valueOf(getShape().getNumVal()).toString() + Integer.valueOf(getColor().getNumVal()).toString() + Integer.valueOf(getShading().getNumVal()).toString(); } @Override public int hashCode() { return (Integer.valueOf(getShapeCount()).toString() + Integer.valueOf(getShape().getNumVal()).toString() + Integer.valueOf(getColor().getNumVal()).toString() + Integer.valueOf(getShading().getNumVal()).toString()).hashCode(); } @Override public boolean equals(Object o) { if(o == null) return false; if(!(o instanceof Tile)) return false; Tile other = (Tile)o; return getColor().equals(other.getColor()) && getShape().equals(other.getShape()) && getShading().equals(other.getShading()) && getShapeCount() == other.getShapeCount(); } }