From 87046fe8b438ced19d358c7d687d51b469148e7e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 26 Jan 2017 11:54:24 +0100 Subject: [PATCH] Add score and game over --- .../github/lonamiwebs/klooni/game/Board.java | 49 ++++++++++++------- .../lonamiwebs/klooni/game/PieceHolder.java | 17 ++++++- .../lonamiwebs/klooni/screens/GameScreen.java | 35 ++++++++++++- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/core/src/io/github/lonamiwebs/klooni/game/Board.java b/core/src/io/github/lonamiwebs/klooni/game/Board.java index f6e02fb..32a9534 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/Board.java +++ b/core/src/io/github/lonamiwebs/klooni/game/Board.java @@ -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); } diff --git a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java index 36a3a1d..a3496ce 100644 --- a/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java +++ b/core/src/io/github/lonamiwebs/klooni/game/PieceHolder.java @@ -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 getAvailablePieces() { + Array result = new Array(count); + for (int i = 0; i < count; i++) { + if (pieces[i] != null) { + result.add(pieces[i]); + } + } + return result; + } + public int calculateHeldPieceArea() { - return pieces[heldPiece].calculateArea(); + if (heldPiece > -1) { + return pieces[heldPiece].calculateArea(); + } else { + return 0; + } } public boolean dropPiece(Board board) { diff --git a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java index 7ba279f..0876eb6 100644 --- a/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java +++ b/core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java @@ -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;