Fix issue with move piece

main
Brett 2022-12-18 00:00:19 -05:00
parent 2dac55a528
commit b2a2b493da
3 changed files with 25 additions and 3 deletions

View File

@ -33,7 +33,12 @@ public class Board {
board[size()-4][size()-1] = new King(this, false, size() - 4, size()-1); 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){ public boolean movePiece(int x, int y, int newX, int newY){
System.out.println(x + " " + y + " || " + newX + " " + newY);
ChessPiece selectedPiece; ChessPiece selectedPiece;
// make sure the place we are moving from has a piece // make sure the place we are moving from has a piece
if ((selectedPiece = get(x, y)) == null) if ((selectedPiece = get(x, y)) == null)
@ -46,13 +51,14 @@ public class Board {
ChessPiece movedPiece = get(m); 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 // 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 // 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; return false;
// if we were unable to set the piece down we failed to move the piece // if we were unable to set the piece down we failed to move the piece
if (!set(m, selectedPiece)) if (!set(m, selectedPiece))
return false; return false;
// run special conditions. Only matters for pieces which have special conditions, since is defaulted to empty body. // 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); set(x, y, null);
return true; return true;
} }

View File

@ -24,6 +24,9 @@ public abstract class ChessPiece {
public boolean isWhite(){ public boolean isWhite(){
return isWhite; return isWhite;
} }
public Move getPosition(){
return new Move(x, y);
}
public boolean isFirstMove(){ public boolean isFirstMove(){
return isFirstMove; return isFirstMove;

View File

@ -48,6 +48,20 @@ public class Display extends JFrame implements MouseListener {
for (Move m : moves) { for (Move m : moves) {
drawSelectionRect(g, m); 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) // handle user input (mouse clicking)
@ -59,7 +73,6 @@ public class Display extends JFrame implements MouseListener {
movingPointX = localPointX; movingPointX = localPointX;
movingPointY = localPointY; movingPointY = localPointY;
} }
System.out.println(localPointX + " " + localPointY);
selectionPointY = -1; selectionPointY = -1;
selectionPointX = -1; selectionPointX = -1;
} }