mirror of
https://github.com/OpenLiberty/liberty-bikes.git
synced 2025-01-18 10:23:58 +08:00
convert board to short
This commit is contained in:
parent
ff904a8c6e
commit
3f41f8b712
@ -10,10 +10,10 @@ import java.util.Set;
|
||||
public class GameBoard {
|
||||
|
||||
public static final int BOARD_SIZE = 121;
|
||||
public static final boolean SPOT_AVAILABLE = true, SPOT_TAKEN = false;
|
||||
public static final short SPOT_AVAILABLE = 0, PLAYER_SPOT_TAKEN = 1, OBJECT_SPOT_TAKEN = -1;
|
||||
|
||||
// @JsonbTransient // TODO use annotation here once OpenLiberty upgrades to yasson 1.0.1 (contains bug fix)
|
||||
private final boolean[][] board = new boolean[BOARD_SIZE][BOARD_SIZE];
|
||||
private final short[][] board = new short[BOARD_SIZE][BOARD_SIZE];
|
||||
|
||||
public final Set<Obstacle> obstacles = new HashSet<>();
|
||||
public final Set<MovingObstacle> movingObstacles = new HashSet<>();
|
||||
@ -32,7 +32,7 @@ public class GameBoard {
|
||||
// First make sure all spaces are available
|
||||
for (int x = 0; x < o.width; x++)
|
||||
for (int y = 0; y < o.height; y++)
|
||||
if (!board[o.x + x][o.y + y]) {
|
||||
if (board[o.x + x][o.y + y] != 0) {
|
||||
System.out.println("Obstacle cannot be added to board because spot [" + o.x + x + "][" + o.y + y + "] is taken.");
|
||||
return false;
|
||||
}
|
||||
@ -40,8 +40,7 @@ public class GameBoard {
|
||||
// If all spaces are available, claim them
|
||||
for (int x = 0; x < o.width; x++)
|
||||
for (int y = 0; y < o.height; y++)
|
||||
board[o.x + x][o.y + y] = SPOT_TAKEN;
|
||||
|
||||
board[o.x + x][o.y + y] = OBJECT_SPOT_TAKEN;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -57,13 +56,13 @@ public class GameBoard {
|
||||
if (p.x > BOARD_SIZE || p.y > BOARD_SIZE)
|
||||
throw new IllegalArgumentException("Player does not fit on board: " + p);
|
||||
|
||||
board[p.x][p.y] = SPOT_TAKEN;
|
||||
board[p.x][p.y] = PLAYER_SPOT_TAKEN;
|
||||
|
||||
return players.add(p);
|
||||
}
|
||||
|
||||
// TODO: once OpenLiberty moves up to yasson 1.0.1 this method can be removed
|
||||
public boolean[][] board() {
|
||||
public short[][] board() {
|
||||
return board;
|
||||
}
|
||||
|
||||
@ -71,8 +70,22 @@ public class GameBoard {
|
||||
public void dumpBoard() {
|
||||
for (int i = 0; i < BOARD_SIZE; i++) {
|
||||
StringBuilder row = new StringBuilder();
|
||||
for (int j = 0; j < BOARD_SIZE; j++)
|
||||
row.append(board[i][j] == SPOT_TAKEN ? "X" : "_");
|
||||
for (int j = 0; j < BOARD_SIZE; j++) {
|
||||
switch (board[i][j]) {
|
||||
case (PLAYER_SPOT_TAKEN): {
|
||||
row.append("X");
|
||||
break;
|
||||
}
|
||||
case (OBJECT_SPOT_TAKEN): {
|
||||
row.append("O");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
row.append("_");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(String.format("%03d %s", i, row.toString()));
|
||||
}
|
||||
}
|
||||
|
@ -187,6 +187,9 @@ public class GameRound implements Runnable {
|
||||
}
|
||||
|
||||
private void gameTick() {
|
||||
if (gameState != State.RUNNING)
|
||||
return;
|
||||
|
||||
boolean boardUpdated = board.moveObjects();
|
||||
|
||||
// Move all living players forward 1
|
||||
|
@ -28,7 +28,7 @@ public class MovingObstacle extends Obstacle {
|
||||
this.moveDelay = moveDelay;
|
||||
}
|
||||
|
||||
public void move(boolean[][] board) {
|
||||
public void move(short[][] board) {
|
||||
if (++currentDelay < moveDelay) {
|
||||
// don't move yet
|
||||
hasMoved = false;
|
||||
@ -77,34 +77,34 @@ public class MovingObstacle extends Obstacle {
|
||||
}
|
||||
}
|
||||
|
||||
public void moveRight(boolean[][] board) {
|
||||
for (int i = 0; i < height; i++) {
|
||||
public void moveRight(short[][] board) {
|
||||
for (int i = 0; i <= height; i++) {
|
||||
board[x][y + i] = GameBoard.SPOT_AVAILABLE;
|
||||
board[x + width + 1][y + i] = GameBoard.SPOT_TAKEN;
|
||||
board[x + width + 1][y + i] = GameBoard.OBJECT_SPOT_TAKEN;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
|
||||
public void moveLeft(boolean[][] board) {
|
||||
for (int i = 0; i < height; i++) {
|
||||
board[x - 1][y + i] = GameBoard.SPOT_TAKEN;
|
||||
public void moveLeft(short[][] board) {
|
||||
for (int i = 0; i <= height; i++) {
|
||||
board[x - 1][y + i] = GameBoard.OBJECT_SPOT_TAKEN;
|
||||
board[x + width][y + i] = GameBoard.SPOT_AVAILABLE;
|
||||
}
|
||||
x--;
|
||||
}
|
||||
|
||||
public void moveUp(boolean[][] board) {
|
||||
for (int i = 0; i < width; i++) {
|
||||
board[x + i][y - 1] = GameBoard.SPOT_TAKEN;
|
||||
public void moveUp(short[][] board) {
|
||||
for (int i = 0; i <= width; i++) {
|
||||
board[x + i][y - 1] = GameBoard.OBJECT_SPOT_TAKEN;
|
||||
board[x + i][y + height] = GameBoard.SPOT_AVAILABLE;
|
||||
}
|
||||
y--;
|
||||
}
|
||||
|
||||
public void moveDown(boolean[][] board) {
|
||||
for (int i = 0; i < width; i++) {
|
||||
public void moveDown(short[][] board) {
|
||||
for (int i = 0; i <= width; i++) {
|
||||
board[x + i][y] = GameBoard.SPOT_AVAILABLE;
|
||||
board[x + i][y + height + 1] = GameBoard.SPOT_TAKEN;
|
||||
board[x + i][y + height + 1] = GameBoard.OBJECT_SPOT_TAKEN;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
|
@ -81,9 +81,9 @@ public class Player {
|
||||
*
|
||||
* @return True if the player is still alive after moving forward one space. False otherwise.
|
||||
*/
|
||||
public boolean movePlayer(boolean[][] board) {
|
||||
public boolean movePlayer(short[][] s) {
|
||||
// Consume the space the player was in before the move
|
||||
board[x][y] = false;
|
||||
s[x][y] = GameBoard.PLAYER_SPOT_TAKEN;
|
||||
|
||||
// If a player issues two moves in the same game tick and the second direction is illegal,
|
||||
// spread out the moves across two ticks rather than ignoring the second move entirely
|
||||
@ -112,8 +112,7 @@ public class Player {
|
||||
}
|
||||
|
||||
// Check if the player is now dead after moving
|
||||
boolean spaceAvailable = board[x][y];
|
||||
if (!spaceAvailable) {
|
||||
if (s[x][y] != GameBoard.SPOT_AVAILABLE) {
|
||||
setStatus(STATUS.Dead);
|
||||
}
|
||||
lastDirection = direction;
|
||||
|
@ -58,7 +58,7 @@ public class GameBoardTest {
|
||||
}
|
||||
|
||||
private void verifyAvailable(int x, int y) {
|
||||
if (board.board()[x][y] == GameBoard.SPOT_TAKEN) {
|
||||
if (board.board()[x][y] != GameBoard.SPOT_AVAILABLE) {
|
||||
board.dumpBoard();
|
||||
fail("Spot should be availble but it was taken: [" + x + "][" + y + "]");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user