There are a couple of things I do not understand
1) How to add a color to the specific field?
2) should you use if statements or a a switch statement
3) why does it not accept the empty string "" in the setCellValueEx ?
4) I do not understand this part of the code:
private void drawScene(){
for (int i = 0;i < SIDE;i++){
for (int y = 0;y < SIDE;y++){
setCellColoredNumber(i,y,gameField[i][y]);// what exactly are we doing on this line of code?
}
}
}
Can anybody please explain the above to me. It will be very much appreciated!!
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
public class Game2048 extends Game {
private static final int SIDE = 4;
private int[][] gameField = new int [SIDE][SIDE];
int value;
@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]);//copied this from someone lese but I do not understand this bit
}
private void createNewNumber(){
int x = getRandomNumber(SIDE);//get a random number and assign its value to x
int y = getRandomNumber(SIDE);// same but for y
if(getRandomNumber(10) == 9) value =4;//get a random number and if is equal to nine assign value to 4
else value = 2;//otherwise you assign 2 to the value
if (gameField[x][y] == 2 || gameField[x][y] == 4) createNewNumber();//how do we assign values to one of the fields?
else gameField[x][y] = value;//if the cell has no value yet assign it value 2 or 4 depending on outcome of randomNumber(10)
//as this method createNewNumber is only called twice only two cells will get a value
}
//private Color getColorByValue(int value){
//returns a cell with a color based on its value/*private Color getColorByValue(int value){
//return a cell color based on value
/*switch(value){
case 0: return Color.WHITE;
case 2: return Color.BLUE;
case 4: return Color.RED;
case 8: return Color.GREEN;
case 16: return Color.CYAN;
case 32: return Color.GRAY;
case 64: return Color.MAGENTA;
case 128: return Color.ORANGE;
case 256: return Color.PINK;
case 512: return Color.YELLOW;
case 1024: return Color.PURPLE;
case 2048: return Color.BROWN;
default: return Color.WHITE;
}*/
private Color getColorByValue(int value){//copied this also from someone else
if (value == 0){
return Color.WHITE;
} else if (value == 2){
return Color.ALICEBLUE;
} else if (value == 4){
return Color.ANTIQUEWHITE;
} else if (value == 8){
return Color.AQUA;
} else if (value == 16){
return Color.AQUAMARINE;
} else if (value == 32){
return Color.AZURE;
} else if (value == 64){
return Color.BEIGE;
} else if (value == 128){
return Color.BISQUE;
} else if (value == 256){
return Color.BLACK;
} else if (value == 512){
return Color.BLANCHEDALMOND;
} else if (value == 1024){
return Color.BLUE;
} else if (value == 2048) {
return Color.BLUEVIOLET;
} else return Color.WHITE;
}
private void setCellColoredNumber(int x, int y, int value){
if (value != 0) {
setCellValueEx(y, x, getColorByValue(value), Integer.toString(value));
} else {
setCellValueEx(y, x, getColorByValue(value), "");//why does this not work here? copied it from someone else
}
}
}