fix undo draw check
This commit is contained in:
@@ -34,6 +34,7 @@ public class MoveCommand extends PlayerCommand {
|
||||
|
||||
case Moved:
|
||||
outputSystem.onMove(this.move);
|
||||
game.saveTraitPiecesPos();
|
||||
return result;
|
||||
|
||||
case ActionNeeded:
|
||||
@@ -83,6 +84,7 @@ public class MoveCommand extends PlayerCommand {
|
||||
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
game.undoTraitPiecesPos();
|
||||
board.undoMove(move, deadPiece);
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class NewGameCommand extends Command {
|
||||
board.pieceComes(new Queen(Color.White), new Coordinate(3, Coordinate.VALUE_MAX - 1));
|
||||
board.pieceComes(new King(Color.White), new Coordinate(4, Coordinate.VALUE_MAX - 1));
|
||||
|
||||
game.resetPlayerTurn();
|
||||
game.reset();
|
||||
|
||||
outputSystem.onGameStart();
|
||||
outputSystem.onPlayerTurn(game.getPlayerTurn());
|
||||
|
||||
@@ -85,6 +85,8 @@ public class PromoteCommand extends PlayerCommand {
|
||||
|
||||
assert promoted != null;
|
||||
|
||||
game.undoTraitPiecesPos();
|
||||
|
||||
board.pieceComes(this.oldPawn, this.pieceCoords);
|
||||
|
||||
game.getLastAction().undo(game, outputSystem);
|
||||
|
||||
@@ -23,8 +23,6 @@ public class Game {
|
||||
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() {
|
||||
@@ -35,6 +33,18 @@ public class Game {
|
||||
return playerTurn;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
resetPlayerTurn();
|
||||
this.traitsPos.clear();
|
||||
initPlayer(Color.Black);
|
||||
initPlayer(Color.White);
|
||||
}
|
||||
|
||||
private void initPlayer(Color color) {
|
||||
this.traitsPos.put(color, new HashMap<>());
|
||||
saveTraitPiecesPos(color);
|
||||
}
|
||||
|
||||
public void resetPlayerTurn() {
|
||||
this.playerTurn = Color.White;
|
||||
}
|
||||
@@ -50,6 +60,10 @@ public class Game {
|
||||
this.traitsPos.get(color).put(piecesHash, count == null ? 1 : count + 1);
|
||||
}
|
||||
|
||||
public void saveTraitPiecesPos() {
|
||||
saveTraitPiecesPos(playerTurn);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param player the trait to check
|
||||
@@ -73,14 +87,13 @@ public class Game {
|
||||
* @return true if a draw should occur
|
||||
*/
|
||||
public void switchPlayerTurn() {
|
||||
saveTraitPiecesPos(this.playerTurn);
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
}
|
||||
|
||||
public GameStatus checkGameStatus() {
|
||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||
|
||||
if(checkDraw())
|
||||
if (checkDraw())
|
||||
return GameStatus.Draw;
|
||||
|
||||
if (this.board.isKingInCheck(enemy))
|
||||
@@ -115,4 +128,15 @@ public class Game {
|
||||
}
|
||||
}
|
||||
|
||||
private void undoTraitPiecesPos(Color color) {
|
||||
int piecesHash = this.board.hashPlayerPieces(color);
|
||||
Integer count = this.traitsPos.get(color).get(piecesHash);
|
||||
if (count != null)
|
||||
this.traitsPos.get(color).put(piecesHash, count - 1);
|
||||
}
|
||||
|
||||
public void undoTraitPiecesPos() {
|
||||
undoTraitPiecesPos(Color.getEnemy(playerTurn));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user