Не проходит проверку по последнему пункту. Как только не пробовал, т.е. разные варианты, даже посмотрел как другие делали и у них прошло. У меня же нет. Хотя все вроде работает правильно. Помогите разобраться.
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;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
@Override
public void onKeyPress(Key key) {
if (isGameStopped)
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
}
if (!isGameStopped && canUserMove()) {
if (key == Key.LEFT) {
moveLeft();
drawScene();
} else if (key == Key.RIGHT) {
moveRight();
drawScene();
} else if (key == Key.UP) {
moveUp();
drawScene();
} else if (key == Key.DOWN) {
moveDown();
drawScene();
}
}
else gameOver();
}
private void createGame() {
for (int x = 0; x < SIDE; x++)
for (int y = 0; y < SIDE; y++)
gameField[x][y] = 0;
createNewNumber();
createNewNumber();
}
private void drawScene() {
for (int x = 0; x < SIDE; x++)
for (int y = 0; y < SIDE; y++)
setCellColoredNumber(x, y, gameField[y][x]);
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value != 0) setCellValueEx(x, y, color, String.valueOf(value));
else setCellValueEx(x, y, color, "");
}
private int getMaxTileValue() {
int max = 0;
for (int x = 0; x < SIDE; x++)
for (int y = 0; y < SIDE; y++)
if (max < gameField[x][y])
max = gameField[x][y];
System.out.println(max);
return max;
}
private boolean canUserMove() {
int countZero = 0, countDveRyadom = 0;
for (int x = 0; x < SIDE; x++) {
int[] rowY = new int[SIDE];
for (int y = 0; y < SIDE; y++) {
if (gameField[x][y] == 0)
countZero++;
rowY[y] = gameField[y][x];
}
int[] rowX = gameField[x];
for (int i = 0; i < SIDE - 1; i++) {
if (rowX[i] != 0 && rowX[i] == rowX[i+1] ||
rowY[i] != 0 && rowY[i] == rowY[i+1])
countDveRyadom++;
}
}
if (countZero > 0 || countDveRyadom > 0) return true;
else return false;
}
private boolean compressRow(int[] row) {
int tmp;
boolean rez = false;
while (true) {
tmp = 0;
for (int i = 0; i < SIDE-1; i++) {
if (row[i] == 0 && row[i+1] != 0) {
row[i] = row[i+1];
row[i+1] = 0;
rez = true;
tmp = 1;
}
}
if (tmp == 0) break;
}
return rez;
}
private boolean mergeRow(int[] row) {
boolean rez = false;
for (int i = 0; i < SIDE - 1; i++) {
if (row[i] == row[i + 1] && row[i] != 0) {
rez = true;
row[i] = row[i] * 2;
row[i + 1] = 0;
}
}
return rez;
}
private void rotateClockwise() {
int[][] gameFieldRotate = new int[SIDE][SIDE];
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++)
gameFieldRotate[y][SIDE - 1 - x] = gameField[x][y];
}
for (int x = 0; x < SIDE; x++)
for (int y = 0; y < SIDE; y++)
gameField[x][y] = gameFieldRotate[x][y];
}
private void moveLeft() {
boolean compress = false, merge = false;
for (int x = 0; x < SIDE; x++) {
if (compressRow(gameField[x])) {
compress = true;
}
if (mergeRow(gameField[x])) {
merge = true;
compressRow(gameField[x]);
}
}
if (compress || merge) 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 Color getColorByValue(int value) {
Color color = Color.WHITE;
switch (value) {
case 0:
color = Color.WHITE; break;
case 2:
color = Color.GREEN; break;
case 4:
color = Color.BLUE; break;
case 8:
color = Color.GRAY; break;
case 16:
color = Color.AQUA; break;
case 32:
color = Color.AQUAMARINE; break;
case 64:
color = Color.LEMONCHIFFON; break;
case 128:
color = Color.GREENYELLOW; break;
case 256:
color = Color.SILVER; break;
case 512:
color = Color.ORANGE; break;
case 1024:
color = Color.OLIVE; break;
case 2048:
color = Color.HONEYDEW; break;
//default: color = Color.WHITE;
}
return color;
}
private void createNewNumber() {
if (getMaxTileValue() == 2048)
win();
int a = getRandomNumber(10);
int xField = getRandomNumber(SIDE);
int yField = getRandomNumber(SIDE);
if (gameField[xField][yField] == 0) {
if (a < 9) gameField[xField][yField] = 2;
else gameField[xField][yField] = 4;
}
else createNewNumber();
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.DARKBLUE, "YOU WIN!!!!", Color.GREEN, 100);
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.DARKBLUE, "GAME OVER.", Color.RED, 100);
}
}