Помогите пожалуйста , всю голову сломала 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; private int score; private void createGame(){ gameField =new int[SIDE][SIDE]; createNewNumber(); createNewNumber(); } private void drawScene(){ for(int i=0; i<gameField.length; i++){ for(int j=0; j<gameField.length; j++){ setCellColoredNumber( i, j, gameField[j][i]) ; } } } private void createNewNumber(){ int x; int y; if (getMaxTileValue()==2048){ win(); } do { x = getRandomNumber(SIDE); y = getRandomNumber(SIDE); } while(gameField[x][y]!=0); int percent = getRandomNumber(10); if (percent==9){ gameField[x][y]=4; } else{ gameField[x][y]=2; } } private void setCellColoredNumber(int x, int y, int value){ if (value==0){ setCellValueEx( x, y, getColorByValue(value), ""); } else { setCellValueEx( x, y, getColorByValue(value),value+"" ); } } private Color getColorByValue(int value){ switch (value){ case 0 : return Color.WHITE; case 2 : return Color.BLUE ; case 4 : return Color.YELLOW; case 8 : return Color.GREEN; case 16 : return Color.PINK; case 32 : return Color.BROWN; case 64 :return Color.SALMON; case 128 : return Color.TURQUOISE; case 256 : return Color.MEDIUMSEAGREEN; case 512 : return Color.PLUM; case 1024 : return Color.CORAL; case 2048 : return Color.RED; default: return Color.NONE; } } private boolean compressRow(int[] row){ boolean n =false; int count =0; for (int i = 0; i < SIDE; i++){ if (row[i] > 0 ) { if (i != count) { row[count] = row[i]; row[i] = 0; n = true; } count++; } } return n; } private boolean mergeRow(int[] row){ boolean n = false; for (int i = 0; i <SIDE-1; i++) { if (row[i] == row[i+1] && row[i] != 0) { row[i] *= 2; row[i+1] = 0; score +=row[i]; setScore( score); n = true; } } return n; } @Override public void onKeyPress(Key key){ if(isGameStopped) { if (key == Key.SPACE) { isGameStopped = false; createGame(); drawScene(); score=0; setScore(score); } } else if(!isGameStopped){ if (canUserMove() ) { if (key == Key.LEFT) { moveLeft(); drawScene(); } if (key == Key.RIGHT) { moveRight(); drawScene(); } if (key == Key.DOWN) { moveDown(); drawScene(); } if (key == Key.UP) { moveUp(); drawScene(); } } } else gameOver(); } private void moveLeft() { boolean c = false; for (int[] field : gameField) { if (compressRow(field)){ c=true; } if( mergeRow(field)){ c=true; } compressRow(field); } if (c) { 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[][] tempArr = new int[SIDE][SIDE]; for (int i = 0; i < SIDE ; i++) { for (int j = 0; j <SIDE ; j++) { tempArr[i][j] = gameField[gameField.length-j-1][i]; } } gameField = tempArr; } private int getMaxTileValue(){ int max=0; for (int i = 0; i < SIDE; i++) { for (int j = 0; j < SIDE; j++) { if(gameField[i][j]>max){ max=gameField[i][j]; } } } return max; } private void win(){ isGameStopped=true; showMessageDialog(Color.CORAL, "ПОБЕДА", Color.GREEN, 25); } private boolean canUserMove(){ boolean freefield =false; for(int[] i : gameField){ for(int k : i ){ if(k ==0){ freefield= true; } } } for (int i = 0; i < SIDE-1; i++) { for (int j = 0; j < SIDE - 1; j++) { if (gameField[i][j] != 0 && (gameField[i][j] == gameField[i + 1][j]) || (gameField[i][j] == gameField[i][j + 1])) { freefield = true; } if (gameField[i][SIDE - 1] == gameField[i + 1][SIDE - 1]) { freefield = true; } if (gameField[SIDE - 1][j] == gameField[SIDE - 1][j + 1]) { freefield = true; } } } return freefield; } private void gameOver(){ if (!canUserMove()) { isGameStopped = true; showMessageDialog(Color.BLACK, "GAME OVER", Color.WHITE, 25); } } public void initialize(){ setScreenSize(SIDE, SIDE); createGame(); drawScene(); } }