From 023d0b9a4bfece216561d3d580c3db9599a11d3e Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 10 Mar 2023 15:16:32 -0800 Subject: [PATCH] Implement prevention of teleportation for possible moves --- src/Bishop.java | 16 ++++++++-------- src/Board.java | 3 --- src/King.java | 2 +- src/Knight.java | 2 +- src/Pawn.java | 2 +- src/Piece.java | 15 +++++++++++++-- src/Queen.java | 2 +- src/Rook.java | 2 +- 8 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Bishop.java b/src/Bishop.java index 6b47dfd..d9401d2 100644 --- a/src/Bishop.java +++ b/src/Bishop.java @@ -5,14 +5,14 @@ public class Bishop extends Piece { super(black, panel, "black-bishop.png", "white-bishop.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); - for(int i = 1; i <= 7; i++) { - possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); - } + + getPossibleMovesInDirection(1, 1, position, board, possibleMoves); + getPossibleMovesInDirection(-1, -1, position, board, possibleMoves); + getPossibleMovesInDirection(1, -1, position, board, possibleMoves); + getPossibleMovesInDirection(-1, 1, position, board, possibleMoves); + return possibleMoves; } -} +} \ No newline at end of file diff --git a/src/Board.java b/src/Board.java index b48e736..66d5385 100644 --- a/src/Board.java +++ b/src/Board.java @@ -142,9 +142,6 @@ public class Board { } public void draw(ScreenCoordinate mousePosition) { - - - // Draw board graphics.setColor(BLACK); graphics.fillRect(0, 0, DIMENSION, DIMENSION); diff --git a/src/King.java b/src/King.java index 7510129..04397da 100644 --- a/src/King.java +++ b/src/King.java @@ -5,7 +5,7 @@ public class King extends Piece { super(black, panel, "black-king.png", "white-king.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1)); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1)); diff --git a/src/Knight.java b/src/Knight.java index f7c86d7..8412938 100644 --- a/src/Knight.java +++ b/src/Knight.java @@ -6,7 +6,7 @@ public class Knight extends Piece { super(black, panel, "black-knight.png", "white-knight.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1)); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 2)); diff --git a/src/Pawn.java b/src/Pawn.java index bc73ad4..6ccb100 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -5,7 +5,7 @@ public class Pawn extends Piece { super(black, panel, "black-pawn.png", "white-pawn.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); if (this.black) { possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); diff --git a/src/Piece.java b/src/Piece.java index fb23903..e092656 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -13,10 +13,10 @@ public abstract class Piece { image = panel.loadImage(black ? blackImagePath : whiteImagePath); } - public abstract ArrayList getPossibleMoves(BoardCoordinate position); + public abstract ArrayList getPossibleMoves(BoardCoordinate position, Board board); public ArrayList getLegalMoves(BoardCoordinate position, Board board) { - ArrayList legalMoves = getPossibleMoves(position); + ArrayList legalMoves = getPossibleMoves(position, board); for (int i = 0; i < legalMoves.size(); i++) { BoardCoordinate possibleMove = legalMoves.get(i); Piece targetPiece = board.get(possibleMove); @@ -46,4 +46,15 @@ public abstract class Piece { public void draw(Graphics graphics, ImageObserver observer, ScreenCoordinate coordinate) { draw(graphics, observer, coordinate.x, coordinate.y); } + + void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList possibleMoves) { + for ( + int x = position.x + dx, y = position.y + dy; + !board.outOfBounds(x, y); + x += dx, y += dy) { + BoardCoordinate coordinate = new BoardCoordinate(x, y); + possibleMoves.add(coordinate); + if (board.get(coordinate) != null) break; + } + } } diff --git a/src/Queen.java b/src/Queen.java index c388211..0e43fd3 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -5,7 +5,7 @@ public class Queen extends Piece { super(black, panel, "black-queen.png", "white-queen.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); for(int i = 1; i <= 7; i++) { possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); diff --git a/src/Rook.java b/src/Rook.java index 30dcd16..0325bd9 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -5,7 +5,7 @@ public class Rook extends Piece { super(black, panel, "black-rook.png", "white-rook.png"); } - public ArrayList getPossibleMoves(BoardCoordinate position) { + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); for(int i = 1; i <= 7; i++) { possibleMoves.add(new BoardCoordinate(position.x - i, position.y));