fixed console application
# Conflicts: # app/src/main/java/chess/view/consolerender/Console.java
This commit is contained in:
@@ -2,18 +2,20 @@ package chess.view.consolerender;
|
|||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
import chess.controller.commands.*;
|
||||||
import chess.controller.commands.MoveCommand;
|
|
||||||
import chess.controller.commands.PromoteCommand;
|
|
||||||
import chess.controller.commands.SurrenderCommand;
|
|
||||||
import chess.controller.event.GameListener;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import static java.lang.System.exit;
|
||||||
|
|
||||||
public class Console implements GameListener {
|
public class Console implements GameListener {
|
||||||
private final Scanner scanner = new Scanner(System.in);
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
@@ -27,41 +29,42 @@ public class Console implements GameListener {
|
|||||||
return pieceAt(new Coordinate(x, y));
|
return pieceAt(new Coordinate(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Command.CommandResult sendCommand(Command command) {
|
||||||
|
return this.commandExecutor.executeCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
private Piece pieceAt(Coordinate coordinate) {
|
private Piece pieceAt(Coordinate coordinate) {
|
||||||
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||||
this.commandExecutor.executeCommand(command);
|
sendCommand(command);
|
||||||
return command.getPiece();
|
return command.getPiece();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinate stringToCoordinate(String coordinates) {
|
public Coordinate stringToCoordinate(String coordinates) throws Exception {
|
||||||
char xPos = coordinates.charAt(0);
|
char xPos = coordinates.charAt(0);
|
||||||
char yPos = coordinates.charAt(1);
|
char yPos = coordinates.charAt(1);
|
||||||
int x = xPos - 'a';
|
int x;
|
||||||
int y = 7 - (yPos - '1');
|
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);
|
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
|
@Override
|
||||||
public void playerTurn(Color color) {
|
public void playerTurn(Color color) {
|
||||||
updateDisplay();
|
updateDisplay();
|
||||||
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
boolean endTurn = false;
|
boolean endTurn;
|
||||||
do {
|
do {
|
||||||
System.out.println("""
|
System.out.println("""
|
||||||
Pick your move:
|
Pick your choice:
|
||||||
1 - Move
|
1 - Move
|
||||||
2 - Show potential moves
|
2 - Show potential moves
|
||||||
3 - Surrender
|
3 - Surrender
|
||||||
@@ -74,43 +77,97 @@ public class Console implements GameListener {
|
|||||||
};
|
};
|
||||||
} while (!endTurn);
|
} while (!endTurn);
|
||||||
System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
|
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() {
|
private boolean playerPickedShowMoves() {
|
||||||
System.out.println("Piece to examine: ");
|
try {
|
||||||
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
System.out.println("Piece to examine: ");
|
||||||
// todo
|
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
||||||
return false;
|
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
|
@Override
|
||||||
public void winnerIs(Color color) {
|
public void winnerIs(Color color) {
|
||||||
|
System.out.println(Colors.RED + "Victory of player " + color + Colors.RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInCheck() {
|
public void kingIsInCheck() {
|
||||||
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
||||||
|
// todo
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInMat() {
|
public void kingIsInMat() {
|
||||||
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
||||||
|
gameEnded();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void patSituation() {
|
public void patSituation() {
|
||||||
|
// todo
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void hasSurrendered(Color color) {
|
public void hasSurrendered(Color color) {
|
||||||
System.out.println("The " + color + " player has surrendered!");
|
System.out.println("The " + color + " player has surrendered!");
|
||||||
|
gameEnded(Color.getEnemy(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void gameStarted() {
|
public void gameStarted() {
|
||||||
System.out.println("Game start:");
|
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
|
@Override
|
||||||
@@ -130,7 +187,7 @@ public class Console implements GameListener {
|
|||||||
default -> throw new Exception();
|
default -> throw new Exception();
|
||||||
};
|
};
|
||||||
valid = true;
|
valid = true;
|
||||||
this.commandExecutor.executeCommand(new PromoteCommand(newPiece));
|
sendCommand(new PromoteCommand(newPiece));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Invalid input!");
|
System.out.println("Invalid input!");
|
||||||
}
|
}
|
||||||
@@ -163,4 +220,41 @@ public class Console implements GameListener {
|
|||||||
}
|
}
|
||||||
System.out.println(string);
|
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