Игра заускается и отлично работает в IDE, выполнил все подзадачи, валидатор все принял, опубликовал, попробовал сыграть через сайт, но игра не работает. Отображено стартовое положение поля, но на кнопки управления не реагирует.
ссылка на игру на сайте: https://javarush.com/projects/apps/139341#discussion
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
private boolean isGameStopped = false;
private int score = 0;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
gameField = new int[SIDE][SIDE];
score = 0;
setScore(0);
createNewNumber();
createNewNumber();
drawScene();
}
private void drawScene() {
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
setCellColoredNumber(i, j, gameField[j][i]);
}
}
}
private void createNewNumber() {
int x;
int y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
} while (gameField[x][y] != 0);
int posib = getRandomNumber(10);
if (posib == 9) gameField[x][y] = 4;
else gameField[x][y] = 2;
int max = getMaxTileValue();
if (max == 2048) win();
}
private Color getColorByValue(int value) {
switch (value) {
case 0:
return Color.AZURE;
case 2:
return Color.TAN;
case 4:
return Color.FIREBRICK;
case 8:
return Color.ORANGE;
case 16:
return Color.SADDLEBROWN;
case 32:
return Color.TEAL;
case 64:
return Color.SALMON;
case 128:
return Color.HONEYDEW;
case 256:
return Color.THISTLE;
case 512:
return Color.FLORALWHITE;
case 1024:
return Color.OLDLACE;
case 2048:
return Color.TOMATO;
}
return null;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value != 0) setCellValueEx(x, y, color, Integer.toString(value));
else setCellValueEx(x, y, color, "");
}
private boolean compressRow(int[] row) {
boolean checkChange = false;
for (int a = 0; a < SIDE; a++) {
for (int i = SIDE - 1; i > 0; i--) {
if (row[i] > 0 && row[i - 1] == 0) {
row[i - 1] = row[i];
row[i] = 0;
checkChange = true;
}
}
}
return checkChange;
}
private boolean mergeRow(int[] row) {
boolean checkChange = false;
for (int i = 0; i < SIDE - 1; i++) {
if (row[i] > 0 && row[i + 1] == row[i]) {
row[i] = row[i] * 2;
row[i + 1] = 0;
score = score + row[i];
setScore(score);
checkChange = true;
}
}
return checkChange;
}
@Override
public void onKeyPress(Key key) {
if (canUserMove() == false) {
gameOver();
}
switch (key) {
case UP:
if(isGameStopped) break;
moveUp();
drawScene();
break;
case DOWN:
if(isGameStopped) break;
moveDown();
drawScene();
break;
case LEFT:
if(isGameStopped) break;
moveLeft();
drawScene();
break;
case RIGHT:
if(isGameStopped) break;
moveRight();
drawScene();
break;
case SPACE:
if(isGameStopped){
isGameStopped = false;
createGame();
drawScene();
}
}
}
private void moveLeft() {
boolean wasChange = false;
for (int[] row : gameField) {
if (compressRow(row)) wasChange = true;
if (mergeRow(row)) wasChange = true;
if (compressRow(row)) wasChange = true;
}
if (wasChange) createNewNumber();
}
private void moveRight() {
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveUp() {
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void moveDown() {
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void rotateClockwise() {
int[][] tmp = new int[SIDE][SIDE];
for (int i = 0; i < 4; i++) {
for (int k = 3, f = 0; f < 4; k--, f++) {
tmp[i][k] = gameField[f][i];
}
}
gameField = tmp;
}
private int getMaxTileValue() {
int max = Integer.MIN_VALUE;
for (int[] a : gameField) {
for (int b : a) {
if (b > max) max = b;
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.BISQUE, "You Win", Color.BLANCHEDALMOND, 70);
}
private boolean canUserMove() {
boolean can = false;
for (int a = 0; a < SIDE; a++) {
for (int b = 0; b < SIDE; b++) {
if (gameField[a][b] == 0) can = true;
try {
if (gameField[a + 1][b] == gameField[a][b]) can = true;
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
if (gameField[a - 1][b] == gameField[a][b]) can = true;
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
if (gameField[a][b + 1] == gameField[a][b]) can = true;
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
if (gameField[a][b - 1] == gameField[a][b]) can = true;
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
return can;
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.BISQUE, "Game Over", Color.BLACK, 70);
}
}