This commit is contained in:
Jacob Signorovitch
2025-05-26 23:13:10 -04:00
parent a66e4bb775
commit 83bc3d192a
3 changed files with 108 additions and 29 deletions

View File

@@ -120,44 +120,91 @@ class Board {
} }
// Return the line of free spaces above. // Return the line of free spaces above.
ArrayList<Coord> getLineAbove(Coord coord) { ArrayList<Coord> getLineAbove(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>(); ArrayList<Coord> line = new ArrayList<>();
for (int y = coord.y - 1; y >= 0; y--) { for (int y = coord.y - 1; y >= 0; y--) {
Coord next = new Coord(coord.x, y); Coord next = new Coord(coord.x, y);
if (!this.isFree(next, Util.Col.WHITE)) break; if (!this.isFree(next, col)) break;
line.add(next); line.add(next);
} }
return line; return line;
} }
// Return the line of free spaces below. // Return the line of free spaces below.
ArrayList<Coord> getLineBelow(Coord coord) { ArrayList<Coord> getLineBelow(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>(); ArrayList<Coord> line = new ArrayList<>();
for (int y = coord.y + 1; y < Util.boardW; y++) { for (int y = coord.y + 1; y < Util.boardW; y++) {
Coord next = new Coord(coord.x, y); Coord next = new Coord(coord.x, y);
if (!this.isFree(next, Util.Col.WHITE)) break; if (!this.isFree(next, col)) break;
line.add(next); line.add(next);
} }
return line; return line;
} }
// Return the line of free spaces to the left. // Return the line of free spaces to the left.
ArrayList<Coord> getLineLeft(Coord coord) { ArrayList<Coord> getLineLeft(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>(); ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x - 1; x >= 0; x--) { for (int x = coord.x - 1; x >= 0; x--) {
Coord next = new Coord(x, coord.y); Coord next = new Coord(x, coord.y);
if (!this.isFree(next, Util.Col.WHITE)) break; if (!this.isFree(next, col)) break;
line.add(next); line.add(next);
} }
return line; return line;
} }
// Return the line of free spaces to the right. // Return the line of free spaces to the right.
ArrayList<Coord> getLineRight(Coord coord) { ArrayList<Coord> getLineRight(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>(); ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x + 1; x < Util.boardW; x++) { for (int x = coord.x + 1; x < Util.boardW; x++) {
Coord next = new Coord(x, coord.y); Coord next = new Coord(x, coord.y);
if (!this.isFree(next, Util.Col.WHITE)) break; if (!this.isFree(next, col)) break;
line.add(next);
}
return line;
}
// Return the line of free spaces diagonaly NE.
ArrayList<Coord> getLineDiagNE(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x + 1, y = coord.y + 1;
x < Util.boardW && y < Util.boardW; x++, y++) {
Coord next = new Coord(x, y);
if (!this.isFree(next, col)) break;
line.add(next);
}
return line;
}
// Return the line of free spaces diagonaly NW.
ArrayList<Coord> getLineDiagNW(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x - 1, y = coord.y + 1; x >= 0 && y < Util.boardW;
x--, y++) {
Coord next = new Coord(x, y);
if (!this.isFree(next, col)) break;
line.add(next);
}
return line;
}
// Return the line of free spaces diagonaly SW.
ArrayList<Coord> getLineDiagSW(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x - 1, y = coord.y - 1; x >= 0 && y >= 0; x--, y--) {
Coord next = new Coord(x, y);
if (!this.isFree(next, col)) break;
line.add(next);
}
return line;
}
// Return the line of free spaces diagonaly SE.
ArrayList<Coord> getLineDiagSE(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>();
for (int x = coord.x + 1, y = coord.y - 1; x < Util.boardW && y >= 0;
x++, y--) {
Coord next = new Coord(x, y);
if (!this.isFree(next, col)) break;
line.add(next); line.add(next);
} }
return line; return line;

View File

@@ -67,13 +67,17 @@ class Rook 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>();
for (int i = -7; i < 8; i++) {
if (i == 0) continue; // Can't move to self. ArrayList<Coord> above = board.getLineAbove(coord, this.col);
moves.add(new Coord(coord.x, coord.y + i)); ArrayList<Coord> below = board.getLineBelow(coord, this.col);
moves.add(new Coord(coord.x + i, coord.y)); ArrayList<Coord> left = board.getLineLeft(coord, this.col);
} ArrayList<Coord> right = board.getLineRight(coord, this.col);
moves.addAll(above);
moves.addAll(below);
moves.addAll(left);
moves.addAll(right);
return moves; return moves;
} }
} }
@@ -87,7 +91,7 @@ class Knight 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>();
moves.add(new Coord(coord.x + 1, coord.y + 2)); moves.add(new Coord(coord.x + 1, coord.y + 2));
moves.add(new Coord(coord.x + 2, coord.y + 1)); moves.add(new Coord(coord.x + 2, coord.y + 1));
@@ -99,7 +103,10 @@ class Knight extends Piece {
moves.add(new Coord(coord.x - 2, coord.y + 1)); moves.add(new Coord(coord.x - 2, coord.y + 1));
moves.add(new Coord(coord.x - 1, coord.y - 2)); moves.add(new Coord(coord.x - 1, coord.y - 2));
moves.add(new Coord(coord.x - 2, coord.y - 1)); moves.add(new Coord(coord.x - 2, coord.y - 1));
return moves;
return new ArrayList<Coord>(
moves.stream().filter(c -> board.isFree(c, this.col)).toList()
);
} }
} }
@@ -112,13 +119,24 @@ class Bishop 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>();
/*
for (int i = -7; i < 8; i++) { for (int i = -7; i < 8; i++) {
if (i == 0) continue; // Can't move to own position. if (i == 0) continue; // Can't move to own position.
moves.add(new Coord(coord.x + i, coord.y + i)); moves.add(new Coord(coord.x + i, coord.y + i));
moves.add(new Coord(coord.x + i, coord.y - i)); moves.add(new Coord(coord.x + i, coord.y - i));
} }*/
ArrayList<Coord> ne = board.getLineDiagNE(coord, this.col);
ArrayList<Coord> nw = board.getLineDiagNW(coord, this.col);
ArrayList<Coord> se = board.getLineDiagSE(coord, this.col);
ArrayList<Coord> sw = board.getLineDiagSW(coord, this.col);
moves.addAll(ne);
moves.addAll(nw);
moves.addAll(se);
moves.addAll(sw);
return moves; return moves;
} }
} }
@@ -132,16 +150,21 @@ class Queen 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>();
for (int i = -7; i < 8; i++) {
if (i == 0) continue; // Can't move to own position. moves.addAll(board.getLineAbove(coord, this.col));
moves.add(new Coord(coord.x + i, coord.y + i)); moves.addAll(board.getLineBelow(coord, this.col));
moves.add(new Coord(coord.x + i, coord.y - i)); moves.addAll(board.getLineLeft(coord, this.col));
moves.add(new Coord(coord.x, coord.y + i)); moves.addAll(board.getLineRight(coord, this.col));
moves.add(new Coord(coord.x + i, coord.y)); moves.addAll(board.getLineDiagNE(coord, this.col));
} moves.addAll(board.getLineDiagNW(coord, this.col));
return moves; moves.addAll(board.getLineDiagSE(coord, this.col));
moves.addAll(board.getLineDiagSW(coord, this.col));
return new ArrayList<Coord>(
moves.stream().filter(c -> board.isFree(c, this.col)).toList()
);
} }
} }
@@ -154,7 +177,7 @@ class King 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>();
moves.add(new Coord(coord.x, coord.y + 1)); moves.add(new Coord(coord.x, coord.y + 1));
moves.add(new Coord(coord.x, coord.y - 1)); moves.add(new Coord(coord.x, coord.y - 1));
@@ -164,6 +187,9 @@ class King extends Piece {
moves.add(new Coord(coord.x + 1, coord.y - 1)); moves.add(new Coord(coord.x + 1, coord.y - 1));
moves.add(new Coord(coord.x - 1, coord.y + 1)); moves.add(new Coord(coord.x - 1, coord.y + 1));
moves.add(new Coord(coord.x - 1, coord.y - 1)); moves.add(new Coord(coord.x - 1, coord.y - 1));
return moves;
return new ArrayList<Coord>(
moves.stream().filter(c -> board.isFree(c, this.col)).toList()
);
} }
} }

View File

@@ -46,6 +46,12 @@ class Player extends Actor {
this.board.undisplayMoves(moves); this.board.undisplayMoves(moves);
this.board.unselect(this.selected); this.board.unselect(this.selected);
this.selected = null; this.selected = null;
if (piece != null && piece.col.equals(this.col)) {
this.selected = coord;
this.board.displaySelected(coord);
this.board.displayMoves(coord);
}
return; return;
} }
} }