Skip to content

Commit

Permalink
refactor for better separation of logic and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
arthu-rguo committed Mar 18, 2023
1 parent 9477da6 commit de089da
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 264 deletions.
125 changes: 43 additions & 82 deletions src/main/model/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Board {
private final Square[] gameState;
private final List<Move> history;
private Pawn lastEnPassantTarget;
private boolean isGameOver;

/**
* @EFFECTS: Constructs a new empty board.
Expand All @@ -26,45 +27,22 @@ public Board() {
this.gameState = new Square[SIZE * SIZE];
this.history = new LinkedList<>();
this.lastEnPassantTarget = null;
this.isGameOver = false;

// Initialize an empty board.
for (int i = 0; i < gameState.length; i++) {
gameState[i] = new Square(i % SIZE, i / SIZE);
}
setupPieces();
}

/**
* @EFFECTS: Sets all pieces of the given colour in their starting positions.
* @MODIFIES: {@code this}
*/
public void setupPieces(Colour colour) {
int y = colour.getDirection() > 0 ? 0 : SIZE - 1;

// Remove any pieces of the given colour first.
clearPieces(colour);

// No better way than to hard-code this...
getSquare(0, y).setPiece(new Rook(colour));
getSquare(1, y).setPiece(new Knight(colour));
getSquare(2, y).setPiece(new Bishop(colour));
getSquare(3, y).setPiece(new Queen(colour));
getSquare(SIZE - 4, y).setPiece(new King(colour));
getSquare(SIZE - 3, y).setPiece(new Bishop(colour));
getSquare(SIZE - 2, y).setPiece(new Knight(colour));
getSquare(SIZE - 1, y).setPiece(new Rook(colour));

for (int i = 0; i < SIZE; i++) {
getSquare(i, y + colour.getDirection()).setPiece(new Pawn(colour));
}
}

/**
* @EFFECTS: Updates the board according to the given move, and returns {@code false} if the game is over.
* @EFFECTS: Updates the board according to the given move.
* @MODIFIES: {@code this}
* @REQUIRES: {@code move.isValid()}
*/
public boolean doMove(Move move) {
boolean isGameOver = move.getEnd().getPiece() instanceof King;
public void doMove(Move move) {
isGameOver = move.getEnd().getPiece() instanceof King;

// Handle "special" moves.
if (move.getStart().getPiece() instanceof Pawn) {
Expand All @@ -85,32 +63,28 @@ public boolean doMove(Move move) {
if (move.getEnd().getPiece() instanceof Pawn) {
doPromotion(move);
}

return isGameOver;
}

/**
* @EFFECTS: Returns a string representation of the board for display.
* @EFFECTS: Returns the set of all squares visible to the current player colour.
*/
public String getDisplayString(Colour colour) {
Set<Square> visibleSquares = getVisibleSquares(colour);
StringBuilder stringBuilder = new StringBuilder();

// Iterate up or down depending on the current player to visually "flip" the board.
for (int y = colour.getDirection() < 0 ? 0 : SIZE - 1; !isOutOfBounds(0, y); y -= colour.getDirection()) {
stringBuilder.append(y + 1).append(" ");
for (int x = colour.getDirection() > 0 ? 0 : SIZE - 1;
!isOutOfBounds(x, 0); x += colour.getDirection()) {
stringBuilder.append(getDisplaySymbol(visibleSquares, getSquare(x, y), colour)).append(" ");
public Set<Square> getVisibleSquares() {
Set<Square> visibleSquares = new HashSet<>();

for (Square square : gameState) {
if (square.hasPiece() && square.getPiece().getColour() == getCurrentPlayer()) {
visibleSquares.add(square);
visibleSquares.addAll(square.getPiece().getValidSquares(this, square));
}
stringBuilder.append("\n");
}
stringBuilder.append(" ");
for (int x = colour.getDirection() > 0 ? 0 : SIZE - 1; !isOutOfBounds(x, 0); x += colour.getDirection()) {
stringBuilder.append((char) (x + 'a')).append(" ");
}
return visibleSquares;
}

return stringBuilder.toString();
/**
* @EFFECTS: Returns the player colour whose turn it currently is.
*/
public Colour getCurrentPlayer() {
return Colour.values()[history.size() % Colour.values().length];
}

/**
Expand All @@ -132,14 +106,30 @@ public List<Move> getHistory() {
return history;
}

public boolean isGameOver() {
return isGameOver;
}

/**
* @EFFECTS: Removes all pieces of the given colour from the board.
* @EFFECTS: Sets all pieces in their starting positions.
* @MODIFIES: {@code this}
*/
private void clearPieces(Colour colour) {
for (Square square : gameState) {
if (square.hasPiece() && square.getPiece().getColour() == colour) {
square.setPiece(null);
private void setupPieces() {
for (Colour colour : Colour.values()) {
int y = colour.getDirection() > 0 ? 0 : SIZE - 1;

// No better way than to hard-code this...
getSquare(0, y).setPiece(new Rook(colour));
getSquare(1, y).setPiece(new Knight(colour));
getSquare(2, y).setPiece(new Bishop(colour));
getSquare(3, y).setPiece(new Queen(colour));
getSquare(SIZE - 4, y).setPiece(new King(colour));
getSquare(SIZE - 3, y).setPiece(new Bishop(colour));
getSquare(SIZE - 2, y).setPiece(new Knight(colour));
getSquare(SIZE - 1, y).setPiece(new Rook(colour));

for (int i = 0; i < SIZE; i++) {
getSquare(i, y + colour.getDirection()).setPiece(new Pawn(colour));
}
}
}
Expand Down Expand Up @@ -206,33 +196,4 @@ private void doPromotion(Move move) {
move.getEnd().setPiece(new Queen(pawn.getColour()));
}
}

/**
* @EFFECTS: Returns the set of all squares visible to the given player colour.
*/
private Set<Square> getVisibleSquares(Colour colour) {
Set<Square> visibleSquares = new HashSet<>();
for (Square square : gameState) {
if (square.hasPiece() && square.getPiece().getColour() == colour) {
visibleSquares.add(square);
visibleSquares.addAll(square.getPiece().getValidSquares(this, square));
}
}
return visibleSquares;
}

/**
* @EFFECTS: Returns a string representation of the given square.
*/
private String getDisplaySymbol(Set<Square> visibleSquares, Square square, Colour colour) {
if (!visibleSquares.contains(square)) {
return " ";
} else if (!square.hasPiece()) {
return ".";
} else if (square.getPiece().getColour() == colour) {
return square.getPiece().getPrefix();
}
return square.getPiece().getPrefix().toLowerCase();
}
}

}
1 change: 0 additions & 1 deletion src/main/model/piece/Bishop.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Set<Square> getValidSquares(Board board, Square start) {
validSquares.add(square);
}
}

return validSquares;
}

Expand Down
7 changes: 1 addition & 6 deletions src/main/model/piece/King.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
*/
public class King extends Piece implements FirstMove {
private static final String PREFIX = "K";
private static final Direction[] MOVE_DIRECTIONS = {
Direction.EAST, Direction.NORTHEAST, Direction.NORTH, Direction.NORTHWEST,
Direction.WEST, Direction.SOUTHWEST, Direction.SOUTH, Direction.SOUTHEAST
};
private static final Direction[] CASTLE_DIRECTIONS = {Direction.EAST, Direction.WEST};
private static final int[] CASTLE_OFFSETS_X = {2, -2};

Expand All @@ -37,7 +33,7 @@ public King(Colour colour) {
public Set<Square> getValidSquares(Board board, Square start) {
Set<Square> validSquares = new HashSet<>();

for (Direction direction : MOVE_DIRECTIONS) {
for (Direction direction : Direction.values()) {
// Apply offset to starting square based on direction.
int x = start.getX() + direction.getX();
int y = start.getY() + direction.getY();
Expand All @@ -51,7 +47,6 @@ public Set<Square> getValidSquares(Board board, Square start) {
}
}
}

addCastleSquares(validSquares, board, start);
return validSquares;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/model/piece/Knight.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public Set<Square> getValidSquares(Board board, Square start) {
}
}
}

return validSquares;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/model/piece/Pawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Represents a pawn piece.
*/
public class Pawn extends Piece implements FirstMove {
private static final String PREFIX = "P";
private static final String PREFIX = "";
private static final int[] CAPTURE_OFFSETS_X = {1, -1};

private boolean hasMoved;
Expand Down Expand Up @@ -50,7 +50,6 @@ public Set<Square> getValidSquares(Board board, Square start) {
}
}
}

addCaptureSquares(validSquares, board, start);
return validSquares;
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/model/piece/Queen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
*/
public class Queen extends Piece {
private static final String PREFIX = "Q";
private static final Direction[] MOVE_DIRECTIONS = {
Direction.EAST, Direction.NORTHEAST, Direction.NORTH, Direction.NORTHWEST,
Direction.WEST, Direction.SOUTHWEST, Direction.SOUTH, Direction.SOUTHEAST
};

/**
* @EFFECTS: Constructs a new queen with the given params.
Expand All @@ -32,7 +28,7 @@ public Queen(Colour colour) {
public Set<Square> getValidSquares(Board board, Square start) {
Set<Square> validSquares = new HashSet<>();

for (Direction direction : MOVE_DIRECTIONS) {
for (Direction direction : Direction.values()) {
// Apply offset to starting square based on direction until out of bounds.
for (int x = start.getX() + direction.getX(), y = start.getY() + direction.getY();
!board.isOutOfBounds(x, y); x += direction.getX(), y += direction.getY()) {
Expand Down
1 change: 0 additions & 1 deletion src/main/model/piece/Rook.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public Set<Square> getValidSquares(Board board, Square start) {
validSquares.add(square);
}
}

return validSquares;
}

Expand Down
Loading

0 comments on commit de089da

Please sign in to comment.