167 lines
5.4 KiB
Java
167 lines
5.4 KiB
Java
package chess.view.consolerender;
|
|
|
|
import chess.controller.Command;
|
|
import chess.controller.CommandExecutor;
|
|
import chess.controller.OutputSystem;
|
|
import chess.controller.commands.GetPieceAtCommand;
|
|
import chess.controller.commands.MoveCommand;
|
|
import chess.controller.commands.PromoteCommand;
|
|
import chess.controller.commands.SurrenderCommand;
|
|
import chess.model.Color;
|
|
import chess.model.Coordinate;
|
|
import chess.model.Move;
|
|
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 Console(CommandExecutor commandExecutor) {
|
|
this.commandExecutor = commandExecutor;
|
|
}
|
|
|
|
private Piece pieceAt(int x, int y) {
|
|
return pieceAt(new Coordinate(x, y));
|
|
}
|
|
|
|
private Piece pieceAt(Coordinate coordinate) {
|
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
|
this.commandExecutor.executeCommand(command);
|
|
return command.getPiece();
|
|
}
|
|
|
|
public Coordinate stringToCoordinate(String coordinates) {
|
|
char xPos = coordinates.charAt(0);
|
|
char yPos = coordinates.charAt(1);
|
|
int x = xPos - 'a';
|
|
int y = 7 - (yPos - '1');
|
|
return new Coordinate(x, y);
|
|
}
|
|
|
|
public boolean playerPickedMove() {
|
|
System.out.println("Piece to move: ");
|
|
Coordinate start = stringToCoordinate(scanner.nextLine());
|
|
System.out.println("New position: ");
|
|
Coordinate end = stringToCoordinate(scanner.nextLine());
|
|
return this.commandExecutor.executeCommand(new MoveCommand(new Move(start, end))) == Command.CommandResult.Moved;
|
|
}
|
|
|
|
public boolean playerPickedSurrender(Color player) {
|
|
this.commandExecutor.executeCommand(new SurrenderCommand(player));
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void playerTurn(Color color) {
|
|
updateDisplay();
|
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
|
boolean endTurn = false;
|
|
do {
|
|
System.out.println("""
|
|
Pick your move:
|
|
1 - Move
|
|
2 - Show potential moves
|
|
3 - Surrender
|
|
""");
|
|
endTurn = switch (scanner.nextLine()) {
|
|
case "1" -> playerPickedMove();
|
|
case "2" -> playerPickedShowMoves();
|
|
case "3" -> playerPickedSurrender(color);
|
|
default -> false;
|
|
};
|
|
} while (!endTurn);
|
|
System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
|
|
|
|
}
|
|
|
|
private boolean playerPickedShowMoves() {
|
|
System.out.println("Piece to examine: ");
|
|
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
|
// todo
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void winnerIs(Color color) {
|
|
}
|
|
|
|
@Override
|
|
public void kingIsInCheck() {
|
|
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
|
}
|
|
|
|
@Override
|
|
public void kingIsInMat() {
|
|
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
|
}
|
|
|
|
@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:");
|
|
}
|
|
|
|
@Override
|
|
public void promotePawn(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.");
|
|
boolean valid = false;
|
|
PromoteCommand.PromoteType newPiece;
|
|
do {
|
|
try {
|
|
String promotion = scanner.next();
|
|
newPiece = switch (promotion) {
|
|
case ("B") -> PromoteCommand.PromoteType.Bishop;
|
|
case ("N") -> PromoteCommand.PromoteType.Knight;
|
|
case ("Q") -> PromoteCommand.PromoteType.Queen;
|
|
case ("R") -> PromoteCommand.PromoteType.Rook;
|
|
default -> throw new Exception();
|
|
};
|
|
valid = true;
|
|
this.commandExecutor.executeCommand(new PromoteCommand(newPiece));
|
|
} 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).append(" ");
|
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
|
Piece p = pieceAt(j, i);
|
|
if ((i+j)%2==0) {
|
|
string.append(Colors.WHITE_BACKGROUND);
|
|
}
|
|
else {
|
|
string.append(Colors.BLACK_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);
|
|
}
|
|
}
|