This commit is contained in:
Janet-Doe
2025-04-10 11:41:29 +02:00
parent 7aa129bbc6
commit 746fa4d330
3 changed files with 178 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Game;
import chess.view.consolerender.Console;
import chess.view.simplerender.Window;
public class App {
@@ -14,10 +15,10 @@ public class App {
CommandExecutor commandExecutor = new CommandExecutor();
Game game = new Game(new ChessBoard());
Window window = new Window(commandExecutor);
Console console = new Console(commandExecutor);
commandExecutor.setGame(game);
commandExecutor.setOutputSystem(window);
commandExecutor.setOutputSystem(console);
commandExecutor.executeCommand(new NewGameCommand());
}

View File

@@ -0,0 +1,117 @@
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);
}
}

View File

@@ -0,0 +1,58 @@
package chess.view.consolerender;
import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
import chess.model.pieces.*;
public class ConsolePieceName implements PieceVisitor<String> {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public String getString(Piece piece){
if (piece.getColor()== Color.Black){
return ANSI_BLACK + visit(piece);
}
else {
return ANSI_WHITE + visit(piece);
}
}
@Override
public String visitPiece(Bishop bishop) {
return "B";
}
@Override
public String visitPiece(King king) {
return "K";
}
@Override
public String visitPiece(Knight knight) {
return "N";
}
@Override
public String visitPiece(Pawn pawn) {
return "P";
}
@Override
public String visitPiece(Queen queen) {
return "Q";
}
@Override
public String visitPiece(Rook rook) {
return "R";
}
}