I am having a harder time with this than any other task. I want to do this with a loop, but I had to find a pattern first so I wrote out all 16 parts of the rotation. It seems like I found the pattern but it still won't validate.
In the example, I am assuming that gameField[0] is the first array, {2, 2, 2, 2}. After rotation, those move to gameField[0][3], gameField[1][3], gameField[2][3], and gameField[3][3].
package com.codegym.games.game2048;
import com.codegym.engine.cell.*;
import java.util.Arrays;
public class Game2048 extends Game
{
private static final int SIDE = 4;
private int[][] gameField = new int[SIDE][SIDE];
public void initialize()
{
setScreenSize(SIDE, SIDE);
createGame();
drawScene();
}
private void createGame()
{
createNewNumber();
createNewNumber();
}
private void drawScene()
{
for(int y = 0; y < SIDE; y++)
{
for(int x = 0; x < SIDE; x++)
{
setCellColoredNumber(x, y, gameField[y][x]);
}
}
}
private void createNewNumber()
{
int twoOrFour = getRandomNumber(10);
int x = getRandomNumber(SIDE);
int y = getRandomNumber(SIDE);
boolean occupied = true;
if(twoOrFour == 1)
{
twoOrFour = 4;
}
else
{
twoOrFour = 2;
}
while(occupied)
{
if(gameField[x][y] != 0)
{
x = getRandomNumber(SIDE);
y = getRandomNumber(SIDE);
}
else
{
occupied = false;
}
}
gameField[x][y] = twoOrFour;
}
private Color getColorByValue(int c)
{
switch(c)
{
case 0:
return Color.LIGHTGRAY;
case 2:
return Color.LIGHTYELLOW;
case 4:
return Color.LIGHTBLUE;
case 8:
return Color.LIGHTGREEN;
case 16:
return Color.CYAN;
case 32:
return Color.PINK;
case 64:
return Color.ORANGE;
case 128:
return Color.RED;
case 256:
return Color.YELLOW;
case 512:
return Color.PURPLE;
case 1024:
return Color.BLUE;
case 2048:
return Color.GREEN;
}
return Color.BLACK; //denotes an error
}
private void setCellColoredNumber(int x, int y, int value)
{
if(value != 0)
{
setCellValueEx(x, y, getColorByValue(value), "" + value);
}
else
{
setCellValueEx(x, y, getColorByValue(value), "");
}
}
private boolean compressRow(int[] row)
{
boolean didShift = false;
int start = 0;
int end = SIDE;
for(int x = start+1; x < end; x++)
{
for(int n = x; n > start; n--)
{
if(row[n] > 0 && row[n-1] == 0)
{
didShift = true;
row[n] = row[n] + row[n-1];
row[n-1] = row[n] - row[n-1];
row[n] = row[n] - row[n-1];
}
}
}
return didShift;
}
private boolean mergeRow(int[] row)
{
int start = 0;
boolean didMerge = false;
for(int x = start; x < SIDE - 1; x++)
{
if(row[x] == row[x+1] && row[x] > 0)
{
didMerge = true;
row[x] *= 2;
row[x+1] = 0;
x++;
}
}
return didMerge;
}
public void onKeyPress(Key key)
{
switch(key)
{
case UP:
moveUp();
drawScene();
break;
case DOWN:
moveDown();
drawScene();
break;
case RIGHT:
moveRight();
drawScene();
break;
case LEFT:
moveLeft();
drawScene();
break;
}
}
private void moveLeft()
{
boolean boardChanged = false;
for(int n = 0; n < SIDE; n++)
{
if(compressRow(gameField[n]))
{
boardChanged = true;
}
if(mergeRow(gameField[n]))
{
boardChanged = true;
compressRow(gameField[n]);
}
}
if(boardChanged)
{
createNewNumber();
}
}
private void moveRight()
{
}
private void moveUp()
{
}
private void moveDown()
{
}
private void rotateClockwise()
{
int[][] tempArray = Arrays.copyOf(gameField,gameField.length);
/*gameField[0][3] = tempArray[0][0];
gameField[1][3] = tempArray[0][1];
gameField[2][3] = tempArray[0][2];
gameField[3][3] = tempArray[0][3];
gameField[0][2] = tempArray[1][0];
gameField[1][2] = tempArray[1][1];
gameField[2][2] = tempArray[1][2];
gameField[3][2] = tempArray[1][3];
gameField[0][1] = tempArray[2][0];
gameField[1][1] = tempArray[2][1];
gameField[2][1] = tempArray[2][2];
gameField[3][1] = tempArray[2][3];
gameField[0][0] = tempArray[3][0];
gameField[1][0] = tempArray[3][1];
gameField[2][0] = tempArray[3][2];
gameField[3][0] = tempArray[3][3];*/
for(int y = 0; y < SIDE; y++)
{
for(int x = 0; x < SIDE; x++)
{
gameField[y][Math.abs(x-3)] = tempArray[x][y];
}
}
}
}