working castling on console + app to play either mode
This commit is contained in:
26
app/src/main/java/chess/App.java
Normal file
26
app/src/main/java/chess/App.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import chess.controller.CommandExecutor;
|
|||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
import chess.simulator.CastlingTest;
|
||||||
import chess.view.consolerender.Console;
|
import chess.view.consolerender.Console;
|
||||||
|
|
||||||
public class ConsoleMain {
|
public class ConsoleMain {
|
||||||
|
|||||||
31
app/src/main/java/chess/simulator/CastlingTest.java
Normal file
31
app/src/main/java/chess/simulator/CastlingTest.java
Normal 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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ public class FoolCheckMate extends Simulator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Move> getMoves() {
|
public List<Move> getMoves() {
|
||||||
List<Move> moves = Arrays.asList(
|
return Arrays.asList(
|
||||||
// white pawn
|
// white pawn
|
||||||
new Move(new Coordinate(5, 6), new Coordinate(5, 5)),
|
new Move(new Coordinate(5, 6), new Coordinate(5, 5)),
|
||||||
// black pawn
|
// black pawn
|
||||||
@@ -24,7 +24,5 @@ public class FoolCheckMate extends Simulator {
|
|||||||
new Move(new Coordinate(6, 6), new Coordinate(6, 4)),
|
new Move(new Coordinate(6, 6), new Coordinate(6, 4)),
|
||||||
// black queen
|
// black queen
|
||||||
new Move(new Coordinate(3, 0), new Coordinate(7, 4)));
|
new Move(new Coordinate(3, 0), new Coordinate(7, 4)));
|
||||||
|
|
||||||
return moves;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class PromoteTest extends Simulator{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Move> getMoves() {
|
protected List<Move> getMoves() {
|
||||||
List<Move> moves = Arrays.asList(
|
return Arrays.asList(
|
||||||
// white pawn
|
// white pawn
|
||||||
new Move(new Coordinate(5, 6), new Coordinate(5, 4)),
|
new Move(new Coordinate(5, 6), new Coordinate(5, 4)),
|
||||||
// black pawn
|
// black pawn
|
||||||
@@ -33,7 +33,6 @@ public class PromoteTest extends Simulator{
|
|||||||
// black king
|
// black king
|
||||||
new Move(new Coordinate(5, 2), new Coordinate(6, 2))
|
new Move(new Coordinate(5, 2), new Coordinate(6, 2))
|
||||||
);
|
);
|
||||||
return moves;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,8 +66,8 @@ public class Console implements GameListener {
|
|||||||
3 - Surrender
|
3 - Surrender
|
||||||
""");
|
""");
|
||||||
endTurn = switch (scanner.nextLine()) {
|
endTurn = switch (scanner.nextLine()) {
|
||||||
case "1" -> playerPickedMove();
|
case "1" -> playerPickedMove(color);
|
||||||
case "2" -> playerPickedShowMoves();
|
case "2" -> playerPickedShowMoves(color);
|
||||||
case "3" -> playerPickedSurrender(color);
|
case "3" -> playerPickedSurrender(color);
|
||||||
default -> false;
|
default -> false;
|
||||||
};
|
};
|
||||||
@@ -79,10 +79,14 @@ public class Console implements GameListener {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean playerPickedMove() {
|
public boolean playerPickedMove(Color color) {
|
||||||
try {
|
try {
|
||||||
System.out.println("Piece to move: ");
|
System.out.println("Piece to move, or \"castling\" for a castling");
|
||||||
Coordinate start = stringToCoordinate(scanner.nextLine());
|
String answer = scanner.nextLine();
|
||||||
|
if (answer.equalsIgnoreCase("castling")) {
|
||||||
|
return onAskedCastling();
|
||||||
|
}
|
||||||
|
Coordinate start = stringToCoordinate(answer);
|
||||||
System.out.println("New position: ");
|
System.out.println("New position: ");
|
||||||
Coordinate end = stringToCoordinate(scanner.nextLine());
|
Coordinate end = stringToCoordinate(scanner.nextLine());
|
||||||
Command.CommandResult result = sendCommand(new MoveCommand(new Move(start, end)));
|
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 {
|
try {
|
||||||
System.out.println("Piece to examine: ");
|
System.out.println("Piece to examine: ");
|
||||||
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
||||||
@@ -253,4 +257,53 @@ public class Console implements GameListener {
|
|||||||
public void onDraw() {
|
public void onDraw() {
|
||||||
System.out.println("Repeated positions!");
|
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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user