Метод setCellColoredNumber(int, int, int) не должен отображать значение плитки на экране, если оно равно 0.
Но он же не отображает
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
import com.javarush.engine.cell.Game;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
createNewNumber();
createNewNumber();
}
private void drawScene() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
setCellColoredNumber(y, x, gameField[y][x]);
}
}
}
private void createNewNumber() {
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
int z = getRandomNumber(10);
if (gameField[y][x] == 0) {
if (z == 9) {
gameField[y][x] = 4;
} else if (z < 9) {
gameField[y][x] = 2;
}
} else if (gameField[y][x] != 0) {
createNewNumber();
}
}
private Color getColorByValue(int value) {//будет возвращать цвет в зависимости от переданного в метод значения клетки
switch (value) {
case (0):
return Color.SKYBLUE;
case (2):
return Color.WHEAT;//чтобы изменить цвет и значение клетки одновременно
case (4):
return Color.SILVER;
case (8):
return Color.BLUEVIOLET;
case (16):
return Color.DARKGRAY;
case (32):
return Color.SALMON;
case (64):
return Color.ALICEBLUE;
case (128):
return Color.ROYALBLUE;
case (256):
return Color.RED;
case (512):
return Color.FIREBRICK;
case (1024):
return Color.AZURE;
case (2048):
return Color.BEIGE;
}
return null;
}
private void setCellColoredNumber(int x, int y, int value) {
if (value == 0) {
setCellValueEx(y, x, getColorByValue(value),"");
} else if (value != 0) {
setCellValueEx(y, x, getColorByValue(value), "" + value);
}
}
}
