Ошибка в логике к 3-му пункту?
Метод CanUserMove() - в самом низу.
Перепроверил, вроде бы всё в порядке..
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
import javafx.geometry.Side;
import java.util.Arrays;
import java.util.Collections;
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();
/*gameField = new int[][]{
{2, 4, 8, 16},
{32, 64, 128, 256},
{512, 1024, 2048, 0},
{2, 4, 8, 16}
};*/
drawScene();
}
private void createGame() {
createNewNumber();
createNewNumber();
}
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() {
if(getMaxTileValue() == 2048){
win();
}
while (true) {
int a = getRandomNumber(SIDE);
int b = getRandomNumber(SIDE);
if (gameField[a][b] == 0) {
if (getRandomNumber(10) == 9) {
gameField[a][b] = 4;
} else {
gameField[a][b] = 2;
}
break;
}
}
}
private Color getColorByValue(int value) {
Color color = Color.LIGHTGRAY;
switch (value) {
case (0):
color = Color.LIGHTGRAY;
break;
case (2):
color = Color.LIGHTBLUE;
break;
case (4):
color = Color.LIGHTCORAL;
break;
case (8):
color = Color.LIGHTSEAGREEN;
break;
case (16):
color = Color.LIGHTGREEN;
break;
case (32):
color = Color.LIGHTSKYBLUE;
break;
case (64):
color = Color.LIGHTYELLOW;
break;
case (128):
color = Color.LIGHTGOLDENRODYELLOW;
break;
case (256):
color = Color.LIGHTCYAN;
break;
case (512):
color = Color.LIGHTPINK;
break;
case (1024):
color = Color.OLIVE;
break;
case (2048):
color = Color.DARKOLIVEGREEN;
break;
}
return color;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
String str = Integer.toString(value);
if (value == 0) {
setCellValueEx(x, y, color, "");
} else {
setCellValueEx(x, y, color, str);
}
}
private boolean compressRow(int[] row) {
boolean isChanged = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] == 0 && row[i] != row[i + 1]) {
isChanged = true;
row[i] = row[i + 1];
row[i + 1] = 0;
}
}
if (isChanged) {
compressRow(row);
}
return isChanged;
}
private boolean mergeRow(int[] row) {
boolean isChanged = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] != 0 && row[i] == row[i + 1]) {
row[i] *= 2;
row[i + 1] = 0;
isChanged = true;
}
/*else if(row[i] != 0 && row[i + 1] == 0){
for(int j = i +2; j < row.length; j++){
if(row[i] == row[j]){
row[i] *= 2;
row[j] = 0;
isChanged = true;
}
else if (row[i] != row[j] && row[j] != 0){
break;
}
}
}*/
}
return isChanged;
}
@Override
public void onKeyPress(Key key) {
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();
}
}
private void moveLeft() {
int counter = 0;
boolean flag = false;
for (int i = 0; i < SIDE; i++) {
flag = compressRow(gameField[i]);
if (flag == true)
counter++;
}
for (int i = 0; i < SIDE; i++) {
flag = mergeRow(gameField[i]);
if (flag == true)
counter++;
}
for (int i = 0; i < SIDE; i++) {
flag = compressRow(gameField[i]);
if (flag == true)
counter++;
}
if (counter > 0) {
createNewNumber();
}
/*boolean create = false;
for (int y = 0; y < SIDE; y++) {
if (compressRow(gameField[y])) create = true;
if (mergeRow(gameField[y])) create = true;
if (compressRow(gameField[y])) create = true;
}
if (create) 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[][] temp = new int[SIDE][SIDE];
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
temp[j][SIDE - 1 - i] = gameField[i][j];
}
}
gameField = temp;
}
private int getMaxTileValue(){
int a = gameField[0][0];
for(int i = 0; i < SIDE; i ++){
for(int j = 0; j < SIDE; j++){
a = Math.max(gameField[i][j], a);
}
}
return a;
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.RED, "You think, thats u Win", Color.BLACK, 20);
}
private boolean canUserMove(){
boolean Flag2 = false;
for(int[] i : gameField){
for(int k : i ){
if(k == 0){
return true;
}
}
}
for (int i = 0; i < SIDE-1; i++){
for(int j = 0; j < SIDE-1; j++) {
if (gameField[i][j] == gameField[i][j + 1]) {
return true;
} else if (gameField[i][j] == gameField[i + 1][j]) {
return true;
}
}
if(gameField[i][SIDE] == gameField[i+1][SIDE]){
return true;
}
if(gameField[SIDE][i] == gameField[SIDE][i+1]){
return true;
}
}
return false;
}
}