From 1f578b07709f0d098999c0b2a09631ca0bba7ede Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 18 Feb 2023 20:14:16 -0800 Subject: [PATCH] Make piece move restrictions, minor bugfixes --- src/Board.java | 26 ++++++++++++++++++++------ src/Piece.java | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Board.java b/src/Board.java index c76e1da..719a903 100644 --- a/src/Board.java +++ b/src/Board.java @@ -1,5 +1,6 @@ import java.awt.Color; import java.awt.Graphics; +import java.util.ArrayList; public class Board { // Width and height of each board tile @@ -46,11 +47,21 @@ public class Board { } } + public boolean outOfBounds(int x, int y) { + return x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE; + } + + public boolean outOfBounds(BoardCoordinate coordinate) { + return outOfBounds(coordinate.x, coordinate.y); + } + public Piece get(int x, int y) { + if (outOfBounds(x, y)) return null; return board[y][x]; } public Piece get(BoardCoordinate coordinate) { + if (coordinate == null) return null; return get(coordinate.x, coordinate.y); } @@ -88,12 +99,15 @@ public class Board { // Get board coordinate of mouse release BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard(); // Only do something if new coordinate is different from the originating coordinate - if (!dragging.equals(newCoordinate)) { - // Get piece on target coordinate - Piece capturedPiece = get(newCoordinate); - // Only move piece to square if there's nothing there, or they are of differing colors - if (capturedPiece == null || capturedPiece.black != get(dragging).black) { - move(dragging, newCoordinate); + if (dragging != null && !newCoordinate.equals(dragging)) { + // dragging is BoardCoordinate of piece being dragged + Piece piece = get(dragging); + ArrayList legalMoves = piece.getLegalMoves(dragging, this); + for (BoardCoordinate legalMove : legalMoves) { + if (newCoordinate.equals(legalMove)) { + move(dragging, newCoordinate); + break; + } } } // Clear dragging diff --git a/src/Piece.java b/src/Piece.java index 8b42338..c991225 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.ArrayList; public class Piece { // Width and height of placeholder rectangle graphic @@ -12,6 +13,37 @@ public class Piece { this.black = black; } + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + possibleMoves.add(new BoardCoordinate(position.x - 1, position.y)); // to left + possibleMoves.add(new BoardCoordinate(position.x + 1, position.y)); // to right + possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); // to up + possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); // to down + return possibleMoves; + } + + public ArrayList getLegalMoves(BoardCoordinate position, Board board) { + ArrayList legalMoves = getPossibleMoves(position); + for (int i = 0; i < legalMoves.size(); i++) { + BoardCoordinate possibleMove = legalMoves.get(i); + Piece targetPiece = board.get(possibleMove); + if ( + // other piece of same color + (targetPiece != null && targetPiece.black == black) || + + // outside of bounds of board + possibleMove.x < 0 || + possibleMove.y < 0 || + possibleMove.x >= Board.BOARD_SIZE || + possibleMove.y >= Board.BOARD_SIZE) { + legalMoves.remove(i); + i--; + } + // TODO: puts us into check + } + return legalMoves; + } + // The Piece class doesn't store position, // so when drawing we need to be provided this along with a graphics context when drawing public void draw(Graphics graphics, int x, int y) {