Логика вроде правильная. Проверял всё работает но кимпилятор отказывается принимать по второму пункту. Проблема в методе canUserMove().
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private boolean isGameStopped = false;
@Override
public void initialize() {
super.initialize();
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private int[][] gameField = new int[SIDE][SIDE];
private void createGame() {
createNewNumber();
createNewNumber();
}
private void drawScene() {
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
setCellColoredNumber(j, i, gameField[i][j]);
}
}
}
private void createNewNumber() {
if (getMaxTileValue() == 2048)
win();
int x, y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
} while (gameField[y][x] != 0);
if (getRandomNumber(10) == 9)
gameField[y][x] = 4;
else
gameField[y][x] = 2;
}
private Color getColorByValue(int value) {
Color color = Color.AZURE;
if (value == 2)
color = Color.ALICEBLUE;
else if (value == 4)
color = Color.ANTIQUEWHITE;
else if (value == 8)
color = Color.AQUA;
else if (value == 16)
color = Color.AQUAMARINE;
else if (value == 32)
color = Color.BEIGE;
else if (value == 64)
color = Color.CORAL;
else if (value == 128)
color = Color.BISQUE;
else if (value == 256)
color = Color.BLANCHEDALMOND;
else if (value == 512)
color = Color.BLUE;
else if (value == 1024)
color = Color.BROWN;
else if (value == 2048)
color = Color.BURLYWOOD;
return color;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value == 0) setCellValueEx(x, y, color, "");
else setCellValueEx(x, y, color, String.valueOf(value));
}
private boolean compressRow(int[] row) {
boolean move = false;
for (int i = row.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (row[j] == 0 && row[j + 1] != 0) {
int tmp = row[j];
row[j] = row[j + 1];
row[j + 1] = tmp;
move = true;
}
}
}
return move;
}
private boolean mergeRow(int[] row) {
boolean merge = false;
for (int j = 0; j < 3; j++) {
if (row[j] == row[j + 1] && row[j] != 0) {
row[j] = row[j] + row[j + 1];
row[j + 1] = 0;
merge = true;
}
}
return merge;
}
@Override
public void onKeyPress(Key key) {
super.onKeyPress(key);
if (key == Key.RIGHT) {
moveRight();
drawScene();
} else if (key == Key.LEFT) {
moveLeft();
drawScene();
} else if (key == Key.DOWN) {
moveDown();
drawScene();
} else if (key == Key.UP) {
moveUp();
drawScene();
}
}
private void moveLeft() {
int count = 0;
for (int j = 0; j < SIDE; j++) {
if (compressRow(gameField[j]))
count++;
if (mergeRow(gameField[j]))
count++;
if (compressRow(gameField[j]))
count++;
}
if (count > 0)
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[][] field = new int[SIDE][SIDE];
for (int i = 3, k = 0; i >= 0; i--, k++) {
for (int j = 0; j < 4; j++) {
field[j][k] = gameField[i][j];
}
}
gameField = field;
}
private int getMaxTileValue() {
int max = gameField[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (max < gameField[i][j])
max = gameField[i][j];
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.AQUA, "YOU WIN", Color.BLACK, 14);
}
private boolean canUserMove() {
boolean canMove = false;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (gameField[i][j] == 0)
canMove = true;
for (int k = 0; k < 3; k++) {
if (gameField[i][k] == gameField[i][k + 1])
canMove = true;
rotateClockwise();
if (gameField[i][k] == gameField[i][k + 1])
canMove = true;
}
}
}
return canMove;
}
}