From 83bc3d192ac7e21e971e58ad0df42da3e11671c6 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Mon, 26 May 2025 23:13:10 -0400 Subject: [PATCH] f --- chess/src/chess/Board.java | 63 +++++++++++++++++++++++++++++----- chess/src/chess/Piece.java | 68 +++++++++++++++++++++++++------------ chess/src/chess/Player.java | 6 ++++ 3 files changed, 108 insertions(+), 29 deletions(-) diff --git a/chess/src/chess/Board.java b/chess/src/chess/Board.java index 2923915..7fe968a 100644 --- a/chess/src/chess/Board.java +++ b/chess/src/chess/Board.java @@ -120,44 +120,91 @@ class Board { } // Return the line of free spaces above. - ArrayList getLineAbove(Coord coord) { + ArrayList getLineAbove(Coord coord, Util.Col col) { ArrayList line = new ArrayList<>(); for (int y = coord.y - 1; y >= 0; 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); } return line; } // Return the line of free spaces below. - ArrayList getLineBelow(Coord coord) { + ArrayList getLineBelow(Coord coord, Util.Col col) { ArrayList line = new ArrayList<>(); for (int y = coord.y + 1; y < Util.boardW; 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); } return line; } // Return the line of free spaces to the left. - ArrayList getLineLeft(Coord coord) { + ArrayList getLineLeft(Coord coord, Util.Col col) { ArrayList line = new ArrayList<>(); for (int x = coord.x - 1; x >= 0; x--) { 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 to the right. - ArrayList getLineRight(Coord coord) { + ArrayList getLineRight(Coord coord, Util.Col col) { ArrayList line = new ArrayList<>(); for (int x = coord.x + 1; x < Util.boardW; x++) { 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 getLineDiagNE(Coord coord, Util.Col col) { + ArrayList 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 getLineDiagNW(Coord coord, Util.Col col) { + ArrayList 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 getLineDiagSW(Coord coord, Util.Col col) { + ArrayList 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 getLineDiagSE(Coord coord, Util.Col col) { + ArrayList 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); } return line; diff --git a/chess/src/chess/Piece.java b/chess/src/chess/Piece.java index 519e1d8..8a3a4f0 100644 --- a/chess/src/chess/Piece.java +++ b/chess/src/chess/Piece.java @@ -67,13 +67,17 @@ class Rook extends Piece { ); } - ArrayList moves(Coord coord) { + ArrayList moves(Coord coord, Board board) { ArrayList moves = new ArrayList(); - for (int i = -7; i < 8; i++) { - if (i == 0) continue; // Can't move to self. - moves.add(new Coord(coord.x, coord.y + i)); - moves.add(new Coord(coord.x + i, coord.y)); - } + + ArrayList above = board.getLineAbove(coord, this.col); + ArrayList below = board.getLineBelow(coord, this.col); + ArrayList left = board.getLineLeft(coord, this.col); + ArrayList right = board.getLineRight(coord, this.col); + moves.addAll(above); + moves.addAll(below); + moves.addAll(left); + moves.addAll(right); return moves; } } @@ -87,7 +91,7 @@ class Knight extends Piece { ); } - ArrayList moves(Coord coord) { + ArrayList moves(Coord coord, Board board) { ArrayList moves = new ArrayList(); moves.add(new Coord(coord.x + 1, coord.y + 2)); 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 - 1, coord.y - 2)); moves.add(new Coord(coord.x - 2, coord.y - 1)); - return moves; + + return new ArrayList( + moves.stream().filter(c -> board.isFree(c, this.col)).toList() + ); } } @@ -112,13 +119,24 @@ class Bishop extends Piece { ); } - ArrayList moves(Coord coord) { + ArrayList moves(Coord coord, Board board) { ArrayList moves = new ArrayList(); + /* for (int i = -7; i < 8; i++) { 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)); - } + }*/ + ArrayList ne = board.getLineDiagNE(coord, this.col); + ArrayList nw = board.getLineDiagNW(coord, this.col); + ArrayList se = board.getLineDiagSE(coord, this.col); + ArrayList sw = board.getLineDiagSW(coord, this.col); + + moves.addAll(ne); + moves.addAll(nw); + moves.addAll(se); + moves.addAll(sw); + return moves; } } @@ -132,16 +150,21 @@ class Queen extends Piece { ); } - ArrayList moves(Coord coord) { + ArrayList moves(Coord coord, Board board) { ArrayList moves = new ArrayList(); - for (int i = -7; i < 8; i++) { - 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, coord.y + i)); - moves.add(new Coord(coord.x + i, coord.y)); - } - return moves; + + moves.addAll(board.getLineAbove(coord, this.col)); + moves.addAll(board.getLineBelow(coord, this.col)); + moves.addAll(board.getLineLeft(coord, this.col)); + moves.addAll(board.getLineRight(coord, this.col)); + moves.addAll(board.getLineDiagNE(coord, this.col)); + moves.addAll(board.getLineDiagNW(coord, this.col)); + moves.addAll(board.getLineDiagSE(coord, this.col)); + moves.addAll(board.getLineDiagSW(coord, this.col)); + + return new ArrayList( + moves.stream().filter(c -> board.isFree(c, this.col)).toList() + ); } } @@ -154,7 +177,7 @@ class King extends Piece { ); } - ArrayList moves(Coord coord) { + ArrayList moves(Coord coord, Board board) { ArrayList moves = new ArrayList(); 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)); - return moves; + + return new ArrayList( + moves.stream().filter(c -> board.isFree(c, this.col)).toList() + ); } } diff --git a/chess/src/chess/Player.java b/chess/src/chess/Player.java index a4e6fa0..ce4f8a4 100644 --- a/chess/src/chess/Player.java +++ b/chess/src/chess/Player.java @@ -46,6 +46,12 @@ class Player extends Actor { this.board.undisplayMoves(moves); this.board.unselect(this.selected); this.selected = null; + + if (piece != null && piece.col.equals(this.col)) { + this.selected = coord; + this.board.displaySelected(coord); + this.board.displayMoves(coord); + } return; } }