This commit is contained in:
Jacob Signorovitch
2025-05-20 21:48:09 -04:00
parent efd742ce83
commit 585947b4ee
2 changed files with 17 additions and 4 deletions

View File

@@ -75,7 +75,7 @@ class Board {
void displayMoves(Coord coord) { void displayMoves(Coord coord) {
Piece piece = this.get(coord); Piece piece = this.get(coord);
if (piece == null) return; if (piece == null) return;
ArrayList<Coord> moves = piece.moves(coord); ArrayList<Coord> moves = piece.moves(coord, this);
for (Coord move : moves) this.info.put(move, Info.CANMOVE); for (Coord move : moves) this.info.put(move, Info.CANMOVE);
} }
@@ -102,7 +102,7 @@ class Board {
ArrayList<Coord> getMoves(Coord coord) { ArrayList<Coord> getMoves(Coord coord) {
Piece piece = this.get(coord); Piece piece = this.get(coord);
return piece.moves(coord); return piece.moves(coord, this);
} }
} }

View File

@@ -20,7 +20,9 @@ abstract class Piece {
WorldImage draw() { return new EmptyImage(); }; WorldImage draw() { return new EmptyImage(); };
// Return list of possible move locations, centered around the given coord. // Return list of possible move locations, centered around the given coord.
ArrayList<Coord> moves(Coord coord) { return new ArrayList<Coord>(); } ArrayList<Coord> moves(Coord coord, Board board) {
return new ArrayList<Coord>();
}
} }
class Pawn extends Piece { class Pawn extends Piece {
@@ -32,8 +34,19 @@ class Pawn extends Piece {
); );
} }
ArrayList<Coord> moves(Coord coord) { ArrayList<Coord> moves(Coord coord, Board board) {
ArrayList<Coord> moves = new ArrayList<Coord>(); ArrayList<Coord> moves = new ArrayList<Coord>();
if (coord.rel(this.col).y == 1) {
moves.add(new Coord(
coord.x, coord.y + (this.col.equals(Col.BLACK) ? 2 : -2)
));
}
if (!board
.get(new Coord(
coord.x + 1,
coord.y + (this.col.equals(Col.BLACK) ? 1 : -1)
))
.col.equals(this.col)) {}
moves.add( moves.add(
new Coord(coord.x, coord.y + (this.col.equals(Col.BLACK) ? 1 : -1)) new Coord(coord.x, coord.y + (this.col.equals(Col.BLACK) ? 1 : -1))
); );