Здравствуйте!
Не запускается, не знаете в чем причина? Хоть и пишут, что задача решена, а она нифига не работает
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;
private int[][] gameField = new int[SIDE][SIDE];
private int score;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
gameField = new int[SIDE][SIDE];
createNewNumber();
createNewNumber();
}
private void drawScene() {
for(int i = 0; i < SIDE; i++) {
for(int j = 0; j < SIDE; j++) {
int value = gameField [j][i];
setCellColoredNumber(i, j, value);
}
}
}
private void createNewNumber() {
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
if(gameField[x][y] == 0) {
if(getRandomNumber(10) == 9) {
gameField[x][y] = 4;
} else {
gameField[x][y] = 2;
}
} else {
createNewNumber();
}
if(getMaxTileValue() == 2048) {
win();
}
}
private Color getColorByValue(int value) {
switch(value) {
case 2 : return Color.PINK;
case 4 : return Color.VIOLET;
case 8 : return Color.INDIGO;
case 16 : return Color.BLUE;
case 32 : return Color.GREEN;
case 64 : return Color.SALMON;
case 128 : return Color.ORANGE;
case 256 : return Color.CORAL;
case 512 : return Color.ORCHID;
case 1024 : return Color.MAGENTA;
case 2048 : return Color.CHOCOLATE;
default : return Color.WHITE;
}
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
String string;
if(value == 0) {
string = "";
} else {
string = String.valueOf(value);
}
setCellValueEx(x, y, color, string);
}
private boolean compressRow(int[] row) {
for(int i = 0; i < row.length - 1; i++) {
if(row[i] == 0 && row[i + 1] > 0) {
row[i] = row[i + 1];
row[i + 1] = 0;
compressRow(row);
return true;
}
}
return false;
}
private boolean mergeRow(int[] row) {
int[] clone = row.clone();
boolean checkChange = false;
for(int i = 0; i < row.length - 1; i++) {
if(row[i] != 0 && row[i] == row[i + 1]) {
row[i] = row[i] + row[i + 1];
row [i + 1] = 0;
setScore(score += row[i]);
checkChange = true;
}
}
return checkChange;
}
public void onKeyPress(Key key) {
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
score = 0;
setScore(score);
return;
}
if (!canUserMove()) {
gameOver();
} else {
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 b = false;
for(int i = 0; i < SIDE; i++){
if(compressRow(gameField[i])) {
b = true;
}
if(mergeRow(gameField[i])) {
compressRow(gameField[i]);
b = true;
}
}
if(b) {
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[i][j] = gameField[SIDE - (1 + j)][i];
}
}
gameField = temp.clone();
}
private int getMaxTileValue() {
int maxValue = 0;
for(int i = 0; i < SIDE; i++) {
for(int j = 0; j < SIDE; j++) {
if(maxValue < gameField[i][j]) {
maxValue = gameField[i][j];
}
}
}
return maxValue;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.WHITE, "You win!", Color.BLACK, 24);
}
private boolean canUserMove() {
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (gameField[i][j] == 0) {
return true;
} if (j == SIDE - 1 && i != SIDE -1 && gameField[i][j] == gameField[i + 1][j]) {
return true;
} if (i == SIDE - 1 && j != SIDE -1 && gameField[i][j] == gameField[i][j + 1]) {
return true;
} if (i <= SIDE - 2 && j <= SIDE - 2 &&
(gameField[i][j] == gameField[i][j + 1] || gameField[i][j] == gameField[i + 1][j])) {
return true;
}
}
}
return false;
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.WHITE, "You lose", Color.BLACK, 24);
}
}
Александр П.
2 уровень
Возникла ошибка с кодом: APP_PUBLISH_ERROR
Решен
Комментарии (1)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Пётр Шайдуров
4 декабря 2020, 11:50решение
скорее всего у тебя та же проблема - методе clone() нужно убрать
+2