working castling on console + app to play either mode

This commit is contained in:
Janet-Doe
2025-04-14 11:19:45 +02:00
parent 91678a42b2
commit 3bea2eeb2d
6 changed files with 119 additions and 11 deletions

View File

@@ -66,8 +66,8 @@ public class Console implements GameListener {
3 - Surrender
""");
endTurn = switch (scanner.nextLine()) {
case "1" -> playerPickedMove();
case "2" -> playerPickedShowMoves();
case "1" -> playerPickedMove(color);
case "2" -> playerPickedShowMoves(color);
case "3" -> playerPickedSurrender(color);
default -> false;
};
@@ -79,10 +79,14 @@ public class Console implements GameListener {
return true;
}
public boolean playerPickedMove() {
public boolean playerPickedMove(Color color) {
try {
System.out.println("Piece to move: ");
Coordinate start = stringToCoordinate(scanner.nextLine());
System.out.println("Piece to move, or \"castling\" for a castling");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("castling")) {
return onAskedCastling();
}
Coordinate start = stringToCoordinate(answer);
System.out.println("New position: ");
Coordinate end = stringToCoordinate(scanner.nextLine());
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
@@ -98,7 +102,7 @@ public class Console implements GameListener {
}
}
private boolean playerPickedShowMoves() {
private boolean playerPickedShowMoves(Color color) {
try {
System.out.println("Piece to examine: ");
Coordinate piece = stringToCoordinate(scanner.nextLine());
@@ -253,4 +257,53 @@ public class Console implements GameListener {
public void onDraw() {
System.out.println("Repeated positions!");
}
private boolean onAskedCastling() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd);
return switch (cmd.getCastlingResult()) {
case GetAllowedCastlingsCommand.CastlingResult.Small -> onSmallCastling();
case GetAllowedCastlingsCommand.CastlingResult.Big -> onBigCastling();
case GetAllowedCastlingsCommand.CastlingResult.Both -> onBothCastling();
default -> {
System.out.println("No castling allowed.");
yield false;
}
};
}
private boolean onSmallCastling() {
System.out.println("Small castling allowed. Confirm with \"y\":");
String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false;
}
else {
return (commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
}
}
private boolean onBigCastling() {
System.out.println("Big castling allowed. Confirm with \"y\":");
String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false;
}
else {
return (commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
}
}
private boolean onBothCastling() {
System.out.println("Both castlings allowed. Pick \"s\" to play a castling, \"b\" to play a big castling.");
String answer = scanner.nextLine();
return switch (answer) {
case "s", "S", "small", "Small", "castling", "normal", "Normal" ->
(commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
case "b", "B", "big", "Big", "big castling", "Big castling" ->
(commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
default -> false;
};
}
}