public void setGame() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { gridA[j][i] = new Box(j, i); active[i][j] = false; } } spawnBox(); spawnBox(); }
public void spawnBox() { int num1 = rand.nextInt(4); int num2 = rand.nextInt(4); if (gridA[num1][num2].getValue() == 0) { gridA[num1][num2].randValue(); } else { spawnBox(); } }
public void moveDown() { int position; for (int j = 0; j < 4; j++) { for (int i = 2; i >= 0; i--) { position = i; while (position < 3 && gridA[position + 1][j].getValue() == 0) { // movement gridA[position + 1][j].setValue(gridA[position][j].getValue()); gridA[position][j].setValue(0); position++; } } if (gridA[3][j].getValue() == gridA[2][j].getValue()) { // combine gridA[3][j].setValue(gridA[2][j].getValue() * 2); gridA[2][j].setValue(0); } else if (gridA[2][j].getValue() == gridA[1][j].getValue()) { gridA[2][j].setValue(gridA[1][j].getValue() * 2); gridA[1][j].setValue(0); } else if (gridA[1][j].getValue() == gridA[0][j].getValue()) { gridA[1][j].setValue(gridA[0][j].getValue() * 2); gridA[0][j].setValue(0); } } spawnBox(); }
public void moveUp() { int position; for (int j = 0; j < 4; j++) { for (int i = 1; i < 4; i++) { position = i; while (position > 0 && gridA[position - 1][j].getValue() == 0) { gridA[position - 1][j].setValue(gridA[position][j].getValue()); gridA[position][j].setValue(0); position--; } } if (gridA[0][j].getValue() == gridA[1][j].getValue()) { gridA[0][j].setValue(gridA[1][j].getValue() * 2); gridA[1][j].setValue(0); } else if (gridA[1][j].getValue() == gridA[2][j].getValue()) { gridA[1][j].setValue(gridA[1][j].getValue() * 2); gridA[2][j].setValue(0); } else if (gridA[2][j].getValue() == gridA[3][j].getValue()) { gridA[2][j].setValue(gridA[3][j].getValue() * 2); gridA[3][j].setValue(0); } } spawnBox(); }