Не могу в чем проблема. Сделал вроде правильно (не уверен) но валидатор не пропускает.
private void moveLeft() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
if (x == 4);
else if (gameField[y][x] == gameField[y][x + 1]) {
gameField[y][x] = gameField[y][x] + gameField[y][x + 1];
gameField[y][x + 1] = 0;
}
while (gameField[y][0] == 0) {
gameField[y][0] = gameField[y][1];
gameField[y][3] = 0;
}
}
}
drawScene();
}
Рекомендация от ментора:
Проверь, что метод onKeyPress(Key) вызывает метод moveLeft() при нажатии клавиши влево.package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private int[][] gameField =
{{2, 4, 8, 16},
{32, 64, 128, 256},
{512, 1024, 2048, 0},
{32, 64, 128, 16}};
private void createGame() {
createNewNumber();
createNewNumber();
}
private void drawScene() {
for (int x=0;x<SIDE;x++) {
for (int y = 0; y < SIDE; y++) {
setCellColoredNumber(x, y, gameField[y][x]);
}
}
}
private void createNewNumber(){
int x, y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
} while (gameField[y][x] != 0);
if (getRandomNumber(10) == 9)
gameField[y][x] = 4;
else
gameField[y][x] = 2;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(gameField[y][x]);
if (value == 0) setCellValueEx(x,y,color,"");
else setCellValueEx(x,y,color,String.valueOf(value));
}
private Color getColorByValue(int value) {
Color color = Color.WHITE;
if (value == 2) color = Color.PINK;
if (value == 4) color = Color.VIOLET;
if (value == 8) color = Color.INDIGO;
if (value == 16) color = Color.BLUE;
if (value == 32) color = Color.GREEN;
if (value == 64) color = Color.SALMON;
if (value == 128) color = Color.ORANGE;
if (value == 256) color = Color.CORAL;
if (value == 512) color = Color.ORCHID;
if (value == 1024) color = Color.MAGENTA;
if (value == 2048) color = Color.CHOCOLATE;
return color;
}
private boolean compressRow(int[] row) {
int[] newRow = new int[row.length];
int newCount = 0;
int count = 0;
boolean res = false;
for (int element:row){
if (element>0) {
newRow[newCount] = row[count];
newCount++;
count++;
} else {
count++;
}
}
for (int i=0;i<row.length;i++){
if (row[i]!=newRow[i]) res = true;
row[i] = newRow[i];
}
row = newRow;
return res;
}
private boolean mergeRow(int[] row) {
boolean result = false;
for (int i = 0; i < row.length - 1; i++)
{
if (row[i] == row[i + 1] && row[i] != 0)
{
row[i] += row[i + 1];
row[i + 1] = 0;
result = true;
}
}
return result;
}
public void onKeyPress(Key key) {
if (key == Key.LEFT)
moveLeft();
else if (key == Key.RIGHT)
moveRight();
else if (key == Key.UP)
moveUp();
else if (key == Key.DOWN)
moveDown();
}
private void moveLeft() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
if (gameField[y][x] == gameField[y][x + 1]) {
gameField[y][x] = gameField[y][x] + gameField[y][x + 1];
gameField[y][x + 1] = 0;
}
while (gameField[y][0] == 0) {
gameField[y][0] = gameField[y][1];
}
}
}
drawScene();
}
private void moveRight() {
for (int y = 0; y < SIDE; y++) {
for (int x = SIDE; x < SIDE; x--) {
if (gameField[y][x] == gameField[y][x - 1]) {
gameField[y][x - 1] = gameField[y][x] + gameField[y][x - 1];
gameField[y][x] = 0;
}
while (gameField[y][4] == 0) {
gameField[y][4] = gameField[y][3];
}
}
}
drawScene();
}
private void moveUp() {
}
private void moveDown() {
}
}