Fixed bugs in move generation, prepared checkmate recognition

This commit is contained in:
Kai S. K. Engelbart 2019-07-06 12:42:21 +02:00
parent 8158490431
commit 9354a83569
7 changed files with 23 additions and 19 deletions

View File

@ -55,15 +55,15 @@ public class Board {
Piece capturePiece = move(move); Piece capturePiece = move(move);
// Revert move if it caused a check for its team // Revert move if it caused a check for its team
if (checkCheck(piece.getColor())) { if (checkCheck(piece.getColor()) != GameEventType.NONE) {
revert(move, capturePiece); revert(move, capturePiece);
return false; return false;
} }
// TODO: detecting checkmate // Detect check on the opposite team
// Check for check on the opposite team Color oppositeColor = piece.getColor().opposite();
Color oppositeColor = piece.getColor() == Color.WHITE ? Color.BLACK : Color.WHITE; GameEventType eventType = checkCheck(oppositeColor);
if (checkCheck(oppositeColor)) notifyListeners(new GameEvent(this, GameEventType.CHECK, oppositeColor)); if (eventType != GameEventType.NONE) notifyListeners(new GameEvent(this, eventType, oppositeColor));
return true; return true;
} }
@ -121,13 +121,16 @@ public class Board {
return get(pos).getMoves(pos); return get(pos).getMoves(pos);
} }
public boolean checkCheck(Color color) { public GameEventType checkCheck(Color color) {
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) for (int j = 0; j < 8; j++) {
if (boardArr[i][j] != null && boardArr[i][j].getColor() != color Position pos = new Position(i, j);
&& boardArr[i][j].isValidMove(new Move(new Position(i, j), kingPos.get(color)))) if (get(pos) != null && get(pos).getColor() != color
return true; && get(pos).isValidMove(new Move(pos, kingPos.get(color))))
return false; return GameEventType.CHECK;
//return getMoves(kingPos.get(color)).isEmpty() ? GameEventType.CHECKMATE : GameEventType.CHECK;
}
return GameEventType.NONE;
} }
public void registerGameEventListener(GameEventListener listener) { public void registerGameEventListener(GameEventListener listener) {

View File

@ -123,6 +123,9 @@ public class BoardPanel extends JPanel implements GameEventListener {
case CHECK: case CHECK:
JOptionPane.showMessageDialog(this, evt.getColor().toString() + " in check!"); JOptionPane.showMessageDialog(this, evt.getColor().toString() + " in check!");
break; break;
case CHECKMATE:
JOptionPane.showMessageDialog(this, evt.getColor().toString() + " in checkmate!");
break;
} }
} }

View File

@ -33,6 +33,6 @@ public class GameEvent extends EventObject {
public Color getColor() { return color; } public Color getColor() { return color; }
public static enum GameEventType { public static enum GameEventType {
CHECK, CHECKMATE CHECK, CHECKMATE, STALEMENT, NONE;
} }
} }

View File

@ -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++) for (int j = Math.max(0, pos.y - 1); j < Math.min(8, pos.y + 2); j++)
if (i != pos.x || j != pos.y) { if (i != pos.x || j != pos.y) {
Move move = new Move(pos, new Position(i, j)); Move move = new Move(pos, new Position(i, j));
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) { if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) moves.add(move);
moves.add(move);
if (board.getDest(move) != null) break;
}
} }
return moves; return moves;
} }

View File

@ -21,7 +21,7 @@ public class Knight extends Piece {
@Override @Override
public boolean isValidMove(Move move) { 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) { private void checkAndInsertMove(List<Move> moves, Position pos, int offsetX, int offsetY) {

View File

@ -34,7 +34,7 @@ public class Pawn extends Piece {
protected boolean isFreePath(Move move) { protected boolean isFreePath(Move move) {
// Two steps forward // Two steps forward
if (move.yDist == 2) 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 // One step forward
else if (move.xDist == 0) return board.getDest(move) == null; else if (move.xDist == 0) return board.getDest(move) == null;
// Capture move // Capture move

View File

@ -6,6 +6,7 @@ import java.util.List;
import dev.kske.chess.Board; import dev.kske.chess.Board;
import dev.kske.chess.Move; import dev.kske.chess.Move;
import dev.kske.chess.Position; import dev.kske.chess.Position;
import dev.kske.chess.event.GameEvent.GameEventType;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -28,7 +29,7 @@ public abstract class Piece {
for (Iterator<Move> iterator = moves.iterator(); iterator.hasNext();) { for (Iterator<Move> iterator = moves.iterator(); iterator.hasNext();) {
Move move = iterator.next(); Move move = iterator.next();
Piece capturePiece = board.move(move); Piece capturePiece = board.move(move);
if (board.checkCheck(getColor())) if (board.checkCheck(getColor()) != GameEventType.NONE)
iterator.remove(); iterator.remove();
board.revert(move, capturePiece); board.revert(move, capturePiece);
} }