highlight when kingInCheck function working
This commit is contained in:
parent
fdc91afa1a
commit
5ee54d97cd
|
|
@ -30,6 +30,8 @@ public class Board implements Cloneable {
|
|||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
||||
|
||||
private LinkedList<ArrayList<Piece>> boardHistory = new LinkedList<>();
|
||||
|
||||
private int[] kingCheckPos = null;
|
||||
|
||||
/* ====== AUDIO STATE ====== */
|
||||
private Clip moveClip;
|
||||
|
|
@ -429,7 +431,7 @@ public class Board implements Cloneable {
|
|||
moveClip = AudioSystem.getClip();
|
||||
moveClip.open(ais);
|
||||
|
||||
soundEnabled = true; // <<< MISSING LINE
|
||||
soundEnabled = true;
|
||||
System.out.println("Move-sound loaded OK");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -500,12 +502,11 @@ public class Board implements Cloneable {
|
|||
}
|
||||
|
||||
public void highlightKingInCheck() {
|
||||
|
||||
// Clear previous
|
||||
kingCheckPos = null;
|
||||
boolean isWhiteTurn = isTurnWhite();
|
||||
|
||||
// Determine the color of the current player
|
||||
boolean isTurnWhite = isTurnWhite();
|
||||
|
||||
|
||||
// Find the King
|
||||
Piece king = null;
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
|
||||
|
|
@ -513,37 +514,29 @@ public class Board implements Cloneable {
|
|||
break;
|
||||
}
|
||||
}
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
|
||||
king = piece;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (king == null) {
|
||||
throw new IllegalStateException("No king found for the current player!");
|
||||
}
|
||||
|
||||
if (king == null) {
|
||||
throw new IllegalStateException("No king found for the current player!");
|
||||
}
|
||||
|
||||
|
||||
// Opponent’s moves
|
||||
ArrayList<Move> opponentMoves = getAllLegalMoves(!isWhiteTurn);
|
||||
|
||||
|
||||
// See if any can capture the King
|
||||
for (Move move : opponentMoves) {
|
||||
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
|
||||
highlightedPositions.add(new int[]{king.getX(), king.getY()});
|
||||
return;
|
||||
kingCheckPos = new int[]{king.getX(), king.getY()};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if any opponent move targets the king's position
|
||||
for (Move move : opponentMoves) {
|
||||
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
|
||||
// If the king is in check, highlight its position
|
||||
highlightedPositions.add(new int[]{king.getX(), king.getY()});
|
||||
return; // No need to continue after finding a check
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isKingCheckSquare(int squareX, int squareY) {
|
||||
return (kingCheckPos != null
|
||||
&& kingCheckPos[0] == squareX
|
||||
&& kingCheckPos[1] == squareY);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -108,5 +108,12 @@ public class Game extends Thread {
|
|||
public void toggleAI(boolean isWhite) {
|
||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||
}
|
||||
public void highlightKingInCheck() {
|
||||
board.highlightKingInCheck();
|
||||
}
|
||||
|
||||
public boolean isKingCheckSquare(int squareX, int squareY) {
|
||||
return board.isKingCheckSquare(squareX, squareY);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,175 +22,195 @@ public class JPanelChessBoard extends JPanel {
|
|||
private Game myGame;
|
||||
private MyInterface interfaceGlobal;
|
||||
private BufferedImage spriteSheet;
|
||||
private int PIECE_WIDTH = 16; //in spritesheet
|
||||
private int PIECE_HEIGHT = 16; //in spritesheet
|
||||
private int PIECE_WIDTH = 16; // in spritesheet
|
||||
private int PIECE_HEIGHT = 16; // in spritesheet
|
||||
private int MARGIN = 6;
|
||||
|
||||
|
||||
private boolean pieceSelectorMode;
|
||||
private boolean selectedPieceIsWhite;
|
||||
private PieceType selectedPieceType;
|
||||
private boolean pieceAdderMode;
|
||||
|
||||
public JPanelChessBoard(MyInterface itf) {
|
||||
super();
|
||||
myGame = null;
|
||||
interfaceGlobal = itf;
|
||||
selectedPieceIsWhite = true;
|
||||
selectedPieceType = PieceType.Pawn;
|
||||
pieceSelectorMode = false;
|
||||
try {
|
||||
spriteSheet = ImageIO.read(new File("pieces.png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
pieceSelectorMode = false;
|
||||
pieceAdderMode = false;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(pieceSelectorMode) {
|
||||
int x = Math.round(me.getX()/cellWidth());
|
||||
selectedPieceType = PieceType.values()[5-x];
|
||||
selectedPieceIsWhite = (me.getY() > cellHeight());
|
||||
pieceSelectorMode = false;
|
||||
} else {
|
||||
if(myGame == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX()*myGame.getWidth())/getWidth();
|
||||
int y = (me.getY()*myGame.getHeight())/getHeight();
|
||||
if(pieceAdderMode) {
|
||||
//TODO
|
||||
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
||||
pieceAdderMode = false;
|
||||
} else {
|
||||
myGame.clickCoords(x,y);
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
super();
|
||||
myGame = null;
|
||||
interfaceGlobal = itf;
|
||||
selectedPieceIsWhite = true;
|
||||
selectedPieceType = PieceType.Pawn;
|
||||
pieceSelectorMode = false;
|
||||
try {
|
||||
spriteSheet = ImageIO.read(new File("pieces.png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
pieceSelectorMode = false;
|
||||
pieceAdderMode = false;
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(pieceSelectorMode) {
|
||||
int x = Math.round(me.getX()/cellWidth());
|
||||
selectedPieceType = PieceType.values()[5-x];
|
||||
selectedPieceIsWhite = (me.getY() > cellHeight());
|
||||
pieceSelectorMode = false;
|
||||
} else {
|
||||
if(myGame == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
int x = (me.getX() * myGame.getWidth()) / getWidth();
|
||||
int y = (me.getY() * myGame.getHeight()) / getHeight();
|
||||
|
||||
if(pieceAdderMode) {
|
||||
// TODO
|
||||
myGame.setPiece(selectedPieceIsWhite, selectedPieceType, x, y);
|
||||
pieceAdderMode = false;
|
||||
} else {
|
||||
myGame.clickCoords(x, y);
|
||||
|
||||
myGame.highlightKingInCheck();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setGame(Game simu) {
|
||||
myGame = simu;
|
||||
myGame = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if(pieceSelectorMode) {
|
||||
g.drawImage(
|
||||
spriteSheet,
|
||||
0,
|
||||
0,
|
||||
Math.round(5*cellWidth()),
|
||||
Math.round(2*cellHeight()),
|
||||
null
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (myGame != null) {
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = cellWidth();
|
||||
float cellHeight = cellHeight();
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if(pieceSelectorMode) {
|
||||
g.drawImage(
|
||||
spriteSheet,
|
||||
0,
|
||||
0,
|
||||
Math.round(5*cellWidth()),
|
||||
Math.round(2*cellHeight()),
|
||||
null
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
g.setColor(Color.white);
|
||||
for(int x=0; x<myGame.getWidth();x++) {
|
||||
for (int y=0; y<myGame.getHeight(); y++) {
|
||||
boolean isSelect = myGame.isSelected(x,y);
|
||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
||||
if(isSelect) {
|
||||
g.setColor(Color.blue);
|
||||
}
|
||||
if(isHighlight) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
if((x+y)%2==1 || isSelect || isHighlight) {
|
||||
g.fillRect(
|
||||
Math.round(x*cellWidth),
|
||||
Math.round(y*cellHeight),
|
||||
Math.round(cellWidth),
|
||||
Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
if(isHighlight || isSelect) {
|
||||
g.setColor(Color.white);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<myGame.getWidth();x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<myGame.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
|
||||
for (Piece piece : myGame.getPieces()) {
|
||||
drawPiece(g,piece);
|
||||
}
|
||||
}
|
||||
if (myGame != null) {
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = cellWidth();
|
||||
float cellHeight = cellHeight();
|
||||
|
||||
g.setColor(Color.white);
|
||||
for(int x=0; x<myGame.getWidth(); x++) {
|
||||
for (int y=0; y<myGame.getHeight(); y++) {
|
||||
boolean isSelect = myGame.isSelected(x,y);
|
||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
||||
|
||||
if(isSelect) {
|
||||
g.setColor(Color.blue);
|
||||
}
|
||||
if(isHighlight) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
|
||||
// Fill either dark squares or new color if selected/highlighted
|
||||
if((x + y) % 2 == 1 || isSelect || isHighlight) {
|
||||
g.fillRect(
|
||||
Math.round(x*cellWidth),
|
||||
Math.round(y*cellHeight),
|
||||
Math.round(cellWidth),
|
||||
Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
|
||||
// Restore to white if we changed the color
|
||||
if(isHighlight || isSelect) {
|
||||
g.setColor(Color.white);
|
||||
}
|
||||
|
||||
// Check if King is in check at (x, y)
|
||||
if(myGame.isKingCheckSquare(x, y)) {
|
||||
g.setColor(new Color(255, 0, 0, 120)); // Red overlay
|
||||
g.fillRect(
|
||||
Math.round(x * cellWidth),
|
||||
Math.round(y * cellHeight),
|
||||
Math.round(cellWidth),
|
||||
Math.round(cellHeight)
|
||||
);
|
||||
g.setColor(Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw grid lines
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<myGame.getWidth(); x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<myGame.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
|
||||
// Draw pieces
|
||||
for (Piece piece : myGame.getPieces()) {
|
||||
drawPiece(g, piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawPiece(Graphics g, Piece piece) {
|
||||
g.drawImage(
|
||||
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
||||
MARGIN+(xCoordFromGame(piece.getX())),
|
||||
MARGIN+(yCoordFromGame(piece.getY())),
|
||||
null
|
||||
);
|
||||
g.drawImage(
|
||||
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
||||
MARGIN + (xCoordFromGame(piece.getX())),
|
||||
MARGIN + (yCoordFromGame(piece.getY())),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
|
||||
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
||||
int y = PIECE_HEIGHT * (isWhite?1:0);
|
||||
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
||||
return subImage.getScaledInstance(
|
||||
Math.round(cellWidth())-2*MARGIN,
|
||||
Math.round(cellHeight())-2*MARGIN, 0
|
||||
);
|
||||
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
||||
int y = PIECE_HEIGHT * (isWhite?1:0);
|
||||
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
||||
return subImage.getScaledInstance(
|
||||
Math.round(cellWidth()) - 2*MARGIN,
|
||||
Math.round(cellHeight()) - 2*MARGIN,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private int spriteSheetPositionOfPieceType(PieceType type) {
|
||||
return 5-type.ordinal();
|
||||
return 5 - type.ordinal();
|
||||
}
|
||||
|
||||
|
||||
private float cellWidth() {
|
||||
return (float) this.getWidth()/ (float)myGame.getWidth();
|
||||
return (float)this.getWidth() / (float)myGame.getWidth();
|
||||
}
|
||||
private float cellHeight() {
|
||||
return (float)this.getHeight()/ (float)myGame.getHeight();
|
||||
return (float)this.getHeight() / (float)myGame.getHeight();
|
||||
}
|
||||
private int xCoordFromGame(int x) {
|
||||
return Math.round(x*cellWidth());
|
||||
return Math.round(x*cellWidth());
|
||||
}
|
||||
private int yCoordFromGame(int y) {
|
||||
return Math.round(y*cellHeight());
|
||||
return Math.round(y*cellHeight());
|
||||
}
|
||||
|
||||
public void togglePieceSelector() {
|
||||
pieceSelectorMode = ! pieceSelectorMode;
|
||||
pieceSelectorMode = !pieceSelectorMode;
|
||||
}
|
||||
|
||||
public void toggleAdderMode() {
|
||||
pieceAdderMode = ! pieceAdderMode;
|
||||
pieceAdderMode = !pieceAdderMode;
|
||||
}
|
||||
|
||||
public boolean isPieceSelectorMode() {
|
||||
return pieceSelectorMode;
|
||||
return pieceSelectorMode;
|
||||
}
|
||||
|
||||
|
||||
public boolean isPieceAdderMode() {
|
||||
return pieceAdderMode;
|
||||
return pieceAdderMode;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue