package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private int[][] gameField = new int[SIDE][SIDE];
private static final int SIDE = 4;
private void drawScene() {
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
setCellColoredNumber(y, x, gameField[x][y]);
}
}
}
private void createGame() {
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
gameField[y][x] = 0;
}
}
createNewNumber();
createNewNumber();
}
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createNewNumber() {
getMaxTileValue();
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;
for (int s = 0; s < SIDE; s++)
for (int k = 0; k < SIDE; k++)
if (gameField[k][s] == 2048)
win();
}
private Color getColorByValue(int value) {
switch (value) {
case 2:
return Color.BLUEVIOLET;
case 4:
return Color.DEEPPINK;
case 8:
return Color.GOLD;
case 16:
return Color.GREEN;
case 32:
return Color.BLUE;
case 64:
return Color.PURPLE;
case 128:
return Color.ORANGE;
case 256:
return Color.CORAL;
case 512:
return Color.GOLD;
case 1024:
return Color.FIREBRICK;
case 2048:
return Color.RED;
default:
return Color.WHITE;
}
}
private void setCellColoredNumber(int y, int x, int value) {
if (value == 0)
setCellValueEx(y, x, getColorByValue(value), "");
else
setCellValueEx(y, x, getColorByValue(value), String.valueOf(value));
}
private boolean compressRow(int[] row) {
boolean flagCheck = false;
for (int i = 0; i < row.length; i++) {
if (row[i] == 0) {
for (int j = i; j < row.length; j++)
if (row[j] != 0) {
int tmp = row[j];
row[i] = tmp;
row[j] = 0;
flagCheck = true;
break;
}
} else {
}
}
return flagCheck;
}
private boolean mergeRow(int[] row){
boolean sumCell = false;
int emptyNumber = 0;
for (int i = 0; i<row.length-1; i++){
if(row[i] == row[i+1] && row[i] != 0 ) {
row[i] = row[i] + row[i + 1];
row[i + 1] = emptyNumber;
int score = 0;
{
score=score +row[i]+row[i+1];
setScore(score);
}
sumCell = true;
}
}
return sumCell;
}
public void onKeyPress(Key key) {
if (isGameStopped){
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
}
else {
return;
}
}
if (!canUserMove()) {
gameOver();
return;
}
else {
if (key.equals(Key.LEFT)) {
drawScene();
moveLeft();
}
if (key.equals(Key.RIGHT)) {
moveRight();
drawScene();
}
if (key.equals(Key.DOWN)) {
moveDown();
drawScene();
}
if (key.equals(Key.UP)) {
moveUp();
drawScene();
}
}
}
private void moveLeft() {
boolean create = false;
for (int y = 0; y < SIDE; y++) {
if (compressRow(gameField[y])) create = true;
if (mergeRow(gameField[y])) create = true;
if (compressRow(gameField[y])) create = true;
}
if (create) createNewNumber();
}
private void moveRight(){
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveDown(){
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void moveUp(){
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void rotateClockwise(){
int[][] rotateMatrix = new int[SIDE][SIDE];
for (int i = 0; i < SIDE; i++){
for (int j = 0; j < SIDE; j++){
rotateMatrix[i][j] = gameField[gameField.length - j - 1][i];
}
}
gameField = rotateMatrix;
}
private int getMaxTileValue() {
int max = 0;
for (int[] ints : gameField) {
for (int anInt : ints) {
max = Math.max(anInt, max);
}
}
return max;
}
private boolean isGameStopped = false;
private void win(){
isGameStopped = true;
showMessageDialog(Color.GREEN,"Ти виграв!",Color.BLUE,80);
}
private boolean canUserMove(){
for (int x=0; x<SIDE; x++){
for(int y=0; y<SIDE; y++){
if (gameField[x][y] == 0){
return true;
}
if(x < SIDE-1){
if (gameField[x][y] == gameField[x + 1][y]){
return true;
}
}
if(y < SIDE-1){
if (gameField[x][y] ==gameField[x][y + 1]){
return true;
}
}
}
}
return false;
}
private void gameOver(){
isGameStopped=true;
showMessageDialog(Color.RED,"Ти програв!",Color.BLUE,80);
}
}
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
public class Game2048 extends Game {
private int[][] gameField = new int[SIDE][SIDE];
private static final int SIDE = 4;
private void drawScene() {
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
setCellColoredNumber(y, x, gameField[x][y]);
}
}
}
private void createGame() {
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
gameField[y][x] = 0;
}
}
createNewNumber();
createNewNumber();
}
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createNewNumber() {
getMaxTileValue();
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;
for (int s = 0; s < SIDE; s++)
for (int k = 0; k < SIDE; k++)
if (gameField[k][s] == 2048)
win();
}
private Color getColorByValue(int value) {
switch (value) {
case 2:
return Color.BLUEVIOLET;
case 4:
return Color.DEEPPINK;
case 8:
return Color.GOLD;
case 16:
return Color.GREEN;
case 32:
return Color.BLUE;
case 64:
return Color.PURPLE;
case 128:
return Color.ORANGE;
case 256:
return Color.CORAL;
case 512:
return Color.GOLD;
case 1024:
return Color.FIREBRICK;
case 2048:
return Color.RED;
default:
return Color.WHITE;
}
}
private void setCellColoredNumber(int y, int x, int value) {
if (value == 0)
setCellValueEx(y, x, getColorByValue(value), "");
else
setCellValueEx(y, x, getColorByValue(value), String.valueOf(value));
}
private boolean compressRow(int[] row) {
boolean flagCheck = false;
for (int i = 0; i < row.length; i++) {
if (row[i] == 0) {
for (int j = i; j < row.length; j++)
if (row[j] != 0) {
int tmp = row[j];
row[i] = tmp;
row[j] = 0;
flagCheck = true;
break;
}
} else {
}
}
return flagCheck;
}
private boolean mergeRow(int[] row){
boolean sumCell = false;
int emptyNumber = 0;
for (int i = 0; i<row.length-1; i++){
if(row[i] == row[i+1] && row[i] != 0 ) {
row[i] = row[i] + row[i + 1];
row[i + 1] = emptyNumber;
int score = 0;
{
score=score +row[i]+row[i+1];
setScore(score);
}
sumCell = true;
}
}
return sumCell;
}
public void onKeyPress(Key key) {
if (isGameStopped){
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
}
else {
return;
}
}
if (!canUserMove()) {
gameOver();
return;
}
else {
if (key.equals(Key.LEFT)) {
drawScene();
moveLeft();
}
if (key.equals(Key.RIGHT)) {
moveRight();
drawScene();
}
if (key.equals(Key.DOWN)) {
moveDown();
drawScene();
}
if (key.equals(Key.UP)) {
moveUp();
drawScene();
}
}
}
private void moveLeft() {
boolean create = false;
for (int y = 0; y < SIDE; y++) {
if (compressRow(gameField[y])) create = true;
if (mergeRow(gameField[y])) create = true;
if (compressRow(gameField[y])) create = true;
}
if (create) createNewNumber();
}
private void moveRight(){
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveDown(){
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void moveUp(){
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void rotateClockwise(){
int[][] rotateMatrix = new int[SIDE][SIDE];
for (int i = 0; i < SIDE; i++){
for (int j = 0; j < SIDE; j++){
rotateMatrix[i][j] = gameField[gameField.length - j - 1][i];
}
}
gameField = rotateMatrix;
}
private int getMaxTileValue() {
int max = 0;
for (int[] ints : gameField) {
for (int anInt : ints) {
max = Math.max(anInt, max);
}
}
return max;
}
private boolean isGameStopped = false;
private void win(){
isGameStopped = true;
showMessageDialog(Color.GREEN,"Ти виграв!",Color.BLUE,80);
}
private boolean canUserMove(){
for (int x=0; x<SIDE; x++){
for(int y=0; y<SIDE; y++){
if (gameField[x][y] == 0){
return true;
}
if(x < SIDE-1){
if (gameField[x][y] == gameField[x + 1][y]){
return true;
}
}
if(y < SIDE-1){
if (gameField[x][y] ==gameField[x][y + 1]){
return true;
}
}
}
}
return false;
}
private void gameOver(){
isGameStopped=true;
showMessageDialog(Color.RED,"Ти програв!",Color.BLUE,80);
}
}