Add score and game over
This commit is contained in:
parent
5a680ad262
commit
87046fe8b4
3 changed files with 79 additions and 22 deletions
|
@ -10,7 +10,7 @@ import com.badlogic.gdx.math.Vector2;
|
||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
Cell[][] cells;
|
Cell[][] cells;
|
||||||
private final int count; // Cell count
|
public final int cellCount; // Cell count
|
||||||
public final int cellSize; // Size per cell
|
public final int cellSize; // Size per cell
|
||||||
|
|
||||||
final Vector2 pos;
|
final Vector2 pos;
|
||||||
|
@ -19,12 +19,12 @@ public class Board {
|
||||||
|
|
||||||
public Board(float x, float y, int cellCount, int cellSize, boolean center) {
|
public Board(float x, float y, int cellCount, int cellSize, boolean center) {
|
||||||
pos = new Vector2(x, y);
|
pos = new Vector2(x, y);
|
||||||
count = cellCount;
|
this.cellCount = cellCount;
|
||||||
this.cellSize = cellSize;
|
this.cellSize = cellSize;
|
||||||
|
|
||||||
cells = new Cell[count][count];
|
cells = new Cell[this.cellCount][this.cellCount];
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < this.cellCount; i++) {
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < this.cellCount; j++) {
|
||||||
cells[i][j] = new Cell();
|
cells[i][j] = new Cell();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,11 +39,11 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPxSize() {
|
public int getPxSize() {
|
||||||
return count * cellSize;
|
return cellCount * cellSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean inBounds(int x, int y) {
|
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) {
|
private boolean inBounds(Piece piece, int x, int y) {
|
||||||
|
@ -62,6 +62,17 @@ public class Board {
|
||||||
return true;
|
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) {
|
public boolean putScreenPiece(Piece piece) {
|
||||||
// Get the local piece coordinates
|
// Get the local piece coordinates
|
||||||
// TODO Works weird, it puts the piece like one too low…
|
// 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 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.
|
// If we first cleared the columns, then the rows wouldn't have been cleared.
|
||||||
int clearCount = 0;
|
int clearCount = 0;
|
||||||
boolean[] clearedRows = new boolean[count];
|
boolean[] clearedRows = new boolean[cellCount];
|
||||||
boolean[] clearedCols = new boolean[count];
|
boolean[] clearedCols = new boolean[cellCount];
|
||||||
|
|
||||||
// Analyze rows and columns that will be cleared
|
// 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;
|
clearedRows[i] = true;
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < cellCount; j++) {
|
||||||
if (cells[i][j].isEmpty()) {
|
if (cells[i][j].isEmpty()) {
|
||||||
clearedRows[i] = false;
|
clearedRows[i] = false;
|
||||||
break;
|
break;
|
||||||
|
@ -100,9 +111,9 @@ public class Board {
|
||||||
if (clearedRows[i])
|
if (clearedRows[i])
|
||||||
clearCount++;
|
clearCount++;
|
||||||
}
|
}
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < cellCount; j++) {
|
||||||
clearedCols[j] = true;
|
clearedCols[j] = true;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < cellCount; i++) {
|
||||||
if (cells[i][j].isEmpty()) {
|
if (cells[i][j].isEmpty()) {
|
||||||
clearedCols[j] = false;
|
clearedCols[j] = false;
|
||||||
break;
|
break;
|
||||||
|
@ -113,16 +124,16 @@ public class Board {
|
||||||
}
|
}
|
||||||
if (clearCount > 0) {
|
if (clearCount > 0) {
|
||||||
// Do clear those rows and columns
|
// Do clear those rows and columns
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < cellCount; i++) {
|
||||||
if (clearedRows[i]) {
|
if (clearedRows[i]) {
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < cellCount; j++) {
|
||||||
cells[i][j].setEmpty();
|
cells[i][j].setEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < cellCount; j++) {
|
||||||
if (clearedCols[j]) {
|
if (clearedCols[j]) {
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < cellCount; i++) {
|
||||||
cells[i][j].setEmpty();
|
cells[i][j].setEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,8 +159,8 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(SpriteBatch batch) {
|
public void draw(SpriteBatch batch) {
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < cellCount; i++) {
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < cellCount; j++) {
|
||||||
cells[i][j].draw(batch, cellPatch,
|
cells[i][j].draw(batch, cellPatch,
|
||||||
pos.x + j * cellSize, pos.y + i * cellSize, cellSize);
|
pos.x + j * cellSize, pos.y + i * cellSize, cellSize);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.math.Interpolation;
|
import com.badlogic.gdx.math.Interpolation;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
|
||||||
public class PieceHolder {
|
public class PieceHolder {
|
||||||
|
|
||||||
|
@ -66,8 +67,22 @@ public class PieceHolder {
|
||||||
return false;
|
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() {
|
public int calculateHeldPieceArea() {
|
||||||
|
if (heldPiece > -1) {
|
||||||
return pieces[heldPiece].calculateArea();
|
return pieces[heldPiece].calculateArea();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropPiece(Board board) {
|
public boolean dropPiece(Board board) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package io.github.lonamiwebs.klooni.screens;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.InputProcessor;
|
import com.badlogic.gdx.InputProcessor;
|
||||||
import com.badlogic.gdx.Screen;
|
import com.badlogic.gdx.Screen;
|
||||||
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
|
@ -20,10 +21,15 @@ public class GameScreen implements Screen, InputProcessor {
|
||||||
|
|
||||||
private SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
|
|
||||||
|
private final Color clearColor;
|
||||||
|
private int score;
|
||||||
|
|
||||||
public GameScreen(Klooni aGame) {
|
public GameScreen(Klooni aGame) {
|
||||||
game = 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(
|
board = new Board(
|
||||||
Gdx.graphics.getWidth() / 2,
|
Gdx.graphics.getWidth() / 2,
|
||||||
Gdx.graphics.getHeight() * 3 / 4,
|
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
|
//region Screen
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -52,7 +77,7 @@ public class GameScreen implements Screen, InputProcessor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(float delta) {
|
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);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
batch.begin();
|
batch.begin();
|
||||||
|
@ -118,6 +143,12 @@ public class GameScreen implements Screen, InputProcessor {
|
||||||
int area = holder.calculateHeldPieceArea();
|
int area = holder.calculateHeldPieceArea();
|
||||||
if (holder.dropPiece(board)) {
|
if (holder.dropPiece(board)) {
|
||||||
int cleared = board.clearComplete();
|
int cleared = board.clearComplete();
|
||||||
|
score += area + calculateClearScore(cleared);
|
||||||
|
|
||||||
|
if (isGameOver()) {
|
||||||
|
clearColor.set(0.4f, 0.1f, 0.1f, 1f);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue