From b2a2b493daebffef17f158eb92f1b07c9cb904ae Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 18 Dec 2022 00:00:19 -0500 Subject: [PATCH] Fix issue with move piece --- src/chess/Board.java | 10 ++++++++-- src/chess/ChessPiece.java | 3 +++ src/ui/Display.java | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/chess/Board.java b/src/chess/Board.java index 4de2dd1..b6a4941 100644 --- a/src/chess/Board.java +++ b/src/chess/Board.java @@ -33,7 +33,12 @@ public class Board { board[size()-4][size()-1] = new King(this, false, size() - 4, size()-1); } + public boolean movePiece(Move movingPiece, Move newPos){ + return movePiece(movingPiece.getX(), movingPiece.getY(), newPos.getX(), newPos.getY()); + } + public boolean movePiece(int x, int y, int newX, int newY){ + System.out.println(x + " " + y + " || " + newX + " " + newY); ChessPiece selectedPiece; // make sure the place we are moving from has a piece if ((selectedPiece = get(x, y)) == null) @@ -46,13 +51,14 @@ public class Board { ChessPiece movedPiece = get(m); // make sure they are of the same color. Since we know this move is the position we want to move to // we can early exit because we are not allowed to move on top of our own pieces - if (selectedPiece.isWhite == movedPiece.isWhite) + if (movedPiece != null && selectedPiece.isWhite == movedPiece.isWhite) return false; // if we were unable to set the piece down we failed to move the piece if (!set(m, selectedPiece)) return false; // run special conditions. Only matters for pieces which have special conditions, since is defaulted to empty body. - selectedPiece.applySpecialMove(m); + if (movedPiece != null) + selectedPiece.applySpecialMove(m); set(x, y, null); return true; } diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index caaec22..5a960ad 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -24,6 +24,9 @@ public abstract class ChessPiece { public boolean isWhite(){ return isWhite; } + public Move getPosition(){ + return new Move(x, y); + } public boolean isFirstMove(){ return isFirstMove; diff --git a/src/ui/Display.java b/src/ui/Display.java index fa43024..ae9e9fc 100644 --- a/src/ui/Display.java +++ b/src/ui/Display.java @@ -48,6 +48,20 @@ public class Display extends JFrame implements MouseListener { for (Move m : moves) { drawSelectionRect(g, m); } + if (selectionPointX != -1 && selectionPointY != -1) { + int localPointX = (selectionPointX - xOffset) / width; + int localPointY = (b.size() - 1) - ((selectionPointY - yOffset) / height); + for (Move m : moves){ + if (m.getX() == localPointX && m.getY() == localPointY){ + b.movePiece(piece.getPosition(), new Move(localPointX, localPointY)); + selectionPointX = -1; + selectionPointY = -1; + movingPointX = -1; + movingPointY = -1; + return; + } + } + } } } // handle user input (mouse clicking) @@ -59,7 +73,6 @@ public class Display extends JFrame implements MouseListener { movingPointX = localPointX; movingPointY = localPointY; } - System.out.println(localPointX + " " + localPointY); selectionPointY = -1; selectionPointX = -1; }