diff --git a/app/src/main/java/chess/view/consolerender/Console.java b/app/src/main/java/chess/view/consolerender/Console.java index d48a82c..88d6c96 100644 --- a/app/src/main/java/chess/view/consolerender/Console.java +++ b/app/src/main/java/chess/view/consolerender/Console.java @@ -1,11 +1,16 @@ 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.model.Color; import chess.model.Coordinate; +import chess.model.Move; import chess.model.Piece; +import chess.model.pieces.*; import java.util.Scanner; @@ -19,29 +24,73 @@ public class Console implements OutputSystem { } private Piece pieceAt(int x, int y) { - GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, 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; + } + + @Override public void playerTurn(Color color) { + 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(); + 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("Check!"); + System.out.println(Colors.RED + "Check!" + Colors.RESET); } @Override public void kingIsInMat() { - System.out.println("Checkmate!"); + System.out.println(Colors.RED + "Checkmate!" + Colors.RESET); } @Override @@ -63,15 +112,21 @@ public class Console implements OutputSystem { @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."); + 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 { - int promotion = scanner.nextInt(); - if (promotion < 1 || promotion > 4) throw new Exception(); - System.out.println("blablabla"); + 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!"); } @@ -84,7 +139,7 @@ public class Console implements OutputSystem { 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 + " "); + string.append(8 - i).append(" "); for (int j = 0; j < Coordinate.VALUE_MAX; j++) { Piece p = pieceAt(j, i); if ((i+j)%2==0) { @@ -97,7 +152,7 @@ public class Console implements OutputSystem { string.append(" " + Colors.RESET); } else { - string.append(" " + consolePieceName.getString(p) + " " + Colors.RESET); + string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET); } } string.append("\n");