Hi,
The last 2 conditions are not met, but I can clearly see it is working. I debugged it, the message was displayed, is there something wrong with the test that was written for this one or did I do something wrong?
The createNewNumber() method must call the getMaxTileValue() method.
The win() method must be called in the createNewNumber() method if the value 2048 is in the matrix.
private static final int VICTORY_VALUE = 2048;
(...)
private void createNewNumber() {
if (getMaxTileValue() == VICTORY_VALUE) {
win();
}
(...)
private void win() {
showMessageDialog(Color.GREEN, "WINNER WINNER CHICKEN DINNER", Color.WHITE, 12);
isGameStopped = true;
}
package com.codegym.games.game2048;
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import com.codegym.engine.cell.Key;
import java.util.Arrays;
public class Game2048 extends Game {
private static final int SIDE = 4;
private static final int VICTORY_VALUE = 2048;
private int[][] gameField = new int[SIDE][SIDE];
private boolean isGameStopped = false;
@Override
public void initialize() {
super.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 void createNewNumber() {
if (getMaxTileValue() == VICTORY_VALUE) {
win();
}
int x;
int y;
do {
x = getRandomNumber(SIDE); //Choose a random cell
y = getRandomNumber(SIDE);
} while (gameField[x][y] != 0); //which should be empty
int chance = getRandomNumber(10);
if (chance == 9) gameField[x][y] = 4; //10% chance for a 4
else gameField[x][y] = 2; //90% chance for a 2
setCellValue(x, y, Integer.toString(gameField[x][y]));
}
private Color getColorByValue(int value) {
if (value == 0) {
return Color.WHITE;
} else if (value == 2) {
return Color.ALICEBLUE;
} else if (value == 4) {
return Color.ANTIQUEWHITE;
} else if (value == 8) {
return Color.AQUA;
} else if (value == 16) {
return Color.AQUAMARINE;
} else if (value == 32) {
return Color.AZURE;
} else if (value == 64) {
return Color.BEIGE;
} else if (value == 128) {
return Color.BISQUE;
} else if (value == 256) {
return Color.BLACK;
} else if (value == 512) {
return Color.BLANCHEDALMOND;
} else if (value == 1024) {
return Color.BLUE;
} else if (value == 2048) {
return Color.BLUEVIOLET;
} else return Color.WHITE;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
if (value != 0) {
setCellValueEx(x, y, color, Integer.toString(value));
} else {
setCellValueEx(x, y, color, "");
}
}
private boolean compressRow(int[] row) {
int[] newRow = {0,0,0,0};
int i = 0;
for (int x = 0; x < SIDE; x++) {
if (row[x] != 0 && i < SIDE) {
newRow[i] = row[x];
++i;
}
}
boolean rowChanged = Arrays.equals(row, newRow);
for (int x = 0; x < SIDE; x++) {
row[x] = newRow[x];
}
return !rowChanged;
}
private boolean mergeRow(int[] row) {
boolean rowChanged = false;
for (int x = 0; x < SIDE -1; x++) {
if (row[x] != 0 && row[x+1] == row[x]) {
row[x] = row[x] + row[x+1];
row[x+1] = 0;
if (!rowChanged) {
rowChanged = true;
}
}
}
return rowChanged;
}
@Override
public void onKeyPress(Key key) {
switch (key) {
case UP:
moveUp();
break;
case DOWN:
moveDown();
break;
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
default:
return;
}
drawScene();
}
private void moveRight() {
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveLeft() {
boolean rowCompressed = false;
boolean rowReCompressed = false;
boolean rowMerged = false;
int moveCounter = 0;
for (int y = 0; y < SIDE; y++) {
rowCompressed = compressRow(gameField[y]);
rowMerged = mergeRow(gameField[y]);
rowReCompressed = compressRow(gameField[y]);
if (rowCompressed || rowReCompressed || rowMerged) {
moveCounter++;
}
}
if (moveCounter != 0) {
createNewNumber();
}
}
private void moveDown() {
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void moveUp() {
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void rotateClockwise() {
for (int y = 0; y <SIDE; y++) {
for (int x = y; x <SIDE; x++) {
int tempFieldValue = gameField[y][x];
gameField[y][x] = gameField[x][y];
gameField[x][y] = tempFieldValue;
}
}
for (int y = 0; y <SIDE; y++) {
for (int x = 0; x <SIDE/2; x++) {
int tempFieldValue = gameField[y][x];
gameField[y][x] = gameField[y][SIDE-1-x];
gameField[y][SIDE-1-x] = tempFieldValue;
}
}
}
private int getMaxTileValue() {
int maxTileValue = 0;
for (int y = 0; y < SIDE ; y++) {
for (int x = 0; x < SIDE; x++) {
if(maxTileValue < gameField[y][x]) {
maxTileValue = gameField[y][x];
}
}
}
return maxTileValue;
}
private void win() {
showMessageDialog(Color.GREEN, "WINNER WINNER CHICKEN DINNER", Color.WHITE, 12);
isGameStopped = true;
}
}