Thanks in advance for any help!
When I attempt to verify this task, it says the compressRow method accurately returns true and false. However, it says that the compressRow(int[]) method is not implemented in accordance with the task conditions. Unfortunately, the only error it gives me is that the task conditions are not met.
Because I am not able to use IntelliJ, I am manually testing my code in a REPL using the examples they give and they all seem to be making the correct modifications. I would appreciate any insight into in what way the task conditions are not being met.
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
@Override
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(j, i, gameField[i][j]);
}
}
}
private void createNewNumber() {
boolean isNewNumber = false;
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
while(!isNewNumber) {
if (gameField[y][x] == 0) {
isNewNumber = true;
} else {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
}
if (getRandomNumber(10) == 9) {
gameField[y][x] = 4;
} else {
gameField[y][x] = 2;
}
}
private Color getColorByValue(int value) {
switch(value) {
case 0:
return Color.WHITE;
case 2:
return Color.BLUE;
case 4:
return Color.LIGHTBLUE;
case 8:
return Color.RED;
case 16:
return Color.MEDIUMBLUE;
case 32:
return Color.PURPLE;
case 64:
return Color.PINK;
case 128:
return Color.LAVENDERBLUSH;
case 256:
return Color.ORANGE;
case 512:
return Color.GREEN;
case 1024:
return Color.YELLOW;
case 2048:
return Color.MEDIUMPURPLE;
default:
return Color.WHITE;
}
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value != 0) {
setCellValueEx(x, y, color, String.valueOf(value));
} else {
setCellValueEx(x, y, color, "");
}
}
private boolean compressRow(int[] row) {
boolean shifted = false;
for (int i = 0; i < SIDE - 1; i++) {
if (row[i] == 0) {
for (int j = i + 1; j < SIDE; j++) {
if (row[j] != 0) {
shifted = true;
row[j-1] = row[j];
row[j] = 0;
}
}
}
}
return shifted;
}
}