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