check draw
This commit is contained in:
@@ -2,6 +2,7 @@ package chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import chess.model.visitor.KingIdentifier;
|
||||
import chess.model.visitor.PawnIdentifier;
|
||||
@@ -137,20 +138,6 @@ public class ChessBoard {
|
||||
}
|
||||
|
||||
public boolean hasAllowedMoves(Color player) {
|
||||
// for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
// for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
// Coordinate attackCoords = new Coordinate(i, j);
|
||||
// Piece attackPiece = pieceAt(attackCoords);
|
||||
// if (attackPiece == null)
|
||||
// continue;
|
||||
|
||||
// if (attackPiece.getColor() != player)
|
||||
// continue;
|
||||
|
||||
// if (!getAllowedMoves(attackCoords).isEmpty())
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
return !getAllowedMoves(player).isEmpty();
|
||||
}
|
||||
|
||||
@@ -289,6 +276,19 @@ public class ChessBoard {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int hashPlayerPieces(Color color) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Piece piece = pieceAt(new Coordinate(i, j));
|
||||
if (piece == null || piece.getColor() != color)
|
||||
continue;
|
||||
result = Objects.hash(result, new Coordinate(i, j), piece);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Move getLastMove() {
|
||||
return this.lastMove;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package chess.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Coordinate {
|
||||
private final int x;
|
||||
private final int y;
|
||||
@@ -42,4 +44,9 @@ public class Coordinate {
|
||||
public String toString() {
|
||||
return "(" + this.x + ", " + this.y + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.x, this.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package chess.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
import chess.controller.PlayerCommand;
|
||||
@@ -9,6 +11,7 @@ public class Game {
|
||||
private final ChessBoard board;
|
||||
private Color playerTurn;
|
||||
private final Stack<PlayerCommand> movesHistory;
|
||||
private final Map<Color, Map<Integer, Integer>> traitsPos;
|
||||
|
||||
public enum GameStatus {
|
||||
Check, CheckMate, OnGoing, Pat;
|
||||
@@ -17,6 +20,9 @@ public class Game {
|
||||
public Game(ChessBoard board) {
|
||||
this.board = board;
|
||||
this.movesHistory = new Stack<>();
|
||||
this.traitsPos = new HashMap<>();
|
||||
this.traitsPos.put(Color.Black, new HashMap<>());
|
||||
this.traitsPos.put(Color.White, new HashMap<>());
|
||||
}
|
||||
|
||||
public ChessBoard getBoard() {
|
||||
@@ -31,8 +37,26 @@ public class Game {
|
||||
this.playerTurn = Color.White;
|
||||
}
|
||||
|
||||
public void switchPlayerTurn() {
|
||||
/**
|
||||
*
|
||||
* @param color the current player turn
|
||||
* @return true if a draw should be declared
|
||||
*/
|
||||
private boolean saveTraitPiecesPos(Color color) {
|
||||
int piecesHash = this.board.hashPlayerPieces(color);
|
||||
Integer count = this.traitsPos.get(color).get(piecesHash);
|
||||
this.traitsPos.get(color).put(piecesHash, count == null ? 1 : count + 1);
|
||||
return count == null ? false : count == (3 - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if a draw should occur
|
||||
*/
|
||||
public boolean switchPlayerTurn() {
|
||||
boolean draw = saveTraitPiecesPos(this.playerTurn);
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
return draw;
|
||||
}
|
||||
|
||||
public GameStatus checkGameStatus() {
|
||||
|
||||
@@ -15,4 +15,9 @@ public class Bishop extends Piece {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,4 +14,9 @@ public class King extends Piece {
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,9 @@ public class Knight extends Piece {
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,9 @@ public class Pawn extends Piece {
|
||||
public int multiplier() {
|
||||
return getColor() == Color.White ? 1 : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,9 @@ public class Queen extends Piece {
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,9 @@ public class Rook extends Piece {
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user