Why I have ony done one task?
What's wrong with moving left?
TASK: 2048 (Part 10/18)
• The moveLeft() method must move to the left and merge the elements of the gameField matrix in accordance with the rules of the game.
• The moveLeft() method must call the compressRow(int[]) method at least once for each row of the gameField matrix.
• The moveLeft() method must call the mergeRow(int[]) method at least once for each row of the gameField matrix.
• The moveLeft() method must add a tile using the createNewNumber() method if a move or merger has occurred.
• The moveLeft() method must not change the contents of the gameField matrix if neither a move nor a merger is possible.
Why this first task isn't done?
What is wrong with moving?
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
public class Game2048 extends Game{
public void onKeyPress(Key key){
if(key == Key.UP) {
moveUp();
}else if(key == Key.DOWN){
moveDown();
}else if(key == Key.RIGHT){
moveRight();
}else if(key == Key.LEFT){
moveLeft();
}
}
private static final int SIDE = 4;
private int [][] gameField = new int[SIDE][SIDE];
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[j][i]);
}
}
}
private void createNewNumber() {
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
int value;
if (getRandomNumber(10) == 9){
value = 4;
} else {
value = 2;
}
if (gameField[x][y] == 2 || gameField[x][y] == 4){
createNewNumber();
} else {
gameField[x][y] = 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 void setCellColoredNumber(int x, int y, int value){
if(value!=0){
setCellValueEx(x,y,getColorByValue(value),Integer.toString(value));
}
else{
setCellValueEx(x,y,getColorByValue(value),"");
}
}
private boolean compressRow(int[] row){
int clonevalue=0;
boolean boolvalue = false;
for(int i = 1; i<row.length; i++) {
for (int j = 1; j < row.length; j++) {
if (row[j - 1] == 0 && row[j] != 0) {
clonevalue = row[j - 1];
row[j - 1] = row[j];
row[j] = clonevalue;
boolvalue = true;
}
}
}
return boolvalue;
}
private boolean mergeRow(int[] row){
boolean boolvaluemerge = false;
for(int i = 1; i<row.length; i++) {
if(row[i]!=0 && (row[i-1] == row[i])){
row[i-1] = row[i-1] * 2;
row[i] = 0;
boolvaluemerge = true;
}
}
return boolvaluemerge;
}
private void moveLeft(){
int[] copyField = new int[SIDE];
int clonevalue = 0 ;
//MOVE LEFT
for(int i = 0; i<SIDE; i++) {
for(int k = 1; k<SIDE; k++) {
for (int j = 1; j < SIDE; j++) {
if (gameField[i][j - 1] == 0 && gameField[i][j] != 0) {
clonevalue = gameField[i][j - 1];
gameField[i][j - 1] = gameField[i][j];
gameField[i][j] = clonevalue;
}
}
}
}
//MOVE LEFT
}
private void moveRight(){
}
private void moveUp(){
}
private void moveDown(){
}
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
}