working castling on console + app to play either mode

This commit is contained in:
Janet-Doe
2025-04-14 11:19:45 +02:00
parent 91678a42b2
commit 3bea2eeb2d
6 changed files with 119 additions and 11 deletions

View File

@@ -0,0 +1,26 @@
package chess;
import chess.view.consolerender.Colors;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
System.out.println(Colors.RED + "Credits: Grenier Lilas, Pribylski Simon." + Colors.RESET);
System.out.println("""
Pick the version to use:
1 - Console
2 - Window.""");
switch (new Scanner(System.in).nextLine()) {
case "1", "Console", "console":
ConsoleMain.main(args);
break;
case "2", "Window", "window":
SwingMain.main(args);
break;
default:
System.out.println("Invalid input");
break;
}
}
}

View File

@@ -7,6 +7,7 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Game;
import chess.simulator.CastlingTest;
import chess.view.consolerender.Console;
public class ConsoleMain {

View File

@@ -0,0 +1,31 @@
package chess.simulator;
import chess.controller.CommandExecutor;
import chess.model.Coordinate;
import chess.model.Move;
import java.util.Arrays;
import java.util.List;
public class CastlingTest extends Simulator {
public CastlingTest(CommandExecutor commandExecutor) {
super(commandExecutor);
}
@Override
protected List<Move> getMoves() {
return Arrays.asList(
// white pawn
new Move(new Coordinate(6, 6), new Coordinate(6, 4)),
// black knight
new Move(new Coordinate(1, 0), new Coordinate(0, 2)),
// white bishop
new Move(new Coordinate(5, 7), new Coordinate(7, 5)),
// black pawn
new Move(new Coordinate(1, 1), new Coordinate(1, 2)),
// white knight
new Move(new Coordinate(6, 7), new Coordinate(5, 5)),
// black pawn, bis
new Move(new Coordinate(2, 1), new Coordinate(2, 2)));
}
}

View File

@@ -15,7 +15,7 @@ public class FoolCheckMate extends Simulator {
@Override
public List<Move> getMoves() {
List<Move> moves = Arrays.asList(
return Arrays.asList(
// white pawn
new Move(new Coordinate(5, 6), new Coordinate(5, 5)),
// black pawn
@@ -24,7 +24,5 @@ public class FoolCheckMate extends Simulator {
new Move(new Coordinate(6, 6), new Coordinate(6, 4)),
// black queen
new Move(new Coordinate(3, 0), new Coordinate(7, 4)));
return moves;
}
}

View File

@@ -15,7 +15,7 @@ public class PromoteTest extends Simulator{
@Override
protected List<Move> getMoves() {
List<Move> moves = Arrays.asList(
return Arrays.asList(
// white pawn
new Move(new Coordinate(5, 6), new Coordinate(5, 4)),
// black pawn
@@ -33,7 +33,6 @@ public class PromoteTest extends Simulator{
// black king
new Move(new Coordinate(5, 2), new Coordinate(6, 2))
);
return moves;
}
}

View File

@@ -66,8 +66,8 @@ public class Console implements GameListener {
3 - Surrender
""");
endTurn = switch (scanner.nextLine()) {
case "1" -> playerPickedMove();
case "2" -> playerPickedShowMoves();
case "1" -> playerPickedMove(color);
case "2" -> playerPickedShowMoves(color);
case "3" -> playerPickedSurrender(color);
default -> false;
};
@@ -79,10 +79,14 @@ public class Console implements GameListener {
return true;
}
public boolean playerPickedMove() {
public boolean playerPickedMove(Color color) {
try {
System.out.println("Piece to move: ");
Coordinate start = stringToCoordinate(scanner.nextLine());
System.out.println("Piece to move, or \"castling\" for a castling");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("castling")) {
return onAskedCastling();
}
Coordinate start = stringToCoordinate(answer);
System.out.println("New position: ");
Coordinate end = stringToCoordinate(scanner.nextLine());
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
@@ -98,7 +102,7 @@ public class Console implements GameListener {
}
}
private boolean playerPickedShowMoves() {
private boolean playerPickedShowMoves(Color color) {
try {
System.out.println("Piece to examine: ");
Coordinate piece = stringToCoordinate(scanner.nextLine());
@@ -253,4 +257,53 @@ public class Console implements GameListener {
public void onDraw() {
System.out.println("Repeated positions!");
}
private boolean onAskedCastling() {
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
sendCommand(cmd);
return switch (cmd.getCastlingResult()) {
case GetAllowedCastlingsCommand.CastlingResult.Small -> onSmallCastling();
case GetAllowedCastlingsCommand.CastlingResult.Big -> onBigCastling();
case GetAllowedCastlingsCommand.CastlingResult.Both -> onBothCastling();
default -> {
System.out.println("No castling allowed.");
yield false;
}
};
}
private boolean onSmallCastling() {
System.out.println("Small castling allowed. Confirm with \"y\":");
String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false;
}
else {
return (commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
}
}
private boolean onBigCastling() {
System.out.println("Big castling allowed. Confirm with \"y\":");
String answer = scanner.nextLine();
if (!(answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"))) {
return false;
}
else {
return (commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
}
}
private boolean onBothCastling() {
System.out.println("Both castlings allowed. Pick \"s\" to play a castling, \"b\" to play a big castling.");
String answer = scanner.nextLine();
return switch (answer) {
case "s", "S", "small", "Small", "castling", "normal", "Normal" ->
(commandExecutor.executeCommand(new CastlingCommand(false)) != Command.CommandResult.Moved);
case "b", "B", "big", "Big", "big castling", "Big castling" ->
(commandExecutor.executeCommand(new CastlingCommand(true)) != Command.CommandResult.Moved);
default -> false;
};
}
}