fix console promote

This commit is contained in:
2025-04-15 18:31:09 +02:00
parent 1a038a3de1
commit ae0e76a452
5 changed files with 108 additions and 59 deletions

View File

@@ -7,7 +7,7 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand; import chess.controller.commands.NewGameCommand;
import chess.model.ChessBoard; import chess.model.ChessBoard;
import chess.model.Game; import chess.model.Game;
import chess.simulator.CastlingTest; import chess.simulator.PromoteTest;
import chess.view.consolerender.Console; import chess.view.consolerender.Console;
public class ConsoleMain { public class ConsoleMain {
@@ -15,8 +15,15 @@ public class ConsoleMain {
Game game = new Game(new ChessBoard()); Game game = new Game(new ChessBoard());
CommandExecutor commandExecutor = new CommandExecutor(game); CommandExecutor commandExecutor = new CommandExecutor(game);
PromoteTest promoteTest = new PromoteTest(commandExecutor);
commandExecutor.addListener(promoteTest);
Console console = new Console(commandExecutor); Console console = new Console(commandExecutor);
commandExecutor.addListener(console); commandExecutor.addListener(console);
promoteTest.onComplete.connect(() -> {
console.setCaptureInput(true);
});
commandExecutor.executeCommand(new NewGameCommand()); commandExecutor.executeCommand(new NewGameCommand());
} }

View File

@@ -21,7 +21,7 @@ public class CastlingCommand extends PlayerCommand {
@Override @Override
public CommandResult execute(Game game, GameListener outputSystem) { public CommandResult execute(Game game, GameListener outputSystem) {
final ChessBoard board = game.getBoard(); final ChessBoard board = game.getBoard();
// we must promote the pending pawn before // we must promote the pending pawn before
if (board.pawnShouldBePromoted()) if (board.pawnShouldBePromoted())
return CommandResult.NotAllowed; return CommandResult.NotAllowed;
@@ -39,7 +39,7 @@ public class CastlingCommand extends PlayerCommand {
int kingEndX = bigCastling ? 2 : 6; int kingEndX = bigCastling ? 2 : 6;
int colorLine = game.getPlayerTurn() == Color.White ? 7 : 0; int colorLine = game.getPlayerTurn() == Color.White ? 7 : 0;
Coordinate kingCoords = new Coordinate(kingBeginX, colorLine); Coordinate kingCoords = new Coordinate(kingBeginX, colorLine);
Coordinate rookCoords = new Coordinate(rookBeginX, colorLine); Coordinate rookCoords = new Coordinate(rookBeginX, colorLine);
@@ -52,6 +52,10 @@ public class CastlingCommand extends PlayerCommand {
return CommandResult.Moved; return CommandResult.Moved;
} }
public boolean isBigCastling() {
return bigCastling;
}
@Override @Override
protected CommandResult undoImpl(Game game, GameListener outputSystem) { protected CommandResult undoImpl(Game game, GameListener outputSystem) {
game.getBoard().undoMove(this.kingMove, null); game.getBoard().undoMove(this.kingMove, null);

View File

@@ -94,4 +94,8 @@ public class PromoteCommand extends PlayerCommand {
return CommandResult.Moved; return CommandResult.Moved;
} }
public PromoteType getPromoteType() {
return promoteType;
}
} }

View File

@@ -6,11 +6,15 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.MoveCommand; import chess.controller.commands.MoveCommand;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.Move; import chess.model.Move;
import common.Signal0;
public abstract class Simulator extends GameAdaptator{ public abstract class Simulator extends GameAdaptator {
protected final CommandExecutor commandExecutor; protected final CommandExecutor commandExecutor;
public final Signal0 onComplete = new Signal0();
private int currentMove = 0;
public Simulator(CommandExecutor commandExecutor) { public Simulator(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
} }
@@ -22,6 +26,14 @@ public abstract class Simulator extends GameAdaptator{
} }
} }
@Override
public void onMove(Move move) {
currentMove++;
if (currentMove == getMoves().size()) {
onComplete.emit();
}
}
protected abstract List<Move> getMoves(); protected abstract List<Move> getMoves();
} }

View File

@@ -3,6 +3,7 @@ package chess.view.consolerender;
import chess.controller.Command; import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.*; import chess.controller.commands.*;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameListener; import chess.controller.event.GameListener;
import chess.model.Color; import chess.model.Color;
import chess.model.Coordinate; import chess.model.Coordinate;
@@ -12,14 +13,19 @@ import chess.model.Piece;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Console implements GameListener { public class Console implements GameListener {
private final Scanner scanner = new Scanner(System.in); private final Scanner scanner = new Scanner(System.in);
private final CommandExecutor commandExecutor; private final CommandExecutor commandExecutor;
private final ConsolePieceName consolePieceName = new ConsolePieceName(); private final ConsolePieceName consolePieceName = new ConsolePieceName();
private boolean captureInput = false;
private final ExecutorService executor;
public Console(CommandExecutor commandExecutor) { public Console(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
this.executor = Executors.newSingleThreadExecutor();
} }
private Piece pieceAt(int x, int y) { private Piece pieceAt(int x, int y) {
@@ -56,22 +62,31 @@ public class Console implements GameListener {
@Override @Override
public void onPlayerTurn(Color color) { public void onPlayerTurn(Color color) {
if (!captureInput)
return;
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET); System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
boolean endTurn; this.executor.submit(() -> {
do { boolean endTurn;
System.out.println(""" String line = "0";
Pick your choice: do {
1 - Move if (!line.isEmpty()) {
2 - Show potential moves System.out.println("""
3 - Surrender Pick your choice:
"""); 1 - Move
endTurn = switch (scanner.nextLine()) { 2 - Show potential moves
case "1" -> playerPickedMove(color); 3 - Surrender
case "2" -> playerPickedShowMoves(color); """);
case "3" -> playerPickedSurrender(color); System.out.flush();
default -> false; }
}; line = scanner.nextLine();
} while (!endTurn); endTurn = switch (line) {
case "1" -> playerPickedMove(color);
case "2" -> playerPickedShowMoves(color);
case "3" -> playerPickedSurrender(color);
default -> false;
};
} while (!endTurn);
});
} }
private boolean playerPickedSurrender(Color color) { private boolean playerPickedSurrender(Color color) {
@@ -131,7 +146,7 @@ public class Console implements GameListener {
@Override @Override
public void onKingInCheck() { public void onKingInCheck() {
System.out.println(Colors.RED + "Check!" + Colors.RESET); System.out.println(Colors.RED + "Check!" + Colors.RESET);
} }
@Override @Override
@@ -159,57 +174,61 @@ public class Console implements GameListener {
public void onGameEnd() { public void onGameEnd() {
System.out.println("Thank you for playing!"); System.out.println("Thank you for playing!");
this.commandExecutor.close(); this.commandExecutor.close();
this.executor.shutdown();
} }
@Override @Override
public void onPromotePawn(Coordinate pieceCoords) { public void onPromotePawn(Coordinate pieceCoords) {
System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted."); System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted.");
System.out.println("Enter 'B' to promote it into a Bishop, 'N' for a Knight, 'Q' for a Queen, 'R' for a Rook."); System.out.println("Enter 'B' to promote it into a Bishop, 'N' for a Knight, 'Q' for a Queen, 'R' for a Rook.");
boolean valid = false; System.out.flush();
PromoteCommand.PromoteType newPiece; this.executor.submit(() -> {
do { PromoteType newPiece;
try { boolean valid = false;
String promotion = scanner.next(); do {
newPiece = switch (promotion) { try {
case "B", "b", "Bishop", "bishop" -> PromoteCommand.PromoteType.Bishop; String promotion = scanner.next();
case "N", "n", "Knight", "knight" -> PromoteCommand.PromoteType.Knight; newPiece = switch (promotion) {
case "Q", "q", "Queen", "queen" -> PromoteCommand.PromoteType.Queen; case "B", "b", "Bishop", "bishop" -> PromoteType.Bishop;
case "R", "r", "Rook", "rook" -> PromoteCommand.PromoteType.Rook; case "N", "n", "Knight", "knight" -> PromoteType.Knight;
default -> throw new Exception(); case "Q", "q", "Queen", "queen" -> PromoteType.Queen;
}; case "R", "r", "Rook", "rook" -> PromoteType.Rook;
valid = true; default -> throw new Exception();
sendCommand(new PromoteCommand(newPiece)); };
} catch (Exception e) { valid = true;
System.out.println("Invalid input!"); sendCommand(new PromoteCommand(newPiece));
} } catch (Exception e) {
} while (!valid); System.out.println("Invalid input!");
}
} while (!valid);
});
} }
@Override @Override
public void onBoardUpdate() { public void onBoardUpdate() {
if (!this.captureInput)
return;
StringBuilder string = new StringBuilder(); StringBuilder string = new StringBuilder();
string.append(" a b c d e f g h \n"); string.append(" a b c d e f g h \n");
for (int i = 0; i < Coordinate.VALUE_MAX; i++) { for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
string.append(8 - i).append(" "); string.append(8 - i).append(" ");
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Piece p = pieceAt(j, i); Piece p = pieceAt(j, i);
if ((i+j)%2==0) { if ((i + j) % 2 == 0) {
string.append(Colors.LIGHT_GRAY_BACKGROUND); string.append(Colors.LIGHT_GRAY_BACKGROUND);
} } else {
else {
string.append(Colors.DARK_GRAY_BACKGROUND); string.append(Colors.DARK_GRAY_BACKGROUND);
} }
if (p == null) { if (p == null) {
string.append(" " + Colors.RESET); string.append(" " + Colors.RESET);
} } else {
else {
string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
} }
} }
string.append("\n"); string.append("\n");
} }
System.out.println(string); System.out.println(string);
System.out.flush();
} }
public void displayMoves(Coordinate piece, List<Coordinate> moves) { public void displayMoves(Coordinate piece, List<Coordinate> moves) {
@@ -220,7 +239,7 @@ public class Console implements GameListener {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate currentCell = new Coordinate(j, i); Coordinate currentCell = new Coordinate(j, i);
Piece p = pieceAt(j, i); Piece p = pieceAt(j, i);
if (moves.contains(currentCell)){ if (moves.contains(currentCell)) {
string.append(Colors.YELLOW_BACKGROUND); string.append(Colors.YELLOW_BACKGROUND);
} else { } else {
if ((i + j) % 2 == 0) { if ((i + j) % 2 == 0) {
@@ -231,10 +250,10 @@ public class Console implements GameListener {
} }
if (p == null) { if (p == null) {
string.append(" " + Colors.RESET); string.append(" " + Colors.RESET);
} } else {
else {
if (currentCell.equals(piece)) { if (currentCell.equals(piece)) {
string.append(Colors.RED_BACKGROUND).append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); string.append(Colors.RED_BACKGROUND).append(" ").append(consolePieceName.getString(p))
.append(" ").append(Colors.RESET);
} else { } else {
string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
} }
@@ -246,7 +265,8 @@ public class Console implements GameListener {
} }
@Override @Override
public void onMove(Move move) {} public void onMove(Move move) {
}
@Override @Override
public void onMoveNotAllowed(Move move) { public void onMoveNotAllowed(Move move) {
@@ -262,9 +282,9 @@ public class Console implements GameListener {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand(); GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd); sendCommand(cmd);
return switch (cmd.getCastlingResult()) { return switch (cmd.getCastlingResult()) {
case GetAllowedCastlingsCommand.CastlingResult.Small -> onSmallCastling(); case Small -> onSmallCastling();
case GetAllowedCastlingsCommand.CastlingResult.Big -> onBigCastling(); case Big -> onBigCastling();
case GetAllowedCastlingsCommand.CastlingResult.Both -> onBothCastling(); case Both -> onBothCastling();
default -> { default -> {
System.out.println("No castling allowed."); System.out.println("No castling allowed.");
yield false; yield false;
@@ -277,8 +297,7 @@ public class Console implements GameListener {
String answer = scanner.nextLine(); String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) { if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false; return false;
} } else {
else {
return (commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved); return (commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
} }
} }
@@ -288,8 +307,7 @@ public class Console implements GameListener {
String answer = scanner.nextLine(); String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) { if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false; return false;
} } else {
else {
return (commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved); return (commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
} }
} }
@@ -299,11 +317,15 @@ public class Console implements GameListener {
String answer = scanner.nextLine(); String answer = scanner.nextLine();
return switch (answer) { return switch (answer) {
case "s", "S", "small", "Small", "castling", "normal", "Normal" -> case "s", "S", "small", "Small", "castling", "normal", "Normal" ->
(commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved); (commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
case "b", "B", "big", "Big", "big castling", "Big castling" -> case "b", "B", "big", "Big", "big castling", "Big castling" ->
(commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved); (commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
default -> false; default -> false;
}; };
} }
public void setCaptureInput(boolean captureInput) {
this.captureInput = captureInput;
}
} }