/* * Copyright (C) 2013-2015 2048FX * Jose Pereda, Bruno Borges & Jens Deters * All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.jpereda.game2048; import java.util.Random; import javafx.geometry.Pos; import javafx.scene.control.Label; public class Tile extends Label { private Integer value; private Location location; private Boolean merged; public static Tile newRandomTile() { int value = new Random().nextDouble() < 0.9 ? 2 : 4; return new Tile(value); } public static Tile newTile(int value) { return new Tile(value); } private Tile(Integer value) { final int squareSize = Board.CELL_SIZE - 13; setMinSize(squareSize, squareSize); setMaxSize(squareSize, squareSize); setPrefSize(squareSize, squareSize); setAlignment(Pos.CENTER); this.value = value; this.merged = false; setText(value.toString()); getStyleClass().addAll("game-label", "game-tile-" + value); } public void merge(Tile another) { getStyleClass().remove("game-tile-" + value); this.value += another.getValue(); setText(value.toString()); merged = true; getStyleClass().add("game-tile-" + value); } public Integer getValue() { return value; } public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; } @Override public String toString() { return "Tile{" + "value=" + value + ", location=" + location + '}'; } public boolean isMerged() { return merged; } public void clearMerge() { merged = false; } public boolean isMergeable(Tile anotherTile) { if(anotherTile==null){ return false; } return anotherTile.getValue().equals(getValue()); } }