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

View File

@@ -52,6 +52,10 @@ public class CastlingCommand extends PlayerCommand {
return CommandResult.Moved;
}
public boolean isBigCastling() {
return bigCastling;
}
@Override
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
game.getBoard().undoMove(this.kingMove, null);

View File

@@ -94,4 +94,8 @@ public class PromoteCommand extends PlayerCommand {
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.event.GameAdaptator;
import chess.model.Move;
import common.Signal0;
public abstract class Simulator extends GameAdaptator{
public abstract class Simulator extends GameAdaptator {
protected final CommandExecutor commandExecutor;
public final Signal0 onComplete = new Signal0();
private int currentMove = 0;
public Simulator(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();
}

View File

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