From ec3662b5064826ceec6515ea692d0e1eeed3ee20 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 2 Jul 2019 20:07:47 +0200 Subject: [PATCH] Added check detection + Position class and implementation (mostly pieces) + King position tracking, check detection and utility functions related to Position in Board --- src/dev/kske/chess/Board.java | 52 ++++++++++++++++++++++++---- src/dev/kske/chess/Move.java | 21 ++++++----- src/dev/kske/chess/Position.java | 17 +++++++++ src/dev/kske/chess/piece/Bishop.java | 4 +-- src/dev/kske/chess/piece/Pawn.java | 6 ++-- src/dev/kske/chess/piece/Queen.java | 12 +++---- src/dev/kske/chess/piece/Rook.java | 8 ++--- 7 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 src/dev/kske/chess/Position.java diff --git a/src/dev/kske/chess/Board.java b/src/dev/kske/chess/Board.java index df3ff7b..526c6aa 100644 --- a/src/dev/kske/chess/Board.java +++ b/src/dev/kske/chess/Board.java @@ -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 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); diff --git a/src/dev/kske/chess/Move.java b/src/dev/kske/chess/Move.java index 345ee4f..827046f 100644 --- a/src/dev/kske/chess/Move.java +++ b/src/dev/kske/chess/Move.java @@ -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; } diff --git a/src/dev/kske/chess/Position.java b/src/dev/kske/chess/Position.java new file mode 100644 index 0000000..5b737a5 --- /dev/null +++ b/src/dev/kske/chess/Position.java @@ -0,0 +1,17 @@ +package dev.kske.chess; + +/** + * Project: Chess
+ * File: Position.java
+ * Created: 02.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class Position { + + public final int x, y; + + public Position(int x, int y) { + this.x = x; + this.y = y; + } +} diff --git a/src/dev/kske/chess/piece/Bishop.java b/src/dev/kske/chess/piece/Bishop.java index ce4d656..067485b 100644 --- a/src/dev/kske/chess/piece/Bishop.java +++ b/src/dev/kske/chess/piece/Bishop.java @@ -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); } diff --git a/src/dev/kske/chess/piece/Pawn.java b/src/dev/kske/chess/piece/Pawn.java index b35ff59..3a6a8b3 100644 --- a/src/dev/kske/chess/piece/Pawn.java +++ b/src/dev/kske/chess/piece/Pawn.java @@ -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 diff --git a/src/dev/kske/chess/piece/Queen.java b/src/dev/kske/chess/piece/Queen.java index 15cdba5..107bede 100644 --- a/src/dev/kske/chess/piece/Queen.java +++ b/src/dev/kske/chess/piece/Queen.java @@ -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); diff --git a/src/dev/kske/chess/piece/Rook.java b/src/dev/kske/chess/piece/Rook.java index a7c821f..4a12d28 100644 --- a/src/dev/kske/chess/piece/Rook.java +++ b/src/dev/kske/chess/piece/Rook.java @@ -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); }