Код работает, все видимые требования соблюдены, но валидатор не пропускает. В последнем пункте ментор пишет "Метод createNewNumber() не вызывает метод win(), если в матрице есть значение 2048"
package com.javarush.games.game2048;
import com.javarush.engine.cell.Color;
import com.javarush.engine.cell.Game;
import com.javarush.engine.cell.Key;
import java.util.LinkedList;
import java.util.List;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
private List<byte[]> zeroCells;
private boolean isGameStopped = false;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
zeroCells = findZeroTiles();
while (zeroCells.size() > 1) {
createNewNumber();
}
}
private void drawScene() {
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
setCellColoredNumber(j, i , gameField[i][j]);
}
}
}
private void createNewNumber() {
if (getMaxTileValue() == 2048) {
win();
}
int i = getRandomNumber(zeroCells.size());
byte[] cell = zeroCells.get(i);
int random = getRandomNumber(10);
if (random == 9) {
gameField[cell[0]][cell[1]] = 4;
} else {
gameField[cell[0]][cell[1]] = 2;
}
zeroCells.remove(i);
}
private List<byte[]> findZeroTiles() {
List<byte[]> values = new LinkedList<>();
for (int i = 0; i < gameField.length; i++) {
for (int j = 0; j < gameField[i].length; j++) {
if (gameField[i][j] == 0) {
values.add(new byte[2]);
values.get(values.size() - 1)[0] = (byte) i;
values.get(values.size() - 1)[1] = (byte) j;
}
}
}
return values;
}
private void setCellColoredNumber(int x, int y, int value) {
setCellValueEx(x, y, getColorByValue(value), (value == 0) ? "" : Integer.toString(value));
}
private Color getColorByValue(int value) {
switch (value) {
case 0 : return Color.WHITE;
case 2 : return Color.LAVENDERBLUSH;
case 4 : return Color.YELLOW;
case 8 : return Color.GREEN;
case 16 : return Color.DARKBLUE;
case 32 : return Color.BLUE;
case 64 : return Color.PURPLE;
case 128 : return Color.GREENYELLOW;
case 256 : return Color.BEIGE;
case 512 : return Color.CORAL;
case 1024 : return Color.DARKGREY;
case 2048 : return Color.CYAN;
default : return Color.NONE;
}
}
private boolean compressRow(int[] row) {
boolean changesWereMade = false;
int zeroIndex = -1;
for (int i = 0; i < row.length; i++) {
if (row[i] == 0 && zeroIndex == -1) {
zeroIndex = i;
} else if (row[i] != 0 && zeroIndex != -1) {
row[zeroIndex] = row[i];
row[i] = 0;
i = 0;
changesWereMade = true;
zeroIndex = -1;
}
}
return changesWereMade;
}
private boolean mergeRow(int[] row) {
boolean changesWereMade = false;
for (int i = 1; i < row.length; i++) {
if (row[i] != 0 && row[i - 1] == row[i]) {
row[i - 1] = row[i] * 2;
row[i] = 0;
changesWereMade = true;
}
}
return changesWereMade;
}
@Override
public void onKeyPress(Key key) {
switch (key) {
case UP:
moveUp();
drawScene();
break;
case DOWN:
moveDown();
drawScene();
break;
case RIGHT:
moveRight();
drawScene();
break;
case LEFT:
moveLeft();
drawScene();
break;
default:
drawScene();
}
}
private void moveLeft() {
boolean cellShouldBeAdded = false;
for (int[] ints : gameField) {
if (compressRow(ints) | mergeRow(ints)) {
compressRow(ints);
cellShouldBeAdded = true;
}
}
if (cellShouldBeAdded) {
zeroCells = findZeroTiles();
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[][] rotated = new int[SIDE][SIDE];
int column = rotated[0].length - 1;
for (int[] ints : gameField) {
int row = 0;
for (int anInt : ints) {
rotated[row][column] = anInt;
row++;
}
column--;
}
gameField = rotated;
}
private int getMaxTileValue() {
int max = 0;
for (int[] ints : gameField) {
for (int anInt : ints) {
max = Math.max(anInt, max);
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.NONE, "YOU WIN!", Color.BLUE, 80);
}
private void test1() {
}
}