Hi,
for some reason the code keeps failing on the second to last condition. Could someone please help?
Thanks
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
createNewNumber();
createNewNumber();
}
private void drawScene() {
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
setCellColoredNumber(y,x, gameField[y][x]);
}
}
}
private void createNewNumber() {
int cellValue = 1;
int x = 0;
int y = 0;
while (cellValue != 0) {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
if (gameField[x][y] == 0) {
cellValue = 0;
}
}
int newRandomNum = getRandomNumber(10);
if (newRandomNum == 9) {
gameField[x][y] = 4;
} else {
gameField[x][y] = 2;
}
}
private Color getColorByValue(int value) {
Color color = null;
switch (value) {
case 0:
color = Color.WHITE;
break;
case 2:
color = Color.BLUE;
break;
case 4:
color = Color.RED;
break;
case 8:
color = Color.CYAN;
break;
case 16:
color = Color.GREEN;
break;
case 32:
color = Color.YELLOW;
break;
case 64:
color = Color.ORANGE;
break;
case 128:
color = Color.PINK;
break;
case 256:
color = Color.MAGENTA;
break;
case 512:
color = Color.BLACK;
break;
case 1024:
color = Color.PURPLE;
break;
case 2048:
color = Color.GRAY;
break;
}
return color;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value > 0) {
setCellValueEx(y, x, color, Integer.toString(value));
} else {
setCellValueEx(y,x, color, "");
}
}
}