Add score and game over

This commit is contained in:
Lonami Exo 2017-01-26 11:54:24 +01:00
parent 5a680ad262
commit 87046fe8b4
3 changed files with 79 additions and 22 deletions

View file

@ -10,7 +10,7 @@ import com.badlogic.gdx.math.Vector2;
public class Board {
Cell[][] cells;
private final int count; // Cell count
public final int cellCount; // Cell count
public final int cellSize; // Size per cell
final Vector2 pos;
@ -19,12 +19,12 @@ public class Board {
public Board(float x, float y, int cellCount, int cellSize, boolean center) {
pos = new Vector2(x, y);
count = cellCount;
this.cellCount = cellCount;
this.cellSize = cellSize;
cells = new Cell[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
cells = new Cell[this.cellCount][this.cellCount];
for (int i = 0; i < this.cellCount; i++) {
for (int j = 0; j < this.cellCount; j++) {
cells[i][j] = new Cell();
}
}
@ -39,11 +39,11 @@ public class Board {
}
public int getPxSize() {
return count * cellSize;
return cellCount * cellSize;
}
private boolean inBounds(int x, int y) {
return x >= 0 && x < count && y >= 0 && y < count;
return x >= 0 && x < cellCount && y >= 0 && y < cellCount;
}
private boolean inBounds(Piece piece, int x, int y) {
@ -62,6 +62,17 @@ public class Board {
return true;
}
public boolean canPutPiece(Piece piece) {
for (int i = 0; i < piece.cellRows; i++) {
for (int j = 0; j < piece.cellCols; j++) {
if (canPutPiece(piece, j, i)) {
return true;
}
}
}
return false;
}
public boolean putScreenPiece(Piece piece) {
// Get the local piece coordinates
// TODO Works weird, it puts the piece like one too low
@ -85,13 +96,13 @@ public class Board {
// If the piece is put on the top left corner, all the cells will be cleared.
// If we first cleared the columns, then the rows wouldn't have been cleared.
int clearCount = 0;
boolean[] clearedRows = new boolean[count];
boolean[] clearedCols = new boolean[count];
boolean[] clearedRows = new boolean[cellCount];
boolean[] clearedCols = new boolean[cellCount];
// Analyze rows and columns that will be cleared
for (int i = 0; i < count; i++) {
for (int i = 0; i < cellCount; i++) {
clearedRows[i] = true;
for (int j = 0; j < count; j++) {
for (int j = 0; j < cellCount; j++) {
if (cells[i][j].isEmpty()) {
clearedRows[i] = false;
break;
@ -100,9 +111,9 @@ public class Board {
if (clearedRows[i])
clearCount++;
}
for (int j = 0; j < count; j++) {
for (int j = 0; j < cellCount; j++) {
clearedCols[j] = true;
for (int i = 0; i < count; i++) {
for (int i = 0; i < cellCount; i++) {
if (cells[i][j].isEmpty()) {
clearedCols[j] = false;
break;
@ -113,16 +124,16 @@ public class Board {
}
if (clearCount > 0) {
// Do clear those rows and columns
for (int i = 0; i < count; i++) {
for (int i = 0; i < cellCount; i++) {
if (clearedRows[i]) {
for (int j = 0; j < count; j++) {
for (int j = 0; j < cellCount; j++) {
cells[i][j].setEmpty();
}
}
}
for (int j = 0; j < count; j++) {
for (int j = 0; j < cellCount; j++) {
if (clearedCols[j]) {
for (int i = 0; i < count; i++) {
for (int i = 0; i < cellCount; i++) {
cells[i][j].setEmpty();
}
}
@ -148,8 +159,8 @@ public class Board {
}
public void draw(SpriteBatch batch) {
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
for (int i = 0; i < cellCount; i++) {
for (int j = 0; j < cellCount; j++) {
cells[i][j].draw(batch, cellPatch,
pos.x + j * cellSize, pos.y + i * cellSize, cellSize);
}

View file

@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
public class PieceHolder {
@ -66,8 +67,22 @@ public class PieceHolder {
return false;
}
public Array<Piece> getAvailablePieces() {
Array<Piece> result = new Array<Piece>(count);
for (int i = 0; i < count; i++) {
if (pieces[i] != null) {
result.add(pieces[i]);
}
}
return result;
}
public int calculateHeldPieceArea() {
if (heldPiece > -1) {
return pieces[heldPiece].calculateArea();
} else {
return 0;
}
}
public boolean dropPiece(Board board) {

View file

@ -3,6 +3,7 @@ package io.github.lonamiwebs.klooni.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
@ -20,10 +21,15 @@ public class GameScreen implements Screen, InputProcessor {
private SpriteBatch batch;
private final Color clearColor;
private int score;
public GameScreen(Klooni aGame) {
game = aGame;
score = 0;
clearColor = new Color(0.9f, 0.9f, 0.7f, 1f);
// Board(x, y, cell count, cell size, center)
// Board(x, y, cell cellCount, cell size, center)
board = new Board(
Gdx.graphics.getWidth() / 2,
Gdx.graphics.getHeight() * 3 / 4,
@ -43,6 +49,25 @@ public class GameScreen implements Screen, InputProcessor {
}
}
int calculateClearScore(int cleared) {
// The original game seems to work as follows:
// If < 1 were cleared, score = 0
// If = 1 was cleared, score = cells cleared
// If > 1 were cleared, score = cells cleared + score(cleared - 1)
if (cleared < 1) return 0;
if (cleared == 1) return board.cellCount;
else return board.cellCount * cleared + calculateClearScore(cleared - 1);
}
boolean isGameOver() {
for (Piece piece : holder.getAvailablePieces()) {
if (board.canPutPiece(piece)) {
return false;
}
}
return true;
}
//region Screen
@Override
@ -52,7 +77,7 @@ public class GameScreen implements Screen, InputProcessor {
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.9f, 0.9f, 0.7f, 1);
Gdx.gl.glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
@ -118,6 +143,12 @@ public class GameScreen implements Screen, InputProcessor {
int area = holder.calculateHeldPieceArea();
if (holder.dropPiece(board)) {
int cleared = board.clearComplete();
score += area + calculateClearScore(cleared);
if (isGameOver()) {
clearColor.set(0.4f, 0.1f, 0.1f, 1f);
}
return true;
} else {
return false;