Fix issue with move piece
parent
2dac55a528
commit
b2a2b493da
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue