Added check detection

+ Position class and implementation (mostly pieces)
+ King position tracking, check detection and utility functions related
to Position in Board
This commit is contained in:
Kai S. K. Engelbart 2019-07-02 20:07:47 +02:00
parent 616ed129a1
commit ba77a66e48
7 changed files with 90 additions and 30 deletions

View File

@ -1,11 +1,15 @@
package dev.kske.chess;
import java.util.HashMap;
import java.util.Map;
import dev.kske.chess.piece.Bishop;
import dev.kske.chess.piece.King;
import dev.kske.chess.piece.Knight;
import dev.kske.chess.piece.Pawn;
import dev.kske.chess.piece.Piece;
import dev.kske.chess.piece.Piece.Color;
import dev.kske.chess.piece.Piece.Type;
import dev.kske.chess.piece.Queen;
import dev.kske.chess.piece.Rook;
@ -17,10 +21,12 @@ import dev.kske.chess.piece.Rook;
*/
public class Board {
private Piece[][] boardArr;
private Piece[][] boardArr;
private Map<Color, Position> kingPos;
public Board() {
boardArr = new Piece[8][8];
boardArr = new Piece[8][8];
kingPos = new HashMap<>();
initializeDefaultPositions();
}
@ -33,26 +39,56 @@ public class Board {
if (piece == null || !piece.isValidMove(move)) return false;
else {
// Move piece
// Save destination piece for possible canceling of the move
Piece tmpPiece = getDest(move);
setDest(move, getPos(move));
setPos(move, null);
// Revert move if it caused a check for its team
if (checkCheck(piece.getColor())) {
setPos(move, getDest(move));
setDest(move, tmpPiece);
return false;
}
// Update the king's position if the moved piece is a king
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest);
return true;
}
}
public boolean 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;
}
public Piece get(Position pos) {
return boardArr[pos.x][pos.y];
}
public void set(Position pos, Piece piece) {
boardArr[pos.x][pos.y] = piece;
}
public Piece getPos(Move move) {
return boardArr[move.xPos][move.yPos];
return get(move.pos);
}
public Piece getDest(Move move) {
return boardArr[move.xDest][move.yDest];
return get(move.dest);
}
public void setPos(Move move, Piece piece) {
boardArr[move.xPos][move.yPos] = piece;
set(move.pos, piece);
}
public void setDest(Move move, Piece piece) {
boardArr[move.xDest][move.yDest] = piece;
set(move.dest, piece);
}
/**
@ -69,6 +105,10 @@ public class Board {
boardArr[4][0] = new King(Color.BLACK, this);
boardArr[4][7] = new King(Color.WHITE, this);
// Initialize king position objects
kingPos.put(Color.BLACK, new Position(4, 0));
kingPos.put(Color.WHITE, new Position(4, 7));
// Initialize queens
boardArr[3][0] = new Queen(Color.BLACK, this);
boardArr[3][7] = new Queen(Color.WHITE, this);

View File

@ -8,17 +8,20 @@ package dev.kske.chess;
*/
public class Move {
public final int xPos, yPos, xDest, yDest, xDist, yDist, xSign, ySign;
public final Position pos, dest;
public final int xDist, yDist, xSign, ySign;
public Move(Position pos, Position dest) {
this.pos = pos;
this.dest = dest;
xDist = Math.abs(dest.x - pos.x);
yDist = Math.abs(dest.y - pos.y);
xSign = (int) Math.signum(dest.x - pos.x);
ySign = (int) Math.signum(dest.y - pos.y);
}
public Move(int xPos, int yPos, int xDest, int yDest) {
this.xPos = xPos;
this.yPos = yPos;
this.xDest = xDest;
this.yDest = yDest;
xDist = Math.abs(xDest - xPos);
yDist = Math.abs(yDest - yPos);
xSign = (int) Math.signum(xDest - xPos);
ySign = (int) Math.signum(yDest - yPos);
this(new Position(xPos, yPos), new Position(xDest, yDest));
}
public boolean isHorizontal() { return yDist == 0; }

View File

@ -0,0 +1,17 @@
package dev.kske.chess;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Position.java</strong><br>
* Created: <strong>02.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class Position {
public final int x, y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
}

View File

@ -22,8 +22,8 @@ public class Bishop extends Piece {
@Override
protected boolean isFreePath(Move move) {
for (int i = move.xPos + move.xSign, j = move.yPos
+ move.ySign; i != move.xDest; i += move.xSign, j += move.ySign)
for (int i = move.pos.x + move.xSign, j = move.pos.y
+ move.ySign; i != move.dest.x; i += move.xSign, j += move.ySign)
if (board.getBoardArr()[i][j] != null) return false;
return checkDestination(move);
}

View File

@ -21,8 +21,8 @@ public class Pawn extends Piece {
boolean step = move.isVertical() && move.yDist == 1;
boolean doubleStep = move.isVertical() && move.yDist == 2;
boolean strafe = move.isDiagonal() && move.xDist == 1;
if (getColor() == Color.WHITE) doubleStep &= move.yPos == 6;
else doubleStep &= move.yPos == 1;
if (getColor() == Color.WHITE) doubleStep &= move.pos.y == 6;
else doubleStep &= move.pos.y == 1;
return (step ^ doubleStep ^ strafe) && move.ySign == (getColor() == Color.WHITE ? -1 : 1) && isFreePath(move);
}
@ -30,7 +30,7 @@ public class Pawn extends Piece {
protected boolean isFreePath(Move move) {
// Two steps forward
if (move.yDist == 2)
return board.getBoardArr()[move.xPos][move.yDest - move.ySign] == null && checkDestination(move);
return board.getBoardArr()[move.pos.x][move.dest.y - move.ySign] == null && checkDestination(move);
// One step forward
else if (move.xDist == 0) return board.getDest(move) == null;
// Capture move

View File

@ -23,14 +23,14 @@ public class Queen extends Piece {
@Override
protected boolean isFreePath(Move move) {
if (move.isHorizontal()) {
for (int i = move.xPos + move.xSign; i != move.xDest; i += move.xSign)
if (board.getBoardArr()[i][move.yPos] != null) return false;
for (int i = move.pos.x + move.xSign; i != move.dest.x; i += move.xSign)
if (board.getBoardArr()[i][move.pos.y] != null) return false;
} else if (move.isVertical()) {
for (int i = move.yPos + move.ySign; i != move.yDest; i += move.ySign)
if (board.getBoardArr()[move.xPos][i] != null) return false;
for (int i = move.pos.y + move.ySign; i != move.dest.y; i += move.ySign)
if (board.getBoardArr()[move.pos.x][i] != null) return false;
} else {
for (int i = move.xPos + move.xSign, j = move.yPos
+ move.ySign; i != move.xDest; i += move.xSign, j += move.ySign)
for (int i = move.pos.x + move.xSign, j = move.pos.y
+ move.ySign; i != move.dest.x; i += move.xSign, j += move.ySign)
if (board.getBoardArr()[i][j] != null) return false;
}
return checkDestination(move);

View File

@ -23,11 +23,11 @@ public class Rook extends Piece {
@Override
protected boolean isFreePath(Move move) {
if (move.isHorizontal()) {
for (int i = move.xPos + move.xSign; i != move.xDest; i += move.xSign)
if (board.getBoardArr()[i][move.yPos] != null) return false;
for (int i = move.pos.x + move.xSign; i != move.dest.x; i += move.xSign)
if (board.getBoardArr()[i][move.pos.y] != null) return false;
} else {
for (int i = move.yPos + move.ySign; i != move.yDest; i += move.ySign)
if (board.getBoardArr()[move.xPos][i] != null) return false;
for (int i = move.pos.y + move.ySign; i != move.dest.y; i += move.ySign)
if (board.getBoardArr()[move.pos.x][i] != null) return false;
}
return checkDestination(move);
}