createGame 23 - 27 строка
onKeyPress 131 - 163 строка
canUserMove 227 - 242 строка
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField;
private boolean isGameStopped = false;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame() {
gameField = new int[SIDE][SIDE];
createNewNumber();
createNewNumber();
}
private void win() {
isGameStopped = true;
showMessageDialog(Color.GREEN, " YOU WIN!!!1111 ", Color.WHITE, 30);
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.RED, " YOU LOOSE!!!1111 ", Color.WHITE, 30);
}
private void drawScene() {
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
setCellColoredNumber(x, y, gameField[y][x]);
}
}
}
private void createNewNumber() {
if (!isGameStopped) {
if (getMaxTileValue() == 2048) win();
if (canUserMove()) { ///------------------------------------------
List<Pair> list = getFreeCell();
if (list.size() > 0) {
int randomPair = getRandomNumber(list.size());
int num = getRandomNumber(10);
int x = list.get(randomPair).x;
int y = list.get(randomPair).y;
gameField[y][x] = num == 9 ? 4 : 2;
}
}
}
//printField();
}
private Color getColorByValue(int value) {
Color color;
switch (value) {
case 2: color = Color.LIGHTGRAY;
break;
case 4: color = Color.ANTIQUEWHITE;
break;
case 8: color = Color.LIGHTCORAL;
break;
case 16: color = Color.LIGHTCYAN;
break;
case 32: color = Color.LIGHTYELLOW;
break;
case 64: color = Color.YELLOW;
break;
case 128: color = Color.LIME;
break;
case 256: color = Color.LIMEGREEN;
break;
case 512: color = Color.LIGHTGOLDENRODYELLOW;
break;
case 1024: color = Color.GREENYELLOW;
break;
case 2048: color = Color.GOLD;
break;
default: color = Color.WHITE;
}
return color;
}
private void setCellColoredNumber(int x, int y, int value) {
setCellValueEx(x, y, getColorByValue(value), value == 0 ? "" : String.valueOf(value));
}
private boolean compressRow(int[] row) {
boolean flag = false;
int pos = -1;
for (int i = 0; i < row.length; i++ ){
if (row[i] == 0 && pos == -1) {
pos = i;
}
if (pos != -1 && row[i] != 0) {
row[pos] = row[i];
row[i] = 0;
i = pos;
pos = -1;
flag = true;
}
}
return flag;
}
private boolean mergeRow(int[] row) {
boolean flag = false;
for (int i = 1; i < row.length; i++) {
if (row[i] != 0) {
if (row[i - 1] != 0 && row[i - 1] == row[i]) {
row[i - 1] *= 2;
row[i] = 0;
flag = true;
}
}
}
return flag;
}
@Override
public void onKeyPress(Key key) {
if (isGameStopped) {
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
}
}
if (!isGameStopped) {
if (canUserMove()) {
switch (key) {
case LEFT:
moveLeft();
drawScene();
break;
case RIGHT:
moveRight();
drawScene();
break;
case UP:
moveUp();
drawScene();
break;
case DOWN:
moveDown();
drawScene();
break;
}
} else {
gameOver();
}
}
}
private void moveDown() {
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void moveUp() {
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void moveRight() {
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveLeft() {
boolean flag = false;
for (int[] arr : gameField) {
if (compressRow(arr)) {
flag = true;
}
if (mergeRow(arr)) {
flag = true;
}
if (compressRow(arr)) {
flag = true;
}
}
if (flag) {
createNewNumber();
}
}
private void rotateClockwise() {
int[][] arr = new int[SIDE][SIDE];
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
arr[x][SIDE - y - 1] = gameField[y][x];
}
}
gameField = arr;
}
private int getMaxTileValue() {
int max = 0;
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
max = gameField[y][x] > max ? gameField[y][x] : max;
}
}
return max;
}
private boolean canUserMove() {
boolean flag = false;
for (int y = 0; y < gameField.length && !flag; y++) {
for (int x = 0; x < gameField[y].length && !flag; x++) {
flag = (gameField[y][x] == 0);
if (!flag && x - 1 >= 0)
flag = gameField[y][x] == gameField[y][x - 1];
if (!flag && y - 1 >= 0)
flag = gameField[y][x] == gameField[y - 1][x];
if (!flag && x + 1 <= SIDE - 1)
flag = gameField[y][x] == gameField[y][x + 1];
if (!flag && y + 1 <= SIDE - 1)
flag = gameField[y][x] == gameField[y + 1][x];
}
}
return flag;
}
private void printField() {
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
System.out.print(gameField[y][x] + " ");
}
System.out.println();
}
System.out.println();
}
private class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
private List<Pair> getFreeCell(){
List<Pair> list = new ArrayList<>();
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
if (gameField[y][x] == 0) {
list.add(new Pair(x, y));
}
}
}
Collections.shuffle(list);
return list;
}
}