Hey, what´s the problem here?
The programm is working, it is displaying sometimes 2, sometimes 4 in random cells.
I am calling twice the getRandomNumber(SIDE) and once the getRandomNumber(10).
Any help would be appreciated! :)
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
import javax.naming.SizeLimitExceededException;
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 x=0; x < SIDE; x++) { //set color of every cell
for (int y=0; y < SIDE; y++) {
setCellColor(x, y, Color.WHITE);
}
}
}
private void createNewNumber() {
int x = getRandomNumber(SIDE); //Choose a random cell
int y = getRandomNumber(SIDE);
if (gameField[x][y] != 0) {
createNewNumber(); //rerun method, if cell is not empty
}
if (getRandomNumber(10) == 9) gameField[x][y] = 4; //10% chance for a 4
else gameField[x][y] = 2; //90% chance for a 2
setCellValue(x, y, Integer.toString(gameField[x][y]));
}
}