Files
3DChess/app/src/main/java/chess/view/consolerender/Console.java
2025-04-18 10:33:10 +02:00

337 lines
12 KiB
Java

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;
import chess.model.Move;
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;
private final ExecutorService executor;
public Console(CommandExecutor commandExecutor, boolean captureInput) {
this.commandExecutor = commandExecutor;
this.executor = Executors.newSingleThreadExecutor();
this.captureInput = captureInput;
}
public Console(CommandExecutor commandExecutor) {
this(commandExecutor, true);
}
private Piece pieceAt(int x, int y) {
return pieceAt(new Coordinate(x, y));
}
private Command.CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
private Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command);
return command.getPiece();
}
public Coordinate stringToCoordinate(String coordinates) throws Exception {
char xPos = coordinates.charAt(0);
char yPos = coordinates.charAt(1);
int x;
if (xPos >= 'A' && xPos <= 'Z') {
x = xPos - 'A';
} else if (xPos >= 'a' && xPos <= 'z') {
x = xPos - 'a';
} else {
throw new Exception("Invalid input");
}
if (!(yPos >= '1' && yPos <= '9')) {
throw new Exception("Invalid input");
}
int y = Coordinate.VALUE_MAX - 1 - (yPos - '1');
return new Coordinate(x, y);
}
@Override
public void onPlayerTurn(Color color, boolean undone) {
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
""");
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) {
sendCommand(new SurrenderCommand(color));
return true;
}
public boolean playerPickedMove(Color color) {
try {
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)));
return switch (Objects.requireNonNull(result)) {
case Command.CommandResult.Moved, Command.CommandResult.ActionNeeded -> true;
default -> false;
};
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
private boolean playerPickedShowMoves(Color color) {
try {
System.out.println("Piece to examine: ");
Coordinate piece = stringToCoordinate(scanner.nextLine());
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(piece);
if (sendCommand(movesCommand) == Command.CommandResult.NotAllowed) {
System.out.println("Not allowed.");
return false;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) {
System.out.println("No moves allowed for this piece.");
return false;
}
displayMoves(piece, allowedMoves);
return false;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
@Override
public void onWin(Color color) {
System.out.println(Colors.RED + "Victory of player " + color + Colors.RESET);
}
@Override
public void onKingInCheck() {
System.out.println(Colors.RED + "Check!" + Colors.RESET);
}
@Override
public void onKingInMat() {
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
}
@Override
public void onPatSituation() {
System.out.println("Pat! It's a draw!");
}
@Override
public void onSurrender(Color color) {
System.out.println("The " + color + " player has surrendered!");
}
@Override
public void onGameStart() {
System.out.println("Game start!");
onBoardUpdate();
}
@Override
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;
do {
try {
String promotion = scanner.next();
newPiece = switch (promotion) {
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;
sendCommand(new PromoteCommand(newPiece));
} catch (Exception e) {
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) {
string.append(Colors.LIGHT_GRAY_BACKGROUND);
} else {
string.append(Colors.DARK_GRAY_BACKGROUND);
}
if (p == null) {
string.append(" " + Colors.RESET);
} 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) {
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++) {
Coordinate currentCell = new Coordinate(j, i);
Piece p = pieceAt(j, i);
if (moves.contains(currentCell)) {
string.append(Colors.YELLOW_BACKGROUND);
} else {
if ((i + j) % 2 == 0) {
string.append(Colors.LIGHT_GRAY_BACKGROUND);
} else {
string.append(Colors.DARK_GRAY_BACKGROUND);
}
}
if (p == null) {
string.append(" " + Colors.RESET);
} else {
if (currentCell.equals(piece)) {
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);
}
}
}
string.append("\n");
}
System.out.println(string);
}
@Override
public void onMove(Move move) {
}
@Override
public void onMoveNotAllowed(Move move) {
System.out.println("Move not allowed.");
}
@Override
public void onDraw() {
System.out.println("Repeated positions!");
}
private boolean onAskedCastling() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd);
return switch (cmd.getCastlingResult()) {
case Small -> onSmallCastling();
case Big -> onBigCastling();
case 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;
};
}
public void setCaptureInput(boolean captureInput) {
this.captureInput = captureInput;
}
}