Валидатор не принимает результат false, подскажите
пошёл таким путём:
private boolean canUserMove(){
boolean zeroField = false;
boolean sameField = false;
for (int i=0; i<SIDE; i++){
for(int j=0; j<SIDE; j++){
if (gameField[i][j]==0){
zeroField = true;
}
for(int x=i-1; x<i+2; x++){
for(int y=j-1; y<j+2; y++){
if(x!=i & y!=j){
if( ((x>=0) & (x<SIDE)) & ((y>=0) & (y<SIDE)) ){
if (gameField[i][j] == gameField[x][y]){
sameField = true;
}
}
}
}
}
}
}
if (zeroField | sameField){
return true;
}else{
return false;
}
}
package com.javarush.games.game2048;
import com.javarush.engine.cell.*;
import java.util.*;
//import java.lang.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private static final int INDEXNUMBER = 12;
private int[][] gameField = new int[SIDE][SIDE];
private int[] colorNumber = new int[INDEXNUMBER];
private boolean isGameStopped = false;
@Override
public void initialize(){
setScreenSize(/*int width*/SIDE, /*int height*/SIDE);
showGrid(true);
createGame();
drawScene();
}
private void createGame(){
createNewNumber();
createNewNumber();
initColorRandom();
//String str= " ";
//for (int i : colorNumber){
// str = str+" "+ String.valueOf(Color.values()[i]);
//}
//showMessageDialog(Color.BLUE, String.valueOf(Color.values()[144]), Color.RED, 10);
//showMessageDialog(Color.BLUE, str, Color.RED, 10);
}
private void drawScene(){
for (int y = 0; y < SIDE; y++){
for (int x = 0; x < SIDE; x++){
setCellColoredNumber(x, y, gameField[y][x]);
//setCellColor(x, y, Color.RED);
}
}
}
private void createNewNumber(){
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
if (gameField[x][y] != 0){
createNewNumber();
}else{
gameField[x][y] = getRandomNumber(10)==9? 4 : 2;
}
if (getMaxTileValue() >= 2048){
win();
}
}
private void setCellColoredNumber(int x, int y, int value){
setCellValueEx(x, y, getColorByValue(value), value == 0 ? "" : String.valueOf(value));
}
private Color getColorByValue(int value){
/* if(value == 0)
return Color.WHITE;
else if(value == 2)
return Color.GREY;
else if(value == 4)
return Color.YELLOW;
else if(value == 8)
return Color.GREEN;
else if(value == 16)
return Color.ORANGE;
else if(value == 32)
return Color.AQUA;
else if(value == 64)
return Color.RED;
else if(value == 128)
return Color.AZURE;
else if(value == 256)
return Color.BLUE;
else if(value == 512)
return Color.BEIGE;
else if(value == 1024)
return Color.ALICEBLUE;
else if(value == 2048)
return Color.AQUAMARINE;
return null;*/
switch(value){
case 0:
return Color.values()[colorNumber[0]];
//break;
case 2:
return Color.values()[colorNumber[1]];
//break;
case 4:
return Color.values()[colorNumber[2]];
//break;
case 8:
return Color.values()[colorNumber[3]];
//break;
case 16:
return Color.values()[colorNumber[4]];
//break;
case 32:
return Color.values()[colorNumber[5]];
//break;
case 64:
return Color.values()[colorNumber[6]];
//break;
case 128:
return Color.values()[colorNumber[7]];
//break;
case 256:
return Color.values()[colorNumber[8]];
//break;
case 512:
return Color.values()[colorNumber[9]];
//break;
case 1024:
return Color.values()[colorNumber[10]];
//break;
case 2048:
return Color.values()[colorNumber[11]];
//break;
default:
return null;
}
}
private boolean compressRow(int[] row){
/**int[] buffRow = row;
Arrays.sort(row);
Collections.reverse(Arrays.asList(row));
return !Arrays.equals(buffRow, row);**/
boolean sortStop = false;
boolean changeArray = false;
do{
for (int i=0; i<SIDE; i++){
if (i!=SIDE-1){
if (row[i]==0 & row[i+1]!=0){
row[i]=row[i+1];
row[i+1]=0;
sortStop = changeArray = true;
}
}
}
if(sortStop){
sortStop=false;
}else{
break;
}
}while(true);
return changeArray;
}
private boolean mergeRow(int[] row){
boolean changeArray = false;
for (int i=0; i<SIDE; i++){
if (i!=SIDE-1) {
if ((row[i]!=0) & (row[i+1]!=0)){
if (row[i]==row[i+1]){
row[i]*=2;
row[i+1]=0;
changeArray = true;
}
}
}
}
return changeArray;
}
@Override
public void onKeyPress(Key key){
switch (key){
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
case UP:
moveUp();
break;
case DOWN:
moveDown();
break;
default:
}
drawScene();
}
private void moveLeft(){
boolean change = false;
for (int y = 0; y < SIDE; ++y) {
if (compressRow(gameField[y])) {
change = true;
}
if (mergeRow(gameField[y])) {
change = true;
}
if (compressRow(gameField[y])) {
change = true;
}
}
if (change) {
createNewNumber();
}
}
private void moveRight(){
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
}
private void moveUp(){
rotateClockwise();
rotateClockwise();
rotateClockwise();
moveLeft();
rotateClockwise();
}
private void moveDown(){
rotateClockwise();
moveLeft();
rotateClockwise();
rotateClockwise();
rotateClockwise();
}
private void rotateClockwise(){
int[][] bufGameFiel = new int[SIDE][SIDE];
for(int x=0;x<SIDE;x++){
for(int y=0;y<SIDE;y++){
bufGameFiel[y][x] = gameField[(gameField.length-1)-x][y];
}
}
gameField = bufGameFiel;
}
private int getMaxTileValue(){
int result = 0;
for (int[] i : gameField){
for(int j : i){
if (result<j){
result = j;
}
}
}
return result;
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.RED, "Ура ты выйграл", Color.BLUE, 16);
}
private boolean canUserMove(){
boolean zeroField = false;
boolean sameField = false;
for (int i=0; i<SIDE; i++){
for(int j=0; j<SIDE; j++){
if (gameField[i][j]==0){
zeroField = true;
}
for(int x=i-1; x<i+2; x++){
for(int y=j-1; y<j+2; y++){
if(x!=i & y!=j){
if( ((x>=0) & (x<SIDE)) & ((y>=0) & (y<SIDE)) ){
if (gameField[i][j] == gameField[x][y]){
sameField = true;
}
}
}
}
}
}
}
if (zeroField | sameField){
return true;
}else{
return false;
}
}
private void initColorRandom(){
int bufColorNumber;
int i=0;
boolean concur = false;
do{
bufColorNumber = getRandomNumber(149);
for (int j : colorNumber){
if ((j==bufColorNumber)&(concur!=true)&(bufColorNumber==Color.BLACK.ordinal())&(bufColorNumber==Color.TRANSPARENT.ordinal())){
concur = true;
}
}
if (!concur){
colorNumber[i]=bufColorNumber;
i++;
}
}while(i < INDEXNUMBER);
}
}