Compare commits
2 Commits
8190090adc
...
7b04ec4e6c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b04ec4e6c | ||
|
|
e064902610 |
@@ -2,18 +2,19 @@ package chess.view.consolerender;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.MoveCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.commands.SurrenderCommand;
|
||||
import chess.controller.commands.*;
|
||||
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 static java.lang.System.exit;
|
||||
|
||||
public class Console implements GameListener {
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final CommandExecutor commandExecutor;
|
||||
@@ -27,41 +28,42 @@ public class Console implements GameListener {
|
||||
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);
|
||||
this.commandExecutor.executeCommand(command);
|
||||
sendCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
|
||||
public Coordinate stringToCoordinate(String coordinates) {
|
||||
public Coordinate stringToCoordinate(String coordinates) throws Exception {
|
||||
char xPos = coordinates.charAt(0);
|
||||
char yPos = coordinates.charAt(1);
|
||||
int x = xPos - 'a';
|
||||
int y = 7 - (yPos - '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);
|
||||
}
|
||||
|
||||
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;
|
||||
boolean endTurn;
|
||||
do {
|
||||
System.out.println("""
|
||||
Pick your move:
|
||||
Pick your choice:
|
||||
1 - Move
|
||||
2 - Show potential moves
|
||||
3 - Surrender
|
||||
@@ -74,43 +76,97 @@ public class Console implements GameListener {
|
||||
};
|
||||
} while (!endTurn);
|
||||
System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
|
||||
}
|
||||
|
||||
private boolean playerPickedSurrender(Color color) {
|
||||
sendCommand(new SurrenderCommand(color));
|
||||
hasSurrendered(color);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean playerPickedMove() {
|
||||
try {
|
||||
System.out.println("Piece to move: ");
|
||||
Coordinate start = stringToCoordinate(scanner.nextLine());
|
||||
System.out.println("New position: ");
|
||||
Coordinate end = stringToCoordinate(scanner.nextLine());
|
||||
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
|
||||
if (Objects.requireNonNull(result) == Command.CommandResult.Moved) {
|
||||
updateDisplay();
|
||||
return true;
|
||||
}
|
||||
System.out.println("Move not allowed.");
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean playerPickedShowMoves() {
|
||||
System.out.println("Piece to examine: ");
|
||||
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
||||
// todo
|
||||
return false;
|
||||
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 winnerIs(Color color) {
|
||||
System.out.println(Colors.RED + "Victory of player " + color + Colors.RESET);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInCheck() {
|
||||
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
||||
// todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInMat() {
|
||||
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
||||
gameEnded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void patSituation() {
|
||||
|
||||
// todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hasSurrendered(Color color) {
|
||||
System.out.println("The " + color + " player has surrendered!");
|
||||
gameEnded(Color.getEnemy(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gameStarted() {
|
||||
System.out.println("Game start:");
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
public void gameEnded(){
|
||||
System.out.println("Thank you for playing!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
public void gameEnded(Color winner){
|
||||
winnerIs(winner);
|
||||
gameEnded();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,7 +186,7 @@ public class Console implements GameListener {
|
||||
default -> throw new Exception();
|
||||
};
|
||||
valid = true;
|
||||
this.commandExecutor.executeCommand(new PromoteCommand(newPiece));
|
||||
sendCommand(new PromoteCommand(newPiece));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid input!");
|
||||
}
|
||||
@@ -163,4 +219,41 @@ public class Console implements GameListener {
|
||||
}
|
||||
System.out.println(string);
|
||||
}
|
||||
|
||||
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)){
|
||||
if ((i + j) % 2 == 0) {
|
||||
string.append(Colors.YELLOW_BACKGROUND);
|
||||
} else {
|
||||
string.append(Colors.RED_BACKGROUND);
|
||||
}
|
||||
} else {
|
||||
if ((i + j) % 2 == 0) {
|
||||
string.append(Colors.WHITE_BACKGROUND);
|
||||
} else {
|
||||
string.append(Colors.BLACK_BACKGROUND);
|
||||
}
|
||||
}
|
||||
if (p == null) {
|
||||
string.append(" " + Colors.RESET);
|
||||
}
|
||||
else {
|
||||
if (currentCell == piece) {
|
||||
string.append(Colors.RED).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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user