for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (gameField[i][j] != 0)
isTrue = true;
}
}
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game{
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
private boolean isGameStopped = false;
public void initialize(){
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
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(){
if (getMaxTileValue() == 2048) win();
int x, y;
do {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
while (gameField[y][x] != 0);
gameField[y][x] = getRandomNumber(10) == 9 ? 4 : 2;
}
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 void setCellColoredNumber(int x, int y, int value){
Color color = getColorByValue(value);
if (value == 0) setCellValueEx(x,y,color,"");
else setCellValueEx(x,y,color,String.valueOf(value));
}
private boolean compressRow(int[] row){
boolean isChanged = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] == 0 && row[i] != row[i + 1]) {
isChanged = true;
row[i] = row[i + 1];
row[i + 1] = 0;
}
}
if (isChanged) {
compressRow(row);
}
return isChanged;
}
private boolean mergeRow(int[] row){
boolean isChanged = false;
for (int i = 0; i < row.length - 1; i++) {
if(row[i] == row[i + 1] && row[i] != 0){
isChanged = true;
row[i] *= 2;
row[i + 1] = 0;
}
}
return isChanged;
}
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 moveLeft(){
boolean isChanged = false;
for (int x = 0; x < SIDE; x++){
int[] row = gameField[x];
if (compressRow(row)) {
isChanged = true;
}
if (mergeRow(row)){
isChanged = true;
}
if (compressRow(row)){
isChanged = true;
}
}
if (isChanged){
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 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 int getMaxTileValue(){
int time = 0;
for (int x = 0; x < SIDE; x++){
for (int y = 0; y < SIDE; y++){
if (time < gameField[x][y]){
time = gameField[x][y];
}
}
}
return time;
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.AQUA, "YOU WIN!", Color.BLACK, 15);
}
private boolean canUserMove(){
boolean isTrue = false;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (gameField[i][j] != 0)
isTrue = true;
}
}
for (int i = 0; i < gameField.length; i++) {
for (int j = 1; j < gameField.length; j++) {
if (gameField[i][j] == gameField[i][j-1])
isTrue = true;
}
}
for (int i = 0; i < gameField.length; i++) {
for (int j = 1; j < gameField.length; j++) {
if (gameField[j][i] == gameField[j-1][i])
isTrue = true;
}
}
return isTrue;
}
}