Помогите пожалуйста , всю голову сломала
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();
}
}
katerinaalekseeva
39 уровень
Не работает обновление игрового поля при проигрыше и нажатии пробола
Обсуждается
Комментарии (11)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:05
у тебя есть пропуски и ошибки в тексте по типу лишних фраз как например в самом начале
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 int score; лишняя фраза).
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:08
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;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
} (это начало далее все, что написано как коммент прописывай или если хочешь копируй)
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:16
@Override
public void onKeyPress(Key key) {
if (isGameStopped) {
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
} else {
return;
}
}
if (!canUserMove()) {
gameOver();
return;
}
if (key == Key.UP) {
moveUp();
} else if (key == Key.RIGHT) {
moveRight();
} else if (key == Key.DOWN) {
moveDown();
} else if (key == Key.LEFT) {
moveLeft();
} else {
return;
}
drawScene();
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:18
@Override
public void onKeyPress(Key key) {
if (isGameStopped) {
if (key == Key.SPACE) {
isGameStopped = false;
createGame();
drawScene();
} else {
return;
}
}
if (!canUserMove()) {
gameOver();
return;
}
if (key == Key.UP) {
moveUp();
} else if (key == Key.RIGHT) {
moveRight();
} else if (key == Key.DOWN) {
moveDown();
} else if (key == Key.LEFT) {
moveLeft();
} else {
return;
}
drawScene();
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:18
private void createNewNumber() {
if (getMaxTileValue() >= 2048) {
win();
return;
}
boolean isCreated = false;
do {
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
if (gameField[y][x] == 0) {
gameField[y][x] = getRandomNumber(10) < 9 ? 2 : 4;
isCreated = true;
}
}
while (!isCreated);
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:19
private int getMaxTileValue() {
int max = gameField[0][0];
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
if (gameField[y][x] > max) {
max = gameField[y][x];
}
}
}
return max;
}
private void gameOver() {
showMessageDialog(Color.NONE, "GAME OVER!", Color.WHITE, 50);
isGameStopped = true;
}
private void win() {
showMessageDialog(Color.NONE, "YOU WIN!", Color.WHITE, 50);
isGameStopped = true;
}
private void setCellColoredNumber(int x, int y, int value) {
Color color = getColorByValue(value);
String str = value > 0 ? "" + value : "";
setCellValueEx(x, y, color, str);
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:19
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;
}
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:19
private void moveLeft() {
boolean isNewNumberNeeded = false;
for (int[] row : gameField) {
boolean wasCompressed = compressRow(row);
boolean wasMerged = mergeRow(row);
if (wasMerged) {
compressRow(row);
}
if (wasCompressed || wasMerged) {
isNewNumberNeeded = true;
}
}
if (isNewNumberNeeded) {
createNewNumber();
}
}
private void moveUp() {
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void moveRight() {
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveDown() {
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
0
Sad Mouse.AVI Работает в безработный
6 декабря 2021, 18:20
private boolean compressRow(int[] row) {
int insertPosition = 0;
boolean result = false;
for (int x = 0; x < SIDE; x++) {
if (row[x] > 0) {
if (x != insertPosition) {
row[insertPosition] = row[x];
row[x] = 0;
result = true;
}
insertPosition++;
}
}
return result;
}
private boolean mergeRow(int[] row) {
boolean result = false;
for (int i = 0; i < row.length - 1; i++) {
if (row[i] != 0 && row[i] == row[i + 1]) {
row[i] += row[i + 1];
row[i + 1] = 0;
result = true;
}
}
return result;
}
private void rotateClockwise() {
int[][] result = new int[SIDE][SIDE];
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
result[j][SIDE - 1 - i] = gameField[i][j];
}
}
gameField = result;
}
private void drawScene() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
setCellColoredNumber(x, y, gameField[y][x]);
}
}
}
}
(это конец кода)
0
katerinaalekseeva
13 декабря 2021, 17:28
Спасибо огромное !
0
Андрей
1 марта, 13:01
У меня несколько вопросов:
1) Зачем нам удалять поле score если нам оно нужно по условию задачи?
2) А в чем собственно то ошибка заключалась? Я возможно не очень внимательно изучил оба кода, но я не увидел больших различий, кроме синтаксических
0