Compare commits

...

2 Commits

Author SHA1 Message Date
ae55e0d94c change draw 2025-04-16 11:25:31 +02:00
f54ddc1f59 ai castling 2025-04-16 11:25:05 +02:00
3 changed files with 61 additions and 45 deletions

View File

@@ -5,10 +5,13 @@ import java.util.Random;
import chess.controller.Command; import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedCastlingsCommand;
import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand; import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand; import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.Color; import chess.model.Color;
@@ -34,9 +37,46 @@ public class DumbAI extends GameAdaptator {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand(); GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd); sendCommand(cmd);
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2);
CastlingResult castlings = cmd2.getCastlingResult();
List<Move> moves = cmd.getMoves(); List<Move> moves = cmd.getMoves();
switch (castlings) {
case Both: {
int randomMove = this.random.nextInt(moves.size() + 2);
if (randomMove < moves.size() - 2)
break;
this.commandExecutor.executeCommand(new CastlingCommand(randomMove == moves.size()));
return;
}
case Small: {
int randomMove = this.random.nextInt(moves.size() + 1);
if (randomMove != moves.size())
break;
this.commandExecutor.executeCommand(new CastlingCommand(false));
return;
}
case Big: {
int randomMove = this.random.nextInt(moves.size() + 1);
if (randomMove != moves.size())
break;
this.commandExecutor.executeCommand(new CastlingCommand(true));
return;
}
default:
break;
}
int randomMove = this.random.nextInt(moves.size()); int randomMove = this.random.nextInt(moves.size());
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove))); this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
} }
@Override @Override
@@ -50,10 +90,10 @@ public class DumbAI extends GameAdaptator {
} }
private Piece pieceAt(Coordinate coordinate) { private Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate); GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command); sendCommand(command);
return command.getPiece(); return command.getPiece();
} }
private void sendCommand(Command command) { private void sendCommand(Command command) {
this.commandExecutor.executeCommand(command); this.commandExecutor.executeCommand(command);

View File

@@ -280,14 +280,15 @@ public class ChessBoard {
return null; return null;
} }
public int hashPlayerPieces(Color color) { @Override
public int hashCode() {
int result = 0; int result = 0;
for (int i = 0; i < Coordinate.VALUE_MAX; i++) { for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Piece piece = pieceAt(new Coordinate(i, j)); Piece piece = pieceAt(new Coordinate(i, j));
if (piece == null || piece.getColor() != color) if (piece == null)
continue; continue;
result = Objects.hash(result, new Coordinate(i, j), piece); result = Objects.hash(result, piece.getColor(), new Coordinate(i, j), piece);
} }
} }
return result; return result;
@@ -302,10 +303,10 @@ public class ChessBoard {
} }
@Override @Override
public boolean equals(Object otherBoard){ public boolean equals(Object obj){
if (!(otherBoard instanceof ChessBoard)) return false; if (obj instanceof ChessBoard board)
return ((hashPlayerPieces(Color.White) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.White)) return board.hashCode() == this.hashCode();
&& (hashPlayerPieces(Color.Black) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.Black))); return false;
} }
} }

View File

@@ -12,7 +12,7 @@ public class Game {
private final ChessBoard board; private final ChessBoard board;
private Color playerTurn; private Color playerTurn;
private final Stack<PlayerCommand> movesHistory; private final Stack<PlayerCommand> movesHistory;
private final Map<Color, Map<Integer, Integer>> traitsPos; private final Map<Integer, Integer> traitsPos;
private static final int DRAW_REPETITONS = 3; private static final int DRAW_REPETITONS = 3;
@@ -37,13 +37,6 @@ public class Game {
public void reset() { public void reset() {
resetPlayerTurn(); resetPlayerTurn();
this.traitsPos.clear(); 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() {
@@ -55,32 +48,18 @@ public class Game {
* @param color the current player turn * @param color the current player turn
* @return true if a draw should be declared * @return true if a draw should be declared
*/ */
private void 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);
}
public void saveTraitPiecesPos() { public void saveTraitPiecesPos() {
saveTraitPiecesPos(playerTurn); int piecesHash = this.board.hashCode();
Integer count = this.traitsPos.get(piecesHash);
this.traitsPos.put(piecesHash, count == null ? 1 : count + 1);
} }
/**
*
* @param player the trait to check
* @return true if a position is repeated DRAW_REPETITONS times
*/
private boolean checkDraw(Color player) {
return this.traitsPos.get(player).containsValue(DRAW_REPETITONS);
}
/** /**
* @return whether the game is in a draw situation * @return whether the game is in a draw situation
*/ */
private boolean checkDraw() { private boolean checkDraw() {
if (checkDraw(Color.White)) return this.traitsPos.containsValue(DRAW_REPETITONS);
return true;
return checkDraw(Color.Black);
} }
/** /**
@@ -129,15 +108,11 @@ 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() { public void undoTraitPiecesPos() {
undoTraitPiecesPos(Color.getEnemy(playerTurn)); int piecesHash = this.board.hashCode();
Integer count = this.traitsPos.get(piecesHash);
if (count != null)
this.traitsPos.put(piecesHash, count - 1);
} }
public List<PlayerCommand> getMoves() { public List<PlayerCommand> getMoves() {