Files
3DChess/app/src/main/java/chess/view/consolerender/Console.java
Janet-Doe 746fa4d330 console
2025-04-10 11:41:47 +02:00

118 lines
3.6 KiB
Java

package chess.view.consolerender;
import chess.controller.CommandExecutor;
import chess.controller.OutputSystem;
import chess.controller.commands.GetPieceAtCommand;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Piece;
import java.util.Scanner;
public class Console implements OutputSystem {
private final Scanner scanner = new Scanner(System.in);
private final CommandExecutor commandExecutor;
private final ConsolePieceName consolePieceName = new ConsolePieceName();
public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";
public static final String ANSI_RESET = "\u001B[0m";
public Console(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
}
private Piece pieceAt(int x, int y) {
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
this.commandExecutor.executeCommand(command);
return command.getPiece();
}
@Override
public void playerTurn(Color color) {
}
@Override
public void winnerIs(Color color) {
}
@Override
public void kingIsInCheck() {
System.out.println("Check!");
}
@Override
public void kingIsInMat() {
System.out.println("Checkmate!");
}
@Override
public void patSituation() {
}
@Override
public void hasSurrendered(Color color) {
System.out.println("The " + color + " player has surrendered!");
}
@Override
public void gameStarted() {
System.out.println("Game start:");
updateDisplay();
}
@Override
public void promotePawn(Coordinate pieceCoords) {
System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted.");
System.out.println("Enter 1 to promote it into a Bishop, 2 for a Knight, 3 for a Queen, 4 for a Rook.");
boolean valid = false;
do {
try {
int promotion = scanner.nextInt();
if (promotion < 1 || promotion > 4) throw new Exception();
System.out.println("blablabla");
valid = true;
} catch (Exception e) {
System.out.println("Invalid input!");
}
} while (!valid);
}
@Override
public void updateDisplay() {
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 + " ");
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Piece p = pieceAt(j, i);
if ((i+j)%2==0) {
string.append(ANSI_WHITE_BACKGROUND);
}
else {
string.append(ANSI_BLACK_BACKGROUND);
}
if (p == null) {
string.append(" " + ANSI_RESET);
}
else {
string.append(" " + consolePieceName.getString(p) + " " + ANSI_RESET );
}
}
string.append("\n");
}
System.out.println(string);
}
}