Android Open Source - SeniorDesign Tile






From Project

Back to project page SeniorDesign.

License

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.

Java Source Code

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();
    }
}




Java Source Code List

fakesetgame.seniordesign.BoardTest.java
fakesetgame.seniordesign.GameScreen.java
fakesetgame.seniordesign.GameTest.java
fakesetgame.seniordesign.HomeScreen.java
fakesetgame.seniordesign.OptionsScreen.java
fakesetgame.seniordesign.SettingsTest.java
fakesetgame.seniordesign.SplashScreen.java
fakesetgame.seniordesign.SummaryScreen.java
fakesetgame.seniordesign.TileSetTest.java
fakesetgame.seniordesign.data.FoundSetRecord.java
fakesetgame.seniordesign.data.GameOutcome.java
fakesetgame.seniordesign.data.GameSummaryListItemCursorAdapter.java
fakesetgame.seniordesign.data.OptionsHelper.java
fakesetgame.seniordesign.data.PlayerDataDbHelper.java
fakesetgame.seniordesign.data.Setting.java
fakesetgame.seniordesign.data.package-info.java
fakesetgame.seniordesign.model.BoardSize.java
fakesetgame.seniordesign.model.Board.java
fakesetgame.seniordesign.model.Color.java
fakesetgame.seniordesign.model.GameOverEvent.java
fakesetgame.seniordesign.model.GameOverListener.java
fakesetgame.seniordesign.model.Game.java
fakesetgame.seniordesign.model.HintProvider.java
fakesetgame.seniordesign.model.Modifier.java
fakesetgame.seniordesign.model.Shading.java
fakesetgame.seniordesign.model.Shape.java
fakesetgame.seniordesign.model.TileSet.java
fakesetgame.seniordesign.model.Tile.java
fakesetgame.seniordesign.model.package-info.java
fakesetgame.seniordesign.view.GameSummaryListItemView.java
fakesetgame.seniordesign.view.ShadedImageView.java
fakesetgame.seniordesign.view.package-info.java
fakesetgame.seniordesign.package-info.java