Ejemplo n.º 1
0
 private void addTile() {
   List<Tile> list = availableSpace();
   if (!availableSpace().isEmpty()) {
     int index = (int) (Math.random() * list.size()) % list.size();
     Tile emptyTime = list.get(index);
     emptyTime.value = Math.random() < 0.9 ? 2 : 4;
   }
 }
Ejemplo n.º 2
0
 private List<Tile> availableSpace() {
   final List<Tile> list = new ArrayList<Tile>(16);
   for (Tile t : myTiles) {
     if (t.isEmpty()) {
       list.add(t);
     }
   }
   return list;
 }
Ejemplo n.º 3
0
 public void merge(Tile another) {
   getStyleClass().remove("tile-" + value);
   this.value += another.getValue();
   setText(value.toString());
   merged = true;
   getStyleClass().add("tile-" + value);
 }
Ejemplo n.º 4
0
  private void drawTile(Graphics g2, Tile tile, int x, int y) {
    Graphics2D g = ((Graphics2D) g2);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    int value = tile.value;
    int xOffset = offsetCoors(x);
    int yOffset = offsetCoors(y);
    g.setColor(tile.getBackground());
    g.fillRoundRect(xOffset, yOffset, TILE_SIZE, TILE_SIZE, 14, 14);
    g.setColor(tile.getForeground());
    final int size = value < 100 ? 36 : value < 1000 ? 32 : 24;
    final Font font = new Font(FONT_NAME, Font.BOLD, size);
    g.setFont(font);

    String s = String.valueOf(value);
    final FontMetrics fm = getFontMetrics(font);

    final int w = fm.stringWidth(s);
    final int h = -(int) fm.getLineMetrics(s, g).getBaselineOffsets()[2];

    if (value != 0)
      g.drawString(s, xOffset + (TILE_SIZE - w) / 2, yOffset + TILE_SIZE - (TILE_SIZE - h) / 2 - 2);

    if (myWin || myLose) {
      g.setColor(new Color(255, 255, 255, 30));
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(new Color(78, 139, 202));
      g.setFont(new Font(FONT_NAME, Font.BOLD, 48));
      if (myWin) {
        g.drawString("You won!", 68, 150);
      }
      if (myLose) {
        g.drawString("Game over!", 50, 130);
        g.drawString("You lose!", 64, 200);
      }
      if (myWin || myLose) {
        g.setFont(new Font(FONT_NAME, Font.PLAIN, 16));
        g.setColor(new Color(128, 128, 128, 128));
        g.drawString("Press ESC to play again", 80, getHeight() - 40);
      }
    }
    g.setFont(new Font(FONT_NAME, Font.PLAIN, 18));
    g.drawString("Score: " + myScore, 200, 365);
  }
Ejemplo n.º 5
0
 public boolean isMergeable(Tile anotherTile) {
   return anotherTile != null && getValue().equals(anotherTile.getValue());
 }