add CommanderSender (Fixes #9)
All checks were successful
Linux arm64 / Build (push) Successful in 36s
All checks were successful
Linux arm64 / Build (push) Successful in 36s
This commit is contained in:
@@ -2,9 +2,10 @@ package chess.view.consolerender;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.CommandSender;
|
||||
import chess.controller.commands.*;
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.controller.event.GameListener;
|
||||
import chess.controller.event.GameAdapter;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
@@ -16,7 +17,7 @@ import java.util.Scanner;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Console implements GameListener {
|
||||
public class Console extends GameAdapter implements CommandSender {
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final CommandExecutor commandExecutor;
|
||||
private final ConsolePieceName consolePieceName = new ConsolePieceName();
|
||||
@@ -33,20 +34,6 @@ public class Console implements GameListener {
|
||||
this(commandExecutor, true);
|
||||
}
|
||||
|
||||
private Piece pieceAt(int x, int y) {
|
||||
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);
|
||||
sendCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
|
||||
public Coordinate stringToCoordinate(String coordinates) throws Exception {
|
||||
char xPos = coordinates.charAt(0);
|
||||
char yPos = coordinates.charAt(1);
|
||||
@@ -95,7 +82,7 @@ public class Console implements GameListener {
|
||||
}
|
||||
|
||||
private boolean playerPickedSurrender(Color color) {
|
||||
sendCommand(new SurrenderCommand(color));
|
||||
sendSurrender(color);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -109,7 +96,7 @@ public class Console implements GameListener {
|
||||
Coordinate start = stringToCoordinate(answer);
|
||||
System.out.println("New position: ");
|
||||
Coordinate end = stringToCoordinate(scanner.nextLine());
|
||||
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
|
||||
Command.CommandResult result = sendMove(new Move(start, end));
|
||||
|
||||
return switch (Objects.requireNonNull(result)) {
|
||||
case Command.CommandResult.Moved, Command.CommandResult.ActionNeeded -> true;
|
||||
@@ -126,12 +113,7 @@ public class Console implements GameListener {
|
||||
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();
|
||||
List<Coordinate> allowedMoves = getPieceAllowedMoves(piece);
|
||||
if (allowedMoves.isEmpty()) {
|
||||
System.out.println("No moves allowed for this piece.");
|
||||
return false;
|
||||
@@ -201,7 +183,7 @@ public class Console implements GameListener {
|
||||
default -> throw new Exception();
|
||||
};
|
||||
valid = true;
|
||||
sendCommand(new PromoteCommand(newPiece));
|
||||
sendPawnPromotion(newPiece);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid input!");
|
||||
}
|
||||
@@ -218,7 +200,7 @@ public class Console implements GameListener {
|
||||
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);
|
||||
Piece p = getPieceAt(new Coordinate(j, i));
|
||||
if ((i + j) % 2 == 0) {
|
||||
string.append(Colors.LIGHT_GRAY_BACKGROUND);
|
||||
} else {
|
||||
@@ -243,7 +225,7 @@ public class Console implements GameListener {
|
||||
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);
|
||||
Piece p = getPieceAt(new Coordinate(j, i));
|
||||
if (moves.contains(currentCell)) {
|
||||
string.append(Colors.YELLOW_BACKGROUND);
|
||||
} else {
|
||||
@@ -269,10 +251,6 @@ public class Console implements GameListener {
|
||||
System.out.println(string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMove(Move move, boolean captured) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoveNotAllowed(Move move) {
|
||||
System.out.println("Move not allowed.");
|
||||
@@ -284,9 +262,7 @@ public class Console implements GameListener {
|
||||
}
|
||||
|
||||
private boolean onAskedCastling() {
|
||||
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd);
|
||||
return switch (cmd.getCastlingResult()) {
|
||||
return switch (getAllowedCastlings()) {
|
||||
case Small -> onSmallCastling();
|
||||
case Big -> onBigCastling();
|
||||
case Both -> onBothCastling();
|
||||
@@ -334,10 +310,9 @@ public class Console implements GameListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCastling(boolean bigCastling) {}
|
||||
|
||||
@Override
|
||||
public void onPawnPromoted(PromoteType promotion) {}
|
||||
public CommandExecutor getCommandExecutor() {
|
||||
return this.commandExecutor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user