Fixed bugs in move generation, prepared checkmate recognition
This commit is contained in:
parent
df762ce46c
commit
9e031f4000
@ -55,15 +55,15 @@ public class Board {
|
||||
Piece capturePiece = move(move);
|
||||
|
||||
// Revert move if it caused a check for its team
|
||||
if (checkCheck(piece.getColor())) {
|
||||
if (checkCheck(piece.getColor()) != GameEventType.NONE) {
|
||||
revert(move, capturePiece);
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: detecting checkmate
|
||||
// Check for check on the opposite team
|
||||
Color oppositeColor = piece.getColor() == Color.WHITE ? Color.BLACK : Color.WHITE;
|
||||
if (checkCheck(oppositeColor)) notifyListeners(new GameEvent(this, GameEventType.CHECK, oppositeColor));
|
||||
// Detect check on the opposite team
|
||||
Color oppositeColor = piece.getColor().opposite();
|
||||
GameEventType eventType = checkCheck(oppositeColor);
|
||||
if (eventType != GameEventType.NONE) notifyListeners(new GameEvent(this, eventType, oppositeColor));
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -121,13 +121,16 @@ public class Board {
|
||||
return get(pos).getMoves(pos);
|
||||
}
|
||||
|
||||
public boolean checkCheck(Color color) {
|
||||
public GameEventType checkCheck(Color color) {
|
||||
for (int i = 0; i < 8; i++)
|
||||
for (int j = 0; j < 8; j++)
|
||||
if (boardArr[i][j] != null && boardArr[i][j].getColor() != color
|
||||
&& boardArr[i][j].isValidMove(new Move(new Position(i, j), kingPos.get(color))))
|
||||
return true;
|
||||
return false;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
Position pos = new Position(i, j);
|
||||
if (get(pos) != null && get(pos).getColor() != color
|
||||
&& get(pos).isValidMove(new Move(pos, kingPos.get(color))))
|
||||
return GameEventType.CHECK;
|
||||
//return getMoves(kingPos.get(color)).isEmpty() ? GameEventType.CHECKMATE : GameEventType.CHECK;
|
||||
}
|
||||
return GameEventType.NONE;
|
||||
}
|
||||
|
||||
public void registerGameEventListener(GameEventListener listener) {
|
||||
|
@ -123,6 +123,9 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
||||
case CHECK:
|
||||
JOptionPane.showMessageDialog(this, evt.getColor().toString() + " in check!");
|
||||
break;
|
||||
case CHECKMATE:
|
||||
JOptionPane.showMessageDialog(this, evt.getColor().toString() + " in checkmate!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,6 @@ public class GameEvent extends EventObject {
|
||||
public Color getColor() { return color; }
|
||||
|
||||
public static enum GameEventType {
|
||||
CHECK, CHECKMATE
|
||||
CHECK, CHECKMATE, STALEMENT, NONE;
|
||||
}
|
||||
}
|
||||
|
@ -31,10 +31,7 @@ public class King extends Piece {
|
||||
for (int j = Math.max(0, pos.y - 1); j < Math.min(8, pos.y + 2); j++)
|
||||
if (i != pos.x || j != pos.y) {
|
||||
Move move = new Move(pos, new Position(i, j));
|
||||
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) {
|
||||
moves.add(move);
|
||||
if (board.getDest(move) != null) break;
|
||||
}
|
||||
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) moves.add(move);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class Knight extends Piece {
|
||||
|
||||
@Override
|
||||
public boolean isValidMove(Move move) {
|
||||
return Math.abs(move.xDist - move.yDist) == 1 && move.xDist != 0 && move.yDist != 0 && isFreePath(move);
|
||||
return Math.abs(move.xDist - move.yDist) == 1 && (move.xDist == 1 || move.yDist == 1) && isFreePath(move);
|
||||
}
|
||||
|
||||
private void checkAndInsertMove(List<Move> moves, Position pos, int offsetX, int offsetY) {
|
||||
|
@ -34,7 +34,7 @@ public class Pawn extends Piece {
|
||||
protected boolean isFreePath(Move move) {
|
||||
// Two steps forward
|
||||
if (move.yDist == 2)
|
||||
return board.getBoardArr()[move.pos.x][move.dest.y - move.ySign] == null && checkDestination(move);
|
||||
return board.getBoardArr()[move.pos.x][move.dest.y - move.ySign] == null && board.getDest(move) == null;
|
||||
// One step forward
|
||||
else if (move.xDist == 0) return board.getDest(move) == null;
|
||||
// Capture move
|
||||
|
@ -6,6 +6,7 @@ import java.util.List;
|
||||
import dev.kske.chess.Board;
|
||||
import dev.kske.chess.Move;
|
||||
import dev.kske.chess.Position;
|
||||
import dev.kske.chess.event.GameEvent.GameEventType;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
@ -28,7 +29,7 @@ public abstract class Piece {
|
||||
for (Iterator<Move> iterator = moves.iterator(); iterator.hasNext();) {
|
||||
Move move = iterator.next();
|
||||
Piece capturePiece = board.move(move);
|
||||
if (board.checkCheck(getColor()))
|
||||
if (board.checkCheck(getColor()) != GameEventType.NONE)
|
||||
iterator.remove();
|
||||
board.revert(move, capturePiece);
|
||||
}
|
||||
|
Reference in New Issue
Block a user