Last move highlight + bugfixes
This commit is contained in:
parent
fbceecd80f
commit
e6eecfd671
|
@ -198,21 +198,26 @@ public abstract class AbstractPiece implements IPiece {
|
|||
}
|
||||
|
||||
public void setPossibleMove(boolean[][] moves, int x, int y) {
|
||||
if(x == this.x && y == this.y) return ;
|
||||
if(x == this.x && y == this.y) return;
|
||||
if(x < 0 || x >= moves.length || y < 0 || y >= moves.length) return;
|
||||
IPiece piece = chessboard.getPiece(new PiecePosition(x, y));
|
||||
if(piece != null && color == piece.getColor()) return;
|
||||
moves[y][x] = true;
|
||||
}
|
||||
|
||||
public void move(PiecePosition pos) {
|
||||
public boolean move(PiecePosition pos) {
|
||||
boolean[][] moves = getPossibleMoves();
|
||||
if(moves[pos.y][pos.x]) {
|
||||
boolean[][] lastMove = new boolean[chessboard.SQUARE_COUNT][chessboard.SQUARE_COUNT];
|
||||
lastMove[y][x] = true;
|
||||
lastMove[pos.y][pos.x] = true;
|
||||
chessboard.showLastMove(lastMove);
|
||||
chessboard.returnPiece(pos);
|
||||
moveCount++;
|
||||
}
|
||||
chessboard.returnPiece(null);
|
||||
chessboard.showPossibleMoves(null);
|
||||
return moves[pos.y][pos.x];
|
||||
}
|
||||
|
||||
public void tracePath(boolean[][] moves, int xDirection, int yDirection) {
|
||||
|
|
|
@ -20,7 +20,7 @@ public class Chessboard extends JPanel {
|
|||
/**
|
||||
* Width of the chessboard
|
||||
*/
|
||||
private final double BOARD_WIDTH = SQUARE_COUNT*SQUARE_SIZE;;
|
||||
private final double BOARD_WIDTH = SQUARE_COUNT*SQUARE_SIZE;
|
||||
|
||||
/**
|
||||
* Scale of the chessboard
|
||||
|
@ -54,6 +54,8 @@ public class Chessboard extends JPanel {
|
|||
|
||||
private boolean[][] possibleMoves;
|
||||
|
||||
private boolean[][] lastMove;
|
||||
|
||||
/**
|
||||
* Constructor of the chessboard
|
||||
*/
|
||||
|
@ -123,8 +125,10 @@ public class Chessboard extends JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
isBlack = true;
|
||||
g2.setStroke(new BasicStroke(10));
|
||||
for(int i=0; i<SQUARE_COUNT; i++) {
|
||||
isBlack = !isBlack;
|
||||
for(int j=0; j<SQUARE_COUNT; j++) {
|
||||
if(possibleMoves != null && possibleMoves[j][i]) {
|
||||
g2.setTransform(beforeSquares);
|
||||
|
@ -134,6 +138,12 @@ public class Chessboard extends JPanel {
|
|||
g2.setColor(new Color(255, 50, 50));
|
||||
g2.drawRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
|
||||
}
|
||||
if(lastMove != null && lastMove[j][i]) {
|
||||
g2.setTransform(beforeSquares);
|
||||
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
|
||||
g2.setColor(new Color(0, 0, 200, 25));
|
||||
g2.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -242,5 +252,8 @@ public class Chessboard extends JPanel {
|
|||
void showPossibleMoves(boolean[][] moves) {
|
||||
possibleMoves = moves;
|
||||
}
|
||||
void showLastMove(boolean[][] move) {
|
||||
lastMove = move;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Chessboard mouse adapter class
|
||||
*/
|
||||
public class ChessboardMouseAdapter extends MouseAdapter {
|
||||
|
||||
PieceColor activeColor = PieceColor.WHITE;
|
||||
|
||||
/**
|
||||
* Detect mouse press and select the piece
|
||||
* @param me mouse event
|
||||
|
@ -13,8 +17,10 @@ public class ChessboardMouseAdapter extends MouseAdapter {
|
|||
public void mousePressed(MouseEvent me) {
|
||||
Chessboard c = (Chessboard) me.getSource();
|
||||
PiecePosition pos = c.getPieceCoordinates(me.getX(), me.getY());
|
||||
IPiece piece = c.grabPiece(pos);
|
||||
IPiece piece = c.getPiece(pos);
|
||||
if (piece != null) {
|
||||
if(piece.getColor() != activeColor) return;
|
||||
c.grabPiece(pos);
|
||||
double totalScale = c.pieceScale * c.boardScale;
|
||||
piece.setOverride(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
|
||||
c.repaint();
|
||||
|
@ -47,7 +53,10 @@ public class ChessboardMouseAdapter extends MouseAdapter {
|
|||
PiecePosition pos = c.getPieceCoordinates(me.getX(), me.getY());
|
||||
IPiece piece = c.getFloatingPiece();
|
||||
if(piece != null) {
|
||||
piece.move(pos);
|
||||
if(piece.move(pos)) {
|
||||
List<PieceColor> colors = List.of(PieceColor.values());
|
||||
activeColor = colors.get((colors.indexOf(activeColor)+1)%colors.size());
|
||||
}
|
||||
}
|
||||
c.getRootPane().repaint();
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public interface IPiece {
|
|||
|
||||
PieceColor getColor();
|
||||
|
||||
void move(PiecePosition pos);
|
||||
boolean move(PiecePosition pos);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Knight piece class
|
||||
|
|
|
@ -2,8 +2,6 @@ import java.awt.*;
|
|||
import java.awt.geom.Area;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Path2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Queen piece class
|
||||
|
|
Loading…
Reference in a new issue