package com.javarush.task.jdk13.task53.task5301;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame(){
createNewNumber();
createNewNumber();
}
private void drawScene(){
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int a = getGameFieldValue(i,j);
setCellColoredNumber(i, j, a);
}
}
}
private int getGameFieldValue(int x, int y){
return gameField[y][x];
}
private void setCellColoredNumber(int x, int y, int value){
Color color = getColorByValue(value);
if(value == 0){
setCellValueEx(x, y, color, String.valueOf(value), color);
} else {
setCellValueEx(x, y, color, String.valueOf(value));
}
}
private Color getColorByValue(int value) {
switch (value) {
case 0:
return Color.WHITE;
case 2:
return Color.GREEN;
case 4:
return Color.YELLOW;
case 8:
return Color.BEIGE;
case 16:
return Color.AQUA;
case 32:
return Color.BROWN;
case 64:
return Color.BISQUE;
case 128:
return Color.BLUEVIOLET;
case 256:
return Color.BURLYWOOD;
case 512:
return Color.CADETBLUE;
case 1024:
return Color.CHOCOLATE;
case 2048:
return Color.CYAN;
default:
return Color.GRAY;
}
}
private void createNewNumber(){
int x;
int y;
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
while (gameField[x][y] != 0){
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
int number = getRandomNumber(10);
if(number < 9) {
gameField[x][y] = 2;
} else {
gameField[x][y] = 4;
}
}
}
package com.javarush.task.jdk13.task53.task5301;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame(){
createNewNumber();
createNewNumber();
}
private void drawScene(){
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int a = getGameFieldValue(i,j);
setCellColoredNumber(i, j, a);
}
}
}
private int getGameFieldValue(int x, int y){
return gameField[y][x];
}
private void setCellColoredNumber(int x, int y, int value){
Color color = getColorByValue(value);
if(value == 0){
setCellValueEx(x, y, color, String.valueOf(value), color);
} else {
setCellValueEx(x, y, color, String.valueOf(value));
}
}
private Color getColorByValue(int value) {
switch (value) {
case 0:
return Color.WHITE;
case 2:
return Color.GREEN;
case 4:
return Color.YELLOW;
case 8:
return Color.BEIGE;
case 16:
return Color.AQUA;
case 32:
return Color.BROWN;
case 64:
return Color.BISQUE;
case 128:
return Color.BLUEVIOLET;
case 256:
return Color.BURLYWOOD;
case 512:
return Color.CADETBLUE;
case 1024:
return Color.CHOCOLATE;
case 2048:
return Color.CYAN;
default:
return Color.GRAY;
}
}
private void createNewNumber(){
int x;
int y;
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
while (gameField[x][y] != 0){
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
int number = getRandomNumber(10);
if(number < 9) {
gameField[x][y] = 2;
} else {
gameField[x][y] = 4;
}
}
}
