Код:
private boolean canUserMove() {
boolean result = true;
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (gameField[x][y] == 0)
return true;
}
}
return true;
}
Рисует галочку на
canUserMove() должен возвращать true, если нулевых элементов нет, но в матрице gameField есть хотя бы две соседние клетки с одинаковым значением (по горизонтали или вертикали).
хотя по идеи никакой проверки нет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;
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;
if (getMaxTileValue() == 2048)
win();
}
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();
drawScene();
}
else if (key == Key.RIGHT) {
moveRight();
drawScene();
}
else if (key == Key.UP) {
moveUp();
drawScene();
}
else if (key == Key.DOWN) {
moveDown();
drawScene();
}
}
private void rotateClockwise() {
int n = SIDE;
int[][] newField = new int[n][n];
for (int i = 0; i < n/2; i++) {
for (int j = i; j < n-i-1; j++) {
int tmp = gameField[i][j];
newField[i][j]=gameField[n-j-1][i];
newField[n-j-1][i]=gameField[n-i-1][n-j-1];
newField[n-i-1][n-j-1]=gameField[j][n-i-1];
newField[j][n-i-1]=tmp;
}
}
gameField = newField;
}
private void moveLeft() {
int methodCounter = 0;
for(int y = 0; y < SIDE; y++)
{
if(compressRow(gameField[y]))
methodCounter++;
}
for(int y = 0; y < SIDE; y++)
{
if(mergeRow(gameField[y]))
methodCounter++;
}
for(int y = 0; y < SIDE; y++)
{
if(compressRow(gameField[y]))
methodCounter++;
}
if (methodCounter > 0)
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 int getMaxTileValue() {
int max = gameField[0][0];
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (gameField[x][y] > max)
max = gameField[x][y];
}
}
return max;
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.DARKGREEN, "EZ WIN", Color.BLACK, 50);
}
private boolean canUserMove() {
boolean result = true;
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (gameField[x][y] == 0)
return true;
}
}
return true;
// boolean nearPair = false;
// for (int x = 0; x < SIDE; x++) {
// for (int y = 0; y < SIDE; y++) {
// if (gameField[x][y] == gameField[x + 1][y] || gameField[x][y] == gameField[x - 1][y] || gameField[x][y] == gameField[x][y + 1] || gameField[x][y] == gameField[x][y - 1])
// nearPair = true;
// }
// }
}
}