Второй день на одном месте сижу, не могу никак пройти валидацию. Может кто посмотрит из опытных и подскажет? Изучала и чужие коды этого метода у всех , пыталась подружить их с моим. Все только хуже становилось.((((
Может проблема в других методах, а валидатор пропустил...я уже не знаю...
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;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
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 int getMaxTileValue() {
int max = gameField[0][0];
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (max < gameField[y][x]) {
max = gameField[y][x];
}
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.GOLDENROD, "WIN", Color.BLACK, 100);
}
private boolean canUserMove() {
boolean loser = false;
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (gameField[y][x] > 0){
loser = true;}
if (!(gameField[y][x] == 0) || (gameField[y][x] == (gameField[y + 1][x + 1]))) {
loser = true;
}
}
}
return loser;
}
private void createNewNumber() {
if (getMaxTileValue() == 2048) {
win();
}
int x, y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
} while (gameField[y][x] != 0);
int randomNumber = getRandomNumber(10);
gameField[y][x] = randomNumber == 9 ? 4 : 2;
}
private Color getColorByValue(int value) {
switch (value) {
case 2:
return Color.AQUA;
case 4:
return Color.AQUAMARINE;
case 8:
return Color.PEACHPUFF;
case 16:
return Color.LAVENDER;
case 32:
return Color.PLUM;
case 64:
return Color.LIGHTBLUE;
case 128:
return Color.LIME;
case 256:
return Color.HOTPINK;
case 512:
return Color.KHAKI;
case 1024:
return Color.DARKVIOLET;
case 2048:
return Color.NAVY;
default:
return Color.WHITE;
}
}
private void setCellColoredNumber(int x, int y, int value) {
setCellValueEx(x, y, getColorByValue(gameField[y][x]), String.valueOf(value));
if (value == 0) {
setCellValueEx(x, y, getColorByValue(gameField[y][x]), "");
}
}
// Магия, не знаю как работает((
private boolean compressRow(int[] row) {
boolean left = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] == 0 && row[i] != row[i + 1]) {
left = true;
row[i] = row[i + 1];
row[i + 1] = 0;
}
}
if (left) {
compressRow(row);
}
return left;
}
public void onKeyPress(Key key) {
if (key == Key.LEFT) {
moveLeft();
} else if (key == Key.RIGHT) {
moveRight();
} else if (key == Key.DOWN) {
moveDown();
} else if (key == Key.UP) {
moveUp();
}
if (key == Key.LEFT || key == Key.RIGHT
|| key == Key.DOWN || key == Key.UP) {
drawScene();
}
}
private boolean mergeRow(int[] row) {
boolean ifChanged = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] != 0) {
if (row[i + 1] == row[i]) {
ifChanged = true;
row[i] = row[i] * 2;
row[i + 1] = 0;
}
}
}
return ifChanged;
}
private void rotateClockwise() {
int[][] temp = new int[SIDE][SIDE];
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
temp[i][j] = gameField[gameField.length - j - 1][i];
}
}
gameField = temp;
}
private void moveLeft() {
boolean isChanged = false;
int[] currentRow = new int[SIDE];
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
currentRow[j] = gameField[i][j];
}
if (compressRow(currentRow)) {isChanged = true;}
if (mergeRow(currentRow)) {
isChanged = true;
compressRow(currentRow);
}
for (int j = 0; j < SIDE; j++) {
gameField[i][j] = currentRow[j];
}
}
if (isChanged) {
createNewNumber();
}
}
private void moveRight() {
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveDown() {
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void moveUp() {
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
}