?
/*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];
@Override
public void 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[i][j]);
}
}
// Метод drawScene() должен вызывать метод setCellColoredNumber(int, int, int).
}
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;
}
private Color getColorByValue(int value) {
Color color = null;
if (value == 0) color = Color.SILVER;
if (value == 2) color = Color.WHITE;
if (value == 4) color = Color.AQUA;
if (value == 8) color = Color.BLUE;
if (value == 16) color = Color.ROSYBROWN;
if (value == 32) color = Color.AQUAMARINE;
if (value == 64) color = Color.YELLOW;
if (value == 128) color = Color.DIMGRAY;
if (value == 256) color = Color.LAVENDER;
if (value == 512) color = Color.LIGHTSTEELBLUE;
if (value == 1024) color = Color.GRAY;
if (value == 2048) color = Color.RED;
return color;
}
private void setCellColoredNumber(int x, int y, int value) {
//Метод setCellColoredNumber(int, int, int) не должен отображать значение плитки на экране, если оно равно 0.
if (value != 0) {
setCellValueEx(y, x, getColorByValue(value), Integer.toString(value));
} else {
setCellValueEx(y, x, getColorByValue(value), "");
}
}
*//*private void setCellColoredNumber (int x, int y, int value) {
String valueToString = value == 0 ? "" : String.valueOf(value);
setCellValueEx(x, y, getColorByValue(value), valueToString);
}*//*
}*/
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];
@Override
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++) {
setCellColoredNumber(i, j, gameField[i][j]);
}
}
private void createNewNumber() {
int x;
int y;
while (true) {
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
if (gameField[y][x] == 0)
break;
}
if (getRandomNumber(10) == 9)
gameField[x][y] = 4;
else
gameField[x][y] = 2;
}
private Color getColorByValue(int value) {
switch (value) {
case 2:
return Color.BLUE;
case 4:
return Color.ALICEBLUE;
case 8:
return Color.BEIGE;
case 16:
return Color.GAINSBORO;
case 32:
return Color.DARKSALMON;
case 64:
return Color.MAGENTA;
case 128:
return Color.GOLD;
case 256:
return Color.CORAL;
case 512:
return Color.RED;
case 1024:
return Color.FIREBRICK;
case 2048:
return Color.NAVY;
default:
return Color.WHITE;
}
}
private void setCellColoredNumber(int x, int y, int value) {
if (value == 0)
setCellValueEx(y, x, getColorByValue(value), "");
else
setCellValueEx(x, y, getColorByValue(value), String.valueOf(value));
}
}
/*
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];
@Override
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[x][y]);
}
}
}
private void createNewNumber() {
int randomX;
int randomY;
do {
randomX = getRandomNumber(SIDE);
randomY = getRandomNumber(SIDE);
} while (gameField[randomX][randomY] != 0);
gameField[randomX][randomY] = getRandomNumber(10) == 9 ? 4 : 2;
}
private void setCellColoredNumber(int x, int y, int value) {
String valueToString = value == 0 ? "" : String.valueOf(value);
setCellValueEx(x, y, getColorByValue(value), valueToString);
}
private Color getColorByValue(int value) {
switch (value) {
default:
return Color.CORAL;
case 2:
return Color.GOLD;
case 4:
return Color.AQUA;
case 8:
return Color.BEIGE;
case 16:
return Color.AZURE;
case 32:
return Color.BURLYWOOD;
case 64:
return Color.DARKGREY;
case 128:
return Color.FUCHSIA;
case 256:
return Color.HONEYDEW;
case 512:
return Color.GREEN;
case 1024:
return Color.YELLOW;
case 2048:
return Color.DARKVIOLET;
}
}
}*/