Что я не учла? Почему не проходит? 🥴
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<gameField.length; i++) {
for (int j = 0; j<gameField.length; j++) {
setCellColoredNumber(i, j, gameField[j][i]);
}
}
}
private void createNewNumber() {
int x;
int y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
while (gameField[y][x] != 0);
int randomNumber = getRandomNumber(10);
if (randomNumber == 9) {
gameField[y][x] = 4;
}
else {
gameField[y][x] = 2;
}
getMaxTileValue();
for (int i=0; i<gameField.length; i++) {
for (int j=0; j<gameField.length; j++) {
if (gameField[i][j] == 2048) {
win();
}
}
}
}
private Color getColorByValue (int value) {
switch (value) {
case 2:
return Color.LIGHTPINK;
case 4:
return Color.INDIGO;
case 8:
return Color.ROYALBLUE;
case 16:
return Color.MEDIUMAQUAMARINE;
case 32:
return Color.MEDIUMSEAGREEN;
case 64:
return Color.LIME;
case 128:
return Color.ORANGE;
case 256:
return Color.SALMON;
case 512:
return Color.RED;
case 1024:
return Color.MAGENTA;
case 2048:
return Color.MAROON;
default:
return Color.MEDIUMSPRINGGREEN;
}
}
private void setCellColoredNumber (int x, int y, int value) {
if(value == 0) {
setCellValueEx(x, y, getColorByValue(value), "");}
else {
setCellValueEx(x, y, getColorByValue(value), String.valueOf(value));
}
}
private boolean compressRow(int[] row) {
boolean change = false;
for (int i = 0; i<row.length-1; i++) {
if(row[i] == 0 && row[i] != row[i+1]) {
row[i]=row[i+1];
row[i+1]=0;
change = true;
}
}
if(change) {compressRow(row);}
return change;
}
private boolean mergeRow(int[] row) {
boolean sum = false;
for (int i = 0; i<row.length-1; i++) {
if (row[i]==row[i+1] && row[i] != 0) {
row[i] = row[i] + row[i+1];
row[i+1] = 0;
sum=true;
}
}
return sum;
}
public void onKeyPress(Key key) {
if(key==Key.LEFT) {
moveLeft();
drawScene();
}
if(key==Key.RIGHT) {
moveRight();
drawScene();
}
if(key==Key.UP) {
moveUp();
drawScene();
}
if(key==Key.DOWN) {
moveDown();
drawScene();
}
}
private void moveLeft() {
boolean move = false;
for (int i=0; i<gameField.length; i++){
if (compressRow(gameField[i])) {move = true;}
if (mergeRow(gameField[i])) {move = true;}
if (compressRow(gameField[i])) {move = true;}
}
if (move) {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[][] rotateField = new int[SIDE][SIDE];
for (int i=0; i<SIDE; i++) {
for (int j=0; j<SIDE; j++) {
rotateField[i][j] = gameField[rotateField.length-1-j][i];
}
}
gameField = rotateField;
}
private int getMaxTileValue() {
int max = gameField[0][0];
for (int i=0; i<gameField.length; i++) {
for (int j=0; j<gameField.length; j++) {
if (max < gameField[i][j]) {
max = gameField[i][j];
}
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.CRIMSON, "You win!", Color.WHITE, 24);
}
private boolean canUserMove() {
for (int i=0; i<SIDE; i++) {
for (int j=0; j<SIDE; j++) {
if (gameField[i][j] == 0){
isGameStopped = true;}}}
for (int i=0; i<SIDE-1; i++) {
for (int j=0; j<SIDE-1; j++) {
if (gameField[i][j] == gameField[i+1][j] || gameField[i][j] == gameField[i][j+1]) {
isGameStopped = true;}}}
return isGameStopped;
}
}