Compare commits
54 Commits
9af06e36f8
...
failed
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ac7b4ad65 | |||
| 78d48fafc6 | |||
| ae55e0d94c | |||
| f54ddc1f59 | |||
|
|
ebb013cae5 | ||
|
|
076288a400 | ||
| 3f06a9eb17 | |||
| 2502d2c5d3 | |||
| 577ef0545f | |||
| edf95a1691 | |||
| 87992b181e | |||
| ae0e76a452 | |||
| 1a038a3de1 | |||
| d8c927083a | |||
|
|
3bea2eeb2d | ||
| 91678a42b2 | |||
| 07016be5d8 | |||
|
|
2578e8cf6f | ||
| 2271f610e7 | |||
| 8b8eb428e5 | |||
| b336784a5d | |||
| 224a09c711 | |||
| a23c334994 | |||
|
|
9f44548843 | ||
| 39c289cc47 | |||
| de733fcea2 | |||
| ec2c1abe78 | |||
| 7b7280d807 | |||
| d2485a4d75 | |||
|
|
7b04ec4e6c | ||
|
|
e064902610 | ||
| acd20ef7fa | |||
| 5cf00309b3 | |||
| 8190090adc | |||
| 0d3d77781f | |||
|
|
98b28c5fba | ||
| 17d8333342 | |||
| 3226e72d32 | |||
|
|
0fbea2ca0f | ||
|
|
746fa4d330 | ||
| 7aa129bbc6 | |||
|
|
19b45371bb | ||
| 4b84a30e07 | |||
| e8e79a1e9e | |||
| 6584c5cb91 | |||
| 58cc9f3f17 | |||
| 6eae7e386f | |||
| 6cb1dd826f | |||
| a2224cf618 | |||
| 65362677a5 | |||
| 381e5ed0b8 | |||
| d94f7d733b | |||
| 2ec7be27ca | |||
| 8c2c6946d7 |
@@ -36,6 +36,13 @@ dependencies {
|
|||||||
application {
|
application {
|
||||||
// Define the main class for the application.
|
// Define the main class for the application.
|
||||||
mainClass = "chess.App"
|
mainClass = "chess.App"
|
||||||
|
applicationName = "3DChess"
|
||||||
|
}
|
||||||
|
|
||||||
|
jar {
|
||||||
|
manifest {
|
||||||
|
attributes 'Main-Class': application.mainClass
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
|||||||
@@ -1,24 +1,26 @@
|
|||||||
/*
|
|
||||||
* This Java source file was generated by the Gradle 'init' task.
|
|
||||||
*/
|
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.view.consolerender.Colors;
|
||||||
import chess.controller.commands.NewGameCommand;
|
|
||||||
import chess.model.ChessBoard;
|
import java.util.Scanner;
|
||||||
import chess.model.Game;
|
|
||||||
import chess.view.simplerender.Window;
|
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CommandExecutor commandExecutor = new CommandExecutor();
|
System.out.println(Colors.RED + "Credits: Grenier Lilas, Pribylski Simon." + Colors.RESET);
|
||||||
|
System.out.println("""
|
||||||
Game game = new Game(new ChessBoard());
|
Pick the version to use:
|
||||||
Window window = new Window(commandExecutor);
|
1 - Console
|
||||||
|
2 - Window.""");
|
||||||
commandExecutor.setGame(game);
|
switch (new Scanner(System.in).nextLine()) {
|
||||||
commandExecutor.setOutputSystem(window);
|
case "1", "Console", "console":
|
||||||
|
ConsoleMain.main(args);
|
||||||
commandExecutor.executeCommand(new NewGameCommand());
|
break;
|
||||||
|
case "2", "Window", "window":
|
||||||
|
SwingMain.main(args);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid input");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
app/src/main/java/chess/ConsoleMain.java
Normal file
30
app/src/main/java/chess/ConsoleMain.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This Java source file was generated by the Gradle 'init' task.
|
||||||
|
*/
|
||||||
|
package chess;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.simulator.PromoteTest;
|
||||||
|
import chess.view.consolerender.Console;
|
||||||
|
|
||||||
|
public class ConsoleMain {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
PromoteTest promoteTest = new PromoteTest(commandExecutor);
|
||||||
|
commandExecutor.addListener(promoteTest);
|
||||||
|
|
||||||
|
Console console = new Console(commandExecutor);
|
||||||
|
commandExecutor.addListener(console);
|
||||||
|
|
||||||
|
promoteTest.onComplete.connect(() -> {
|
||||||
|
console.setCaptureInput(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/src/main/java/chess/OpenGLMain.java
Normal file
33
app/src/main/java/chess/OpenGLMain.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package chess;
|
||||||
|
|
||||||
|
import chess.ai.DumbAI;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.pgn.PgnExport;
|
||||||
|
import chess.view.render.Window;
|
||||||
|
|
||||||
|
public class OpenGLMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
Window window = new Window(commandExecutor);
|
||||||
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
|
// DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
// commandExecutor.addListener(ai);
|
||||||
|
|
||||||
|
// DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
// commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
window.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
36
app/src/main/java/chess/SwingMain.java
Normal file
36
app/src/main/java/chess/SwingMain.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package chess;
|
||||||
|
|
||||||
|
import chess.ai.DumbAI;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.pgn.PgnExport;
|
||||||
|
import chess.view.simplerender.Window;
|
||||||
|
|
||||||
|
public class SwingMain {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
Window window = new Window(commandExecutor, false);
|
||||||
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
|
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
|
commandExecutor.addListener(new GameAdaptator(){
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
System.out.println(PgnExport.exportGame(game));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
}
|
||||||
|
}
|
||||||
102
app/src/main/java/chess/ai/DumbAI.java
Normal file
102
app/src/main/java/chess/ai/DumbAI.java
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package chess.ai;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.CastlingCommand;
|
||||||
|
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||||
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GetPlayerMovesCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
public class DumbAI extends GameAdaptator {
|
||||||
|
|
||||||
|
private final Color player;
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
||||||
|
this.player = color;
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
if (color != player)
|
||||||
|
return;
|
||||||
|
|
||||||
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||||
|
sendCommand(cmd);
|
||||||
|
|
||||||
|
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||||
|
sendCommand(cmd2);
|
||||||
|
|
||||||
|
CastlingResult castlings = cmd2.getCastlingResult();
|
||||||
|
List<Move> moves = cmd.getMoves();
|
||||||
|
|
||||||
|
switch (castlings) {
|
||||||
|
case Both: {
|
||||||
|
int randomMove = this.random.nextInt(moves.size() + 2);
|
||||||
|
if (randomMove < moves.size() - 2)
|
||||||
|
break;
|
||||||
|
this.commandExecutor.executeCommand(new CastlingCommand(randomMove == moves.size()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Small: {
|
||||||
|
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||||
|
if (randomMove != moves.size())
|
||||||
|
break;
|
||||||
|
this.commandExecutor.executeCommand(new CastlingCommand(false));
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
case Big: {
|
||||||
|
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||||
|
if (randomMove != moves.size())
|
||||||
|
break;
|
||||||
|
this.commandExecutor.executeCommand(new CastlingCommand(true));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int randomMove = this.random.nextInt(moves.size());
|
||||||
|
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
Piece pawn = pieceAt(pieceCoords);
|
||||||
|
if (pawn.getColor() != this.player)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int promote = this.random.nextInt(PromoteType.values().length);
|
||||||
|
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(Coordinate coordinate) {
|
||||||
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||||
|
sendCommand(command);
|
||||||
|
return command.getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCommand(Command command) {
|
||||||
|
this.commandExecutor.executeCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
package chess.controller;
|
package chess.controller;
|
||||||
|
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
|
||||||
public enum CommandResult {
|
public enum CommandResult {
|
||||||
/** The command was successfull. Should update display and switch player turn. */
|
/**
|
||||||
|
* The command was successfull. Should update display and switch player turn.
|
||||||
|
*/
|
||||||
Moved,
|
Moved,
|
||||||
/** The command was successfull. Should not update anything */
|
/** The command was successfull. Should not update anything */
|
||||||
NotMoved,
|
NotMoved,
|
||||||
@@ -15,7 +18,6 @@ public abstract class Command {
|
|||||||
NotAllowed;
|
NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract CommandResult execute(Game game, OutputSystem outputSystem);
|
public abstract CommandResult execute(Game game, GameListener outputSystem);
|
||||||
|
|
||||||
public void postExec(Game game, OutputSystem outputSystem) {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,43 @@
|
|||||||
package chess.controller;
|
package chess.controller;
|
||||||
|
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.Command.CommandResult;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.event.GameDispatcher;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Game.GameStatus;
|
import chess.model.Game.GameStatus;
|
||||||
|
|
||||||
public class CommandExecutor {
|
public class CommandExecutor {
|
||||||
|
|
||||||
private Game game;
|
private Game game;
|
||||||
private OutputSystem outputSystem;
|
private final GameDispatcher dispatcher;
|
||||||
|
|
||||||
public CommandExecutor() {
|
public CommandExecutor() {
|
||||||
this.game = null;
|
this(null);
|
||||||
this.outputSystem = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandResult executeCommand(Command command) {
|
public CommandExecutor(Game game) {
|
||||||
assert this.game != null : "No input game specified !";
|
this(game, new GameDispatcher());
|
||||||
assert this.outputSystem != null : "No output system specified !";
|
}
|
||||||
|
|
||||||
CommandResult result = command.execute(this.game, this.outputSystem);
|
public CommandExecutor(Game game, GameDispatcher dispatcher) {
|
||||||
|
this.game = game;
|
||||||
|
this.dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized CommandResult executeCommand(Command command) {
|
||||||
|
assert this.game != null : "No input game specified !";
|
||||||
|
|
||||||
|
CommandResult result = command.execute(this.game, this.dispatcher);
|
||||||
|
|
||||||
// non player commands are not supposed to return move result
|
// non player commands are not supposed to return move result
|
||||||
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
assert result != CommandResult.Moved || command instanceof PlayerCommand || command instanceof UndoCommand;
|
||||||
|
|
||||||
processResult(command, result);
|
processResult(command, result);
|
||||||
command.postExec(game, outputSystem);
|
|
||||||
|
if (command instanceof PlayerCommand playerCommand && result != CommandResult.NotAllowed)
|
||||||
|
this.game.addAction(playerCommand);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,21 +48,23 @@ public class CommandExecutor {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
case ActionNeeded:
|
case ActionNeeded:
|
||||||
this.outputSystem.updateDisplay();
|
this.dispatcher.onBoardUpdate();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case Moved:
|
case Moved:
|
||||||
if (checkGameStatus())
|
this.dispatcher.onBoardUpdate();
|
||||||
|
if (checkGameStatus()) {
|
||||||
|
this.dispatcher.onGameEnd();
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
switchPlayerTurn();
|
switchPlayerTurn();
|
||||||
this.outputSystem.updateDisplay();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void switchPlayerTurn() {
|
private void switchPlayerTurn() {
|
||||||
this.game.switchPlayerTurn();
|
this.game.switchPlayerTurn();
|
||||||
this.outputSystem.playerTurn(this.game.getPlayerTurn());
|
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,32 +75,38 @@ public class CommandExecutor {
|
|||||||
GameStatus gameStatus = this.game.checkGameStatus();
|
GameStatus gameStatus = this.game.checkGameStatus();
|
||||||
|
|
||||||
switch (gameStatus) {
|
switch (gameStatus) {
|
||||||
|
case Draw:
|
||||||
|
this.dispatcher.onDraw();
|
||||||
|
return true;
|
||||||
case Check:
|
case Check:
|
||||||
this.outputSystem.kingIsInCheck();
|
this.dispatcher.onKingInCheck();
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case CheckMate:
|
case CheckMate:
|
||||||
this.outputSystem.kingIsInMat();
|
this.dispatcher.onKingInMat();
|
||||||
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
this.dispatcher.onWin(this.game.getPlayerTurn());
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case OnGoing:
|
case OnGoing:
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case Pat:
|
case Pat:
|
||||||
this.outputSystem.patSituation();
|
this.dispatcher.onPatSituation();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOutputSystem(OutputSystem outputSystem) {
|
public void addListener(GameListener listener) {
|
||||||
this.outputSystem = outputSystem;
|
this.dispatcher.addListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGame(Game game) {
|
public void setGame(Game game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
this.dispatcher.stopService();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package chess.controller;
|
|
||||||
|
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Coordinate;
|
|
||||||
|
|
||||||
public interface OutputSystem {
|
|
||||||
|
|
||||||
void playerTurn(Color color);
|
|
||||||
|
|
||||||
void winnerIs(Color color);
|
|
||||||
|
|
||||||
void kingIsInCheck();
|
|
||||||
|
|
||||||
void kingIsInMat();
|
|
||||||
|
|
||||||
void patSituation();
|
|
||||||
|
|
||||||
void hasSurrendered(Color color);
|
|
||||||
|
|
||||||
void gameStarted();
|
|
||||||
|
|
||||||
void promotePawn(Coordinate pieceCoords);
|
|
||||||
|
|
||||||
void updateDisplay();
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,15 @@
|
|||||||
package chess.controller;
|
package chess.controller;
|
||||||
|
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public abstract class PlayerCommand extends Command{
|
public abstract class PlayerCommand extends Command{
|
||||||
public abstract void undo(Game game, OutputSystem outputSystem);
|
|
||||||
|
public CommandResult undo(Game game, GameListener outputSystem) {
|
||||||
|
CommandResult result = undoImpl(game, outputSystem);
|
||||||
|
game.updateLastMove();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract CommandResult undoImpl(Game game, GameListener outputSystem);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,67 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.OutputSystem;
|
|
||||||
import chess.controller.PlayerCommand;
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
public class CastlingCommand extends PlayerCommand {
|
public class CastlingCommand extends PlayerCommand {
|
||||||
|
|
||||||
|
private Move kingMove;
|
||||||
|
private Move rookMove;
|
||||||
|
private final boolean bigCastling;
|
||||||
|
|
||||||
|
public CastlingCommand(boolean bigCastling) {
|
||||||
|
this.bigCastling = bigCastling;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
// we must promote the pending pawn before
|
// we must promote the pending pawn before
|
||||||
if (game.pawnShouldBePromoted())
|
if (board.pawnShouldBePromoted())
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
if (bigCastling && !board.canBigCastle(game.getPlayerTurn()))
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
if (!bigCastling && !board.canSmallCastle(game.getPlayerTurn()))
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
int rookBeginX = bigCastling ? 0 : 7;
|
||||||
|
int rookEndX = bigCastling ? 3 : 5;
|
||||||
|
|
||||||
|
int kingBeginX = 4;
|
||||||
|
int kingEndX = bigCastling ? 2 : 6;
|
||||||
|
|
||||||
|
int colorLine = game.getPlayerTurn() == Color.White ? 7 : 0;
|
||||||
|
|
||||||
|
Coordinate kingCoords = new Coordinate(kingBeginX, colorLine);
|
||||||
|
Coordinate rookCoords = new Coordinate(rookBeginX, colorLine);
|
||||||
|
|
||||||
|
this.kingMove = new Move(kingCoords, new Coordinate(kingEndX, colorLine));
|
||||||
|
this.rookMove = new Move(rookCoords, new Coordinate(rookEndX, colorLine));
|
||||||
|
|
||||||
|
board.applyMove(this.kingMove);
|
||||||
|
board.applyMove(this.rookMove);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBigCastling() {
|
||||||
|
return bigCastling;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
// TODO Auto-generated method stub
|
game.getBoard().undoMove(this.kingMove, null);
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
game.getBoard().undoMove(this.rookMove, null);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Game;
|
||||||
|
|
||||||
|
public class GetAllowedCastlingsCommand extends Command{
|
||||||
|
|
||||||
|
public enum CastlingResult {
|
||||||
|
None, Small, Big, Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CastlingResult castlingResult;
|
||||||
|
|
||||||
|
public GetAllowedCastlingsCommand() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
boolean canSmallCastle = game.getBoard().canSmallCastle(game.getPlayerTurn());
|
||||||
|
boolean canBigCastle = game.getBoard().canBigCastle(game.getPlayerTurn());
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
if (canSmallCastle)
|
||||||
|
result += 1;
|
||||||
|
if (canBigCastle)
|
||||||
|
result += 2;
|
||||||
|
|
||||||
|
this.castlingResult = CastlingResult.values()[result];
|
||||||
|
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CastlingResult getCastlingResult() {
|
||||||
|
return castlingResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,24 +4,24 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class GetAllowedMovesCommand extends Command {
|
public class GetAllowedMovesPieceCommand extends Command {
|
||||||
|
|
||||||
private final Coordinate start;
|
private final Coordinate start;
|
||||||
private List<Coordinate> destinations;
|
private List<Coordinate> destinations;
|
||||||
|
|
||||||
public GetAllowedMovesCommand(Coordinate start) {
|
public GetAllowedMovesPieceCommand(Coordinate start) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.destinations = new ArrayList<>();
|
this.destinations = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
Piece piece = board.pieceAt(start);
|
Piece piece = board.pieceAt(start);
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
@@ -17,7 +17,7 @@ public class GetPieceAtCommand extends Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
if (!pieceCoords.isValid())
|
if (!pieceCoords.isValid())
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class GetPlayerMovesCommand extends Command {
|
||||||
|
|
||||||
|
private List<Move> moves;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
this.moves = game.getBoard().getAllowedMoves(game.getPlayerTurn());
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Move> getMoves() {
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package chess.controller.commands;
|
|
||||||
|
|
||||||
import chess.controller.OutputSystem;
|
|
||||||
import chess.controller.PlayerCommand;
|
|
||||||
import chess.model.Game;
|
|
||||||
|
|
||||||
public class GrandCastlingCommand extends PlayerCommand {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
|
||||||
// we must promote the pending pawn before
|
|
||||||
if (game.pawnShouldBePromoted())
|
|
||||||
return CommandResult.NotAllowed;
|
|
||||||
|
|
||||||
return CommandResult.NotAllowed;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.OutputSystem;
|
|
||||||
import chess.controller.PlayerCommand;
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
@@ -23,12 +23,37 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
return move;
|
return move;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Piece getDeadPiece() {
|
||||||
|
return deadPiece;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
CommandResult result = processMove(game, outputSystem);
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
case NotAllowed:
|
||||||
|
outputSystem.onMoveNotAllowed(this.move);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
case Moved:
|
||||||
|
outputSystem.onMove(this.move);
|
||||||
|
game.saveTraitPiecesPos();
|
||||||
|
return result;
|
||||||
|
|
||||||
|
case ActionNeeded:
|
||||||
|
case NotMoved:
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommandResult processMove(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
// we must promote the pending pawn before
|
// we must promote the pending pawn before
|
||||||
if (game.pawnShouldBePromoted())
|
if (board.pawnShouldBePromoted())
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
Piece piece = board.pieceAt(move.getStart());
|
Piece piece = board.pieceAt(move.getStart());
|
||||||
@@ -42,7 +67,7 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
if (!valid)
|
if (!valid)
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
this.deadPiece = board.pieceAt(move.getFinish());
|
this.deadPiece = board.pieceAt(move.getDeadPieceCoords());
|
||||||
board.applyMove(move);
|
board.applyMove(move);
|
||||||
|
|
||||||
if (board.isKingInCheck(game.getPlayerTurn())) {
|
if (board.isKingInCheck(game.getPlayerTurn())) {
|
||||||
@@ -50,31 +75,31 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game.pawnShouldBePromoted())
|
if (tryPromote(game, outputSystem)) {
|
||||||
return CommandResult.ActionNeeded;
|
return CommandResult.ActionNeeded;
|
||||||
|
}
|
||||||
|
|
||||||
|
board.setLastMove(this.move);
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
game.undoTraitPiecesPos();
|
||||||
board.undoMove(move, deadPiece);
|
board.undoMove(move, deadPiece);
|
||||||
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean tryPromote(Game game, GameListener outputSystem) {
|
||||||
public void postExec(Game game, OutputSystem outputSystem) {
|
Coordinate pawnPos = game.getBoard().pawnPromotePosition();
|
||||||
tryPromote(game, outputSystem);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void tryPromote(Game game, OutputSystem outputSystem) {
|
|
||||||
Coordinate pawnPos = game.pawnPromotePosition();
|
|
||||||
|
|
||||||
if (pawnPos == null)
|
if (pawnPos == null)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
outputSystem.promotePawn(pawnPos);
|
outputSystem.onPromotePawn(pawnPos);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
@@ -14,7 +14,7 @@ import chess.model.pieces.Queen;
|
|||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
|
||||||
public class NewGameCommand extends Command {
|
public class NewGameCommand extends Command {
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
board.clearBoard();
|
board.clearBoard();
|
||||||
@@ -48,10 +48,10 @@ public class NewGameCommand extends Command {
|
|||||||
board.pieceComes(new Queen(Color.White), new Coordinate(3, Coordinate.VALUE_MAX - 1));
|
board.pieceComes(new Queen(Color.White), new Coordinate(3, Coordinate.VALUE_MAX - 1));
|
||||||
board.pieceComes(new King(Color.White), new Coordinate(4, Coordinate.VALUE_MAX - 1));
|
board.pieceComes(new King(Color.White), new Coordinate(4, Coordinate.VALUE_MAX - 1));
|
||||||
|
|
||||||
game.resetPlayerTurn();
|
game.reset();
|
||||||
|
|
||||||
outputSystem.gameStarted();
|
outputSystem.onGameStart();
|
||||||
outputSystem.playerTurn(game.getPlayerTurn());
|
outputSystem.onPlayerTurn(game.getPlayerTurn());
|
||||||
|
|
||||||
return CommandResult.NotMoved;
|
return CommandResult.NotMoved;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.OutputSystem;
|
|
||||||
import chess.controller.PlayerCommand;
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
@@ -9,9 +9,9 @@ import chess.model.Game;
|
|||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.pieces.Bishop;
|
import chess.model.pieces.Bishop;
|
||||||
import chess.model.pieces.Knight;
|
import chess.model.pieces.Knight;
|
||||||
import chess.model.pieces.Pawn;
|
|
||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
|
||||||
public class PromoteCommand extends PlayerCommand {
|
public class PromoteCommand extends PlayerCommand {
|
||||||
|
|
||||||
@@ -24,22 +24,25 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
|
|
||||||
private final PromoteType promoteType;
|
private final PromoteType promoteType;
|
||||||
private Coordinate pieceCoords;
|
private Coordinate pieceCoords;
|
||||||
|
private Piece oldPawn;
|
||||||
|
|
||||||
public PromoteCommand(PromoteType promoteType) {
|
public PromoteCommand(PromoteType promoteType) {
|
||||||
this.promoteType = promoteType;
|
this.promoteType = promoteType;
|
||||||
|
this.pieceCoords = null;
|
||||||
|
this.oldPawn = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
this.pieceCoords = game.pawnPromotePosition();
|
this.pieceCoords = board.pawnPromotePosition();
|
||||||
|
|
||||||
if (this.pieceCoords == null)
|
if (this.pieceCoords == null)
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||||
if (pawn == null || !(pawn instanceof Pawn))
|
if (!new PawnIdentifier(game.getPlayerTurn()).isPawn(pawn))
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
int destY = this.pieceCoords.getY();
|
int destY = this.pieceCoords.getY();
|
||||||
@@ -49,6 +52,7 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
if (destY != enemyLine)
|
if (destY != enemyLine)
|
||||||
return CommandResult.NotAllowed;
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
this.oldPawn = pawn;
|
||||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
@@ -74,15 +78,24 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo(Game game, OutputSystem outputSystem) {
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
final ChessBoard board = game.getBoard();
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
Piece promoted = board.pieceAt(this.pieceCoords);
|
Piece promoted = board.pieceAt(this.pieceCoords);
|
||||||
|
|
||||||
assert promoted != null;
|
assert promoted != null;
|
||||||
|
|
||||||
Color player = promoted.getColor();
|
game.undoTraitPiecesPos();
|
||||||
board.pieceComes(new Pawn(player), this.pieceCoords);
|
|
||||||
|
board.pieceComes(this.oldPawn, this.pieceCoords);
|
||||||
|
|
||||||
|
game.getLastAction().undo(game, outputSystem);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PromoteType getPromoteType() {
|
||||||
|
return promoteType;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
@@ -14,9 +14,10 @@ public class SurrenderCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
outputSystem.hasSurrendered(player);
|
outputSystem.onSurrender(player);
|
||||||
outputSystem.winnerIs(Color.getEnemy(player));
|
outputSystem.onWin(Color.getEnemy(player));
|
||||||
|
outputSystem.onGameEnd();
|
||||||
return CommandResult.NotMoved;
|
return CommandResult.NotMoved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
package chess.controller.commands;
|
package chess.controller.commands;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
|
||||||
public class UndoCommand extends Command{
|
public class UndoCommand extends Command{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
//TODO
|
PlayerCommand lastAction = game.getLastAction();
|
||||||
return CommandResult.Moved;
|
if (lastAction == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
return lastAction.undo(game, outputSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class EmptyGameDispatcher extends GameDispatcher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color coward) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color winner) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
48
app/src/main/java/chess/controller/event/GameAdaptator.java
Normal file
48
app/src/main/java/chess/controller/event/GameAdaptator.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public abstract class GameAdaptator implements GameListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {}
|
||||||
|
|
||||||
|
}
|
||||||
100
app/src/main/java/chess/controller/event/GameDispatcher.java
Normal file
100
app/src/main/java/chess/controller/event/GameDispatcher.java
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class GameDispatcher implements GameListener {
|
||||||
|
|
||||||
|
private final List<GameListener> listeners;
|
||||||
|
private final ExecutorService executor;
|
||||||
|
|
||||||
|
public GameDispatcher() {
|
||||||
|
this.listeners = new ArrayList<>();
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(GameListener listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void asyncForEachCall(Consumer<GameListener> func) {
|
||||||
|
this.executor.execute(() -> this.listeners.forEach(func));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
asyncForEachCall((l) -> l.onPlayerTurn(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color color) {
|
||||||
|
asyncForEachCall((l) -> l.onWin(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
asyncForEachCall((l) -> l.onKingInCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
asyncForEachCall((l) -> l.onKingInMat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
asyncForEachCall((l) -> l.onPatSituation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color color) {
|
||||||
|
asyncForEachCall((l) -> l.onSurrender(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
asyncForEachCall((l) -> l.onGameStart());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
asyncForEachCall((l) -> l.onPromotePawn(pieceCoords));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
asyncForEachCall((l) -> l.onBoardUpdate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
asyncForEachCall((l) -> l.onGameEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
asyncForEachCall((l) -> l.onMove(move));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
asyncForEachCall((l) -> l.onMoveNotAllowed(move));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
asyncForEachCall((l) -> l.onDraw());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopService() {
|
||||||
|
this.executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
80
app/src/main/java/chess/controller/event/GameListener.java
Normal file
80
app/src/main/java/chess/controller/event/GameListener.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public interface GameListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when the display of the board should be updated
|
||||||
|
*/
|
||||||
|
void onBoardUpdate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a draw occurs (same position is repeated three times)
|
||||||
|
*/
|
||||||
|
void onDraw();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when the game has ended (by a win or a draw)
|
||||||
|
*/
|
||||||
|
void onGameEnd();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when the game has started
|
||||||
|
*/
|
||||||
|
void onGameStart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a king is in check
|
||||||
|
*/
|
||||||
|
void onKingInCheck();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a checkmate occurs
|
||||||
|
*/
|
||||||
|
void onKingInMat();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a valid move on the board occurs
|
||||||
|
* @param move the move to be processed
|
||||||
|
*/
|
||||||
|
void onMove(Move move);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a sent move is not allowed
|
||||||
|
* @param move the move to be processed
|
||||||
|
*/
|
||||||
|
void onMoveNotAllowed(Move move);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a pat situation occurs
|
||||||
|
*/
|
||||||
|
void onPatSituation();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when it's the player turn
|
||||||
|
* @param color the color of the player who should play
|
||||||
|
*/
|
||||||
|
void onPlayerTurn(Color color);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a pawn should be promoted
|
||||||
|
* @param pieceCoords the coordinates of the pawn
|
||||||
|
*/
|
||||||
|
void onPromotePawn(Coordinate pieceCoords);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a players surrenders
|
||||||
|
* @param coward the player who gave up
|
||||||
|
*/
|
||||||
|
void onSurrender(Color coward);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked when a player wins (by checkmate or if the other one surrenders)
|
||||||
|
* @param winner
|
||||||
|
*/
|
||||||
|
void onWin(Color winner);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,8 +2,10 @@ package chess.model;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import chess.model.visitor.KingIdentifier;
|
import chess.model.visitor.KingIdentifier;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
import chess.model.visitor.PiecePathChecker;
|
import chess.model.visitor.PiecePathChecker;
|
||||||
|
|
||||||
public class ChessBoard {
|
public class ChessBoard {
|
||||||
@@ -25,6 +27,7 @@ public class ChessBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final Cell[][] cells;
|
private final Cell[][] cells;
|
||||||
|
private Move lastVirtualMove;
|
||||||
private Move lastMove;
|
private Move lastMove;
|
||||||
private Piece lastEjectedPiece;
|
private Piece lastEjectedPiece;
|
||||||
|
|
||||||
@@ -35,35 +38,39 @@ public class ChessBoard {
|
|||||||
this.cells[i][j] = new Cell();
|
this.cells[i][j] = new Cell();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.lastVirtualMove = null;
|
||||||
this.lastMove = null;
|
this.lastMove = null;
|
||||||
this.lastEjectedPiece = null;
|
this.lastEjectedPiece = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyMove(Move move) {
|
public void applyMove(Move move) {
|
||||||
assert move.isValid() : "Invalid move !";
|
assert move.isValid() : "Invalid move !";
|
||||||
Piece deadPiece = pieceAt(move.getFinish());
|
Piece deadPiece = pieceAt(move.getDeadPieceCoords());
|
||||||
if (deadPiece != null) {
|
if (deadPiece != null) {
|
||||||
this.lastEjectedPiece = deadPiece;
|
this.lastEjectedPiece = deadPiece;
|
||||||
} else {
|
} else {
|
||||||
this.lastEjectedPiece = null;
|
this.lastEjectedPiece = null;
|
||||||
}
|
}
|
||||||
Piece movingPiece = pieceAt(move.getStart());
|
Piece movingPiece = pieceAt(move.getStart());
|
||||||
pieceComes(movingPiece, move.getFinish());
|
pieceLeaves(move.getDeadPieceCoords());
|
||||||
pieceLeaves(move.getStart());
|
pieceLeaves(move.getStart());
|
||||||
|
pieceComes(movingPiece, move.getFinish());
|
||||||
movingPiece.move();
|
movingPiece.move();
|
||||||
this.lastMove = move;
|
this.lastVirtualMove = move;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
assert this.lastMove != null: "Can't undo at the beginning!";
|
assert this.lastVirtualMove != null : "Can't undo at the beginning!";
|
||||||
|
|
||||||
undoMove(this.lastMove, this.lastEjectedPiece);
|
undoMove(this.lastVirtualMove, this.lastEjectedPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoMove(Move move, Piece deadPiece) {
|
public void undoMove(Move move, Piece deadPiece) {
|
||||||
Piece movingPiece = pieceAt(move.getFinish());
|
Piece movingPiece = pieceAt(move.getFinish());
|
||||||
pieceComes(movingPiece, move.getStart());
|
pieceComes(movingPiece, move.getStart());
|
||||||
pieceComes(deadPiece, move.getFinish());
|
pieceLeaves(move.getFinish());
|
||||||
|
pieceComes(deadPiece, move.getDeadPieceCoords());
|
||||||
|
assert movingPiece != null;
|
||||||
movingPiece.unMove();
|
movingPiece.unMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +110,7 @@ public class ChessBoard {
|
|||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate coordinate = new Coordinate(i, j);
|
Coordinate coordinate = new Coordinate(i, j);
|
||||||
Piece piece = pieceAt(coordinate);
|
Piece piece = pieceAt(coordinate);
|
||||||
if (piece != null && kingIdentifier.visit(piece)) {
|
if (kingIdentifier.isKing(piece)) {
|
||||||
return coordinate;
|
return coordinate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,21 +139,40 @@ public class ChessBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasAllowedMoves(Color player) {
|
public boolean hasAllowedMoves(Color player) {
|
||||||
|
return !getAllowedMoves(player).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Move> getAllowedMoves(Color player) {
|
||||||
|
List<Move> result = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||||
|
for (int y = 0; y < Coordinate.VALUE_MAX; y++) {
|
||||||
|
|
||||||
|
Coordinate start = new Coordinate(x, y);
|
||||||
|
|
||||||
|
Piece piece = pieceAt(start);
|
||||||
|
if (piece == null || piece.getColor() != player)
|
||||||
|
continue;
|
||||||
|
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate attackCoords = new Coordinate(i, j);
|
Coordinate destination = new Coordinate(i, j);
|
||||||
Piece attackPiece = pieceAt(attackCoords);
|
Move move = new Move(start, destination);
|
||||||
if (attackPiece == null)
|
|
||||||
|
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
||||||
|
move);
|
||||||
|
if (!piecePathChecker.isValid())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (attackPiece.getColor() != player)
|
applyMove(move);
|
||||||
continue;
|
if (!isKingInCheck(player))
|
||||||
|
result.add(move);
|
||||||
if (!getAllowedMoves(attackCoords).isEmpty())
|
undoLastMove();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Coordinate> getAllowedMoves(Coordinate pieceCoords) {
|
public List<Coordinate> getAllowedMoves(Coordinate pieceCoords) {
|
||||||
@@ -177,4 +203,110 @@ public class ChessBoard {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean canCastle(Color color, int rookX, Direction kingDirection) {
|
||||||
|
if (isKingInCheck(color))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int colorLine = color == Color.White ? 7 : 0;
|
||||||
|
|
||||||
|
Coordinate kingCoords = new Coordinate(4, colorLine);
|
||||||
|
Coordinate rookCoords = new Coordinate(rookX, colorLine);
|
||||||
|
Piece king = pieceAt(kingCoords);
|
||||||
|
Piece rook = pieceAt(rookCoords);
|
||||||
|
|
||||||
|
if (king == null || rook == null || king.hasMoved() || rook.hasMoved())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (int step = 1; step <= 2; step++) {
|
||||||
|
Coordinate dest = Coordinate.fromIndex(kingCoords.toIndex() + step * kingDirection.getIndexOffset());
|
||||||
|
Piece obstacle = pieceAt(dest);
|
||||||
|
if (obstacle != null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
applyMove(new Move(kingCoords, dest));
|
||||||
|
if (isKingInCheck(color)) {
|
||||||
|
undoLastMove();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
undoLastMove();
|
||||||
|
}
|
||||||
|
|
||||||
|
Coordinate rookObstacleCoords = Coordinate.fromIndex(rookCoords.toIndex() - kingDirection.getIndexOffset());
|
||||||
|
Piece obstacle = pieceAt(rookObstacleCoords);
|
||||||
|
|
||||||
|
return obstacle == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canSmallCastle(Color color) {
|
||||||
|
return canCastle(color, 7, Direction.Right);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canBigCastle(Color color) {
|
||||||
|
return canCastle(color, 0, Direction.Left);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean pawnShouldBePromoted() {
|
||||||
|
return pawnPromotePosition() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Null if there is no pawn to promote
|
||||||
|
*/
|
||||||
|
public Coordinate pawnPromotePosition() {
|
||||||
|
Coordinate piecePos = pawnPromotePosition(Color.White);
|
||||||
|
if (piecePos != null)
|
||||||
|
return piecePos;
|
||||||
|
return pawnPromotePosition(Color.Black);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Null if there is no pawn to promote
|
||||||
|
*/
|
||||||
|
private Coordinate pawnPromotePosition(Color color) {
|
||||||
|
int enemyLineY = color == Color.White ? 0 : 7;
|
||||||
|
PawnIdentifier identifier = new PawnIdentifier(color);
|
||||||
|
|
||||||
|
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||||
|
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
||||||
|
Piece piece = pieceAt(pieceCoords);
|
||||||
|
|
||||||
|
if (identifier.isPawn(piece))
|
||||||
|
return pieceCoords;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
Piece piece = pieceAt(new Coordinate(i, j));
|
||||||
|
if (piece == null)
|
||||||
|
continue;
|
||||||
|
result = Objects.hash(result, piece.getColor(), new Coordinate(i, j), piece);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Move getLastMove() {
|
||||||
|
return this.lastMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastMove(Move lastMove) {
|
||||||
|
this.lastMove = lastMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj){
|
||||||
|
if (obj instanceof ChessBoard board)
|
||||||
|
return board.hashCode() == this.hashCode();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Coordinate {
|
public class Coordinate {
|
||||||
private final int x;
|
private final int x;
|
||||||
private final int y;
|
private final int y;
|
||||||
@@ -38,4 +40,13 @@ public class Coordinate {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "(" + this.x + ", " + this.y + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(this.x, this.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,29 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
import chess.model.pieces.Pawn;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private final ChessBoard board;
|
private final ChessBoard board;
|
||||||
private Color playerTurn;
|
private Color playerTurn;
|
||||||
|
private final Stack<PlayerCommand> movesHistory;
|
||||||
|
private final Map<Integer, Integer> traitsPos;
|
||||||
|
|
||||||
|
private static final int DRAW_REPETITONS = 3;
|
||||||
|
|
||||||
public enum GameStatus {
|
public enum GameStatus {
|
||||||
Check, CheckMate, OnGoing, Pat;
|
Draw, Check, CheckMate, OnGoing, Pat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Game(ChessBoard board) {
|
public Game(ChessBoard board) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
|
this.movesHistory = new Stack<>();
|
||||||
|
this.traitsPos = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChessBoard getBoard() {
|
public ChessBoard getBoard() {
|
||||||
@@ -22,10 +34,38 @@ public class Game {
|
|||||||
return playerTurn;
|
return playerTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
resetPlayerTurn();
|
||||||
|
this.traitsPos.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public void resetPlayerTurn() {
|
public void resetPlayerTurn() {
|
||||||
this.playerTurn = Color.White;
|
this.playerTurn = Color.White;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param color the current player turn
|
||||||
|
* @return true if a draw should be declared
|
||||||
|
*/
|
||||||
|
public void saveTraitPiecesPos() {
|
||||||
|
int piecesHash = this.board.hashCode();
|
||||||
|
Integer count = this.traitsPos.get(piecesHash);
|
||||||
|
this.traitsPos.put(piecesHash, count == null ? 1 : count + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return whether the game is in a draw situation
|
||||||
|
*/
|
||||||
|
private boolean checkDraw() {
|
||||||
|
return this.traitsPos.containsValue(DRAW_REPETITONS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return true if a draw should occur
|
||||||
|
*/
|
||||||
public void switchPlayerTurn() {
|
public void switchPlayerTurn() {
|
||||||
playerTurn = Color.getEnemy(playerTurn);
|
playerTurn = Color.getEnemy(playerTurn);
|
||||||
}
|
}
|
||||||
@@ -33,6 +73,9 @@ public class Game {
|
|||||||
public GameStatus checkGameStatus() {
|
public GameStatus checkGameStatus() {
|
||||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||||
|
|
||||||
|
if (checkDraw())
|
||||||
|
return GameStatus.Draw;
|
||||||
|
|
||||||
if (this.board.isKingInCheck(enemy))
|
if (this.board.isKingInCheck(enemy))
|
||||||
if (this.board.hasAllowedMoves(enemy))
|
if (this.board.hasAllowedMoves(enemy))
|
||||||
return GameStatus.Check;
|
return GameStatus.Check;
|
||||||
@@ -45,39 +88,35 @@ public class Game {
|
|||||||
return GameStatus.OnGoing;
|
return GameStatus.OnGoing;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean pawnShouldBePromoted() {
|
public void addAction(PlayerCommand command) {
|
||||||
return pawnPromotePosition() != null;
|
this.movesHistory.add(command);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return Null if there is no pawn to promote
|
|
||||||
*/
|
|
||||||
public Coordinate pawnPromotePosition() {
|
|
||||||
Coordinate piecePos = pawnPromotePosition(Color.White);
|
|
||||||
if (piecePos != null)
|
|
||||||
return piecePos;
|
|
||||||
return pawnPromotePosition(Color.Black);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return Null if there is no pawn to promote
|
|
||||||
*/
|
|
||||||
private Coordinate pawnPromotePosition(Color color) {
|
|
||||||
int enemyLineY = color == Color.White ? 0 : 7;
|
|
||||||
|
|
||||||
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
|
||||||
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
|
||||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
|
||||||
|
|
||||||
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
return pieceCoords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PlayerCommand getLastAction() {
|
||||||
|
if (this.movesHistory.isEmpty())
|
||||||
return null;
|
return null;
|
||||||
|
return this.movesHistory.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateLastMove() {
|
||||||
|
if (this.movesHistory.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
PlayerCommand last = this.movesHistory.getLast();
|
||||||
|
if (last instanceof MoveCommand move) {
|
||||||
|
this.board.setLastMove(move.getMove());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undoTraitPiecesPos() {
|
||||||
|
int piecesHash = this.board.hashCode();
|
||||||
|
Integer count = this.traitsPos.get(piecesHash);
|
||||||
|
if (count != null)
|
||||||
|
this.traitsPos.put(piecesHash, count - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PlayerCommand> getMoves() {
|
||||||
|
return this.movesHistory;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ package chess.model;
|
|||||||
public class Move {
|
public class Move {
|
||||||
private final Coordinate start;
|
private final Coordinate start;
|
||||||
private final Coordinate finish;
|
private final Coordinate finish;
|
||||||
|
private Coordinate deadPieceCoords;
|
||||||
|
|
||||||
public Move(Coordinate start, Coordinate finish) {
|
public Move(Coordinate start, Coordinate finish) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.finish = finish;
|
this.finish = finish;
|
||||||
|
this.deadPieceCoords = finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
@@ -28,7 +30,7 @@ public class Move {
|
|||||||
int diffY = getFinish().getY() - getStart().getY();
|
int diffY = getFinish().getY() - getStart().getY();
|
||||||
|
|
||||||
assert Math.abs(diffX) < Coordinate.VALUE_MAX : "Move is too big!";
|
assert Math.abs(diffX) < Coordinate.VALUE_MAX : "Move is too big!";
|
||||||
assert Math.abs(diffX) < Coordinate.VALUE_MAX : "Move is too big!";
|
assert Math.abs(diffY) < Coordinate.VALUE_MAX : "Move is too big!";
|
||||||
|
|
||||||
if (diffX == 0)
|
if (diffX == 0)
|
||||||
return Math.abs(diffY);
|
return Math.abs(diffY);
|
||||||
@@ -39,4 +41,23 @@ public class Move {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Coordinate getMiddle() {
|
||||||
|
return Coordinate.fromIndex((getStart().toIndex() + getFinish().toIndex()) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeadPieceCoords(Coordinate deadCoords) {
|
||||||
|
this.deadPieceCoords = deadCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coordinate getDeadPieceCoords() {
|
||||||
|
return deadPieceCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Move other)
|
||||||
|
return this.start.equals(other.start) && this.finish.equals(other.finish);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,4 +28,7 @@ public abstract class Piece {
|
|||||||
|
|
||||||
public abstract <T> T accept(PieceVisitor<T> visitor);
|
public abstract <T> T accept(PieceVisitor<T> visitor);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract boolean equals(Object other);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,13 @@ public class Bishop extends Piece {
|
|||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Bishop && ((Bishop) obj).getColor() == this.getColor());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,13 @@ public class King extends Piece {
|
|||||||
public <T> T accept(PieceVisitor<T> visitor) {
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof King && ((King) obj).getColor() == this.getColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,13 @@ public class Knight extends Piece {
|
|||||||
public <T> T accept(PieceVisitor<T> visitor) {
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Knight && ((Knight) obj).getColor() == this.getColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,13 @@ public class Pawn extends Piece {
|
|||||||
public int multiplier() {
|
public int multiplier() {
|
||||||
return getColor() == Color.White ? 1 : -1;
|
return getColor() == Color.White ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Pawn && ((Pawn) obj).getColor() == this.getColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,13 @@ public class Queen extends Piece {
|
|||||||
public <T> T accept(PieceVisitor<T> visitor) {
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof Queen && ((Queen) obj).getColor() == this.getColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,13 @@ public class Rook extends Piece {
|
|||||||
public <T> T accept(PieceVisitor<T> visitor) {
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
return (other instanceof Rook && ((Rook) other).getColor() == this.getColor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package chess.model.visitor;
|
package chess.model.visitor;
|
||||||
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
import chess.model.pieces.*;
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
@@ -12,6 +13,12 @@ public class KingIdentifier implements PieceVisitor<Boolean> {
|
|||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isKing(Piece piece) {
|
||||||
|
if (piece == null)
|
||||||
|
return false;
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visitPiece(Bishop bishop) {
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
52
app/src/main/java/chess/model/visitor/PawnIdentifier.java
Normal file
52
app/src/main/java/chess/model/visitor/PawnIdentifier.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package chess.model.visitor;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public class PawnIdentifier implements PieceVisitor<Boolean> {
|
||||||
|
|
||||||
|
private final Color color;
|
||||||
|
|
||||||
|
public PawnIdentifier(Color color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPawn(Piece piece) {
|
||||||
|
if (piece == null)
|
||||||
|
return false;
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(King king) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Knight knight) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Pawn pawn) {
|
||||||
|
return pawn.getColor() == color;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Queen queen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -35,31 +35,27 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visitPiece(Bishop bishop) {
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
if (!new PermissiveRuleChecker(this.move).isValidFor(bishop))
|
return basicCheck(bishop);
|
||||||
return false;
|
|
||||||
return testPath(bishop.getColor());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visitPiece(King king) {
|
public Boolean visitPiece(King king) {
|
||||||
if (!new PermissiveRuleChecker(this.move).isValidFor(king))
|
return destCheck(king);
|
||||||
return false;
|
|
||||||
|
|
||||||
Piece destPiece = board.pieceAt(this.move.getFinish());
|
|
||||||
if (destPiece == null)
|
|
||||||
return true;
|
|
||||||
return destPiece.getColor() != king.getColor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visitPiece(Knight knight) {
|
public Boolean visitPiece(Knight knight) {
|
||||||
if (!new PermissiveRuleChecker(this.move).isValidFor(knight))
|
return destCheck(knight);
|
||||||
return false;
|
}
|
||||||
|
|
||||||
Piece destPiece = board.pieceAt(this.move.getFinish());
|
@Override
|
||||||
if (destPiece == null)
|
public Boolean visitPiece(Queen queen) {
|
||||||
return true;
|
return basicCheck(queen);
|
||||||
return destPiece.getColor() != knight.getColor();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
return basicCheck(rook);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,6 +70,9 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
|||||||
|
|
||||||
assert moveDirection == Direction.FrontLeft || moveDirection == Direction.FrontRight;
|
assert moveDirection == Direction.FrontLeft || moveDirection == Direction.FrontRight;
|
||||||
|
|
||||||
|
if (checkEnPassant())
|
||||||
|
return true;
|
||||||
|
|
||||||
Piece destPiece = this.board.pieceAt(this.move.getFinish());
|
Piece destPiece = this.board.pieceAt(this.move.getFinish());
|
||||||
if (destPiece == null)
|
if (destPiece == null)
|
||||||
return false;
|
return false;
|
||||||
@@ -81,18 +80,51 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
|||||||
return destPiece.getColor() != pawn.getColor();
|
return destPiece.getColor() != pawn.getColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean checkEnPassant() {
|
||||||
public Boolean visitPiece(Queen queen) {
|
Move lastMove = this.board.getLastMove();
|
||||||
if (!new PermissiveRuleChecker(this.move).isValidFor(queen))
|
|
||||||
|
if (lastMove == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Piece pieceToEat = this.board.pieceAt(lastMove.getFinish());
|
||||||
|
|
||||||
|
if (pieceToEat == null || !(pieceToEat instanceof Pawn))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Piece pawn = this.board.pieceAt(this.move.getStart());
|
||||||
|
|
||||||
|
if (pieceToEat.getColor() == pawn.getColor())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Direction lastMoveDir = Direction.findDirection(lastMove);
|
||||||
|
|
||||||
|
if ((lastMoveDir != Direction.Front && lastMoveDir != Direction.Back) || lastMove.traversedCells() != 2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Coordinate middle = lastMove.getMiddle();
|
||||||
|
|
||||||
|
if (middle.equals(this.move.getFinish())
|
||||||
|
&& new PawnIdentifier(pieceToEat.getColor()).isPawn(pieceToEat)) {
|
||||||
|
this.move.setDeadPieceCoords(lastMove.getFinish());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
return testPath(queen.getColor());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean destCheck(Piece piece) {
|
||||||
public Boolean visitPiece(Rook rook) {
|
if (!new PermissiveRuleChecker(this.move).isValidFor(piece))
|
||||||
if (!new PermissiveRuleChecker(this.move).isValidFor(rook))
|
|
||||||
return false;
|
return false;
|
||||||
return testPath(rook.getColor());
|
|
||||||
|
Piece destPiece = board.pieceAt(this.move.getFinish());
|
||||||
|
if (destPiece == null)
|
||||||
|
return true;
|
||||||
|
return destPiece.getColor() != piece.getColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean basicCheck(Piece piece) {
|
||||||
|
if (!new PermissiveRuleChecker(this.move).isValidFor(piece))
|
||||||
|
return false;
|
||||||
|
return testPath(piece.getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean testPath(Color color) {
|
private boolean testPath(Color color) {
|
||||||
|
|||||||
224
app/src/main/java/chess/pgn/PgnExport.java
Normal file
224
app/src/main/java/chess/pgn/PgnExport.java
Normal file
@@ -0,0 +1,224 @@
|
|||||||
|
package chess.pgn;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.commands.CastlingCommand;
|
||||||
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GetPlayerMovesCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.event.EmptyGameDispatcher;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.pieces.Pawn;
|
||||||
|
|
||||||
|
public class PgnExport {
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// final Game game = new Game(new ChessBoard());
|
||||||
|
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
// commandExecutor.addListener(ai1);
|
||||||
|
|
||||||
|
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
// commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
|
// commandExecutor.addListener(new GameAdaptator() {
|
||||||
|
// @Override
|
||||||
|
// public void onGameEnd() {
|
||||||
|
// System.out.println(exportGame(game));
|
||||||
|
// commandExecutor.close();
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
// commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
// }
|
||||||
|
|
||||||
|
private static final PiecePgnName piecePgnName = new PiecePgnName();
|
||||||
|
|
||||||
|
private static Piece pieceAt(CommandExecutor commandExecutor, Coordinate coordinate) {
|
||||||
|
GetPieceAtCommand cmd = new GetPieceAtCommand(coordinate);
|
||||||
|
commandExecutor.executeCommand(cmd);
|
||||||
|
return cmd.getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Move> playerMoves(CommandExecutor commandExecutor) {
|
||||||
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||||
|
commandExecutor.executeCommand(cmd);
|
||||||
|
return cmd.getMoves();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String gameEnd(Game game) {
|
||||||
|
switch (game.checkGameStatus()) {
|
||||||
|
case Draw:
|
||||||
|
case Pat:
|
||||||
|
return "1/2-1/2";
|
||||||
|
|
||||||
|
case CheckMate:
|
||||||
|
if (game.getPlayerTurn() == Color.White)
|
||||||
|
return "1-0";
|
||||||
|
return "0-1";
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String resolveAmbiguity(CommandExecutor cmdExec, Move pieceMove) {
|
||||||
|
Piece movingPiece = pieceAt(cmdExec, pieceMove.getStart());
|
||||||
|
|
||||||
|
assert movingPiece != null;
|
||||||
|
|
||||||
|
if (movingPiece instanceof Pawn)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
List<Move> moves = playerMoves(cmdExec);
|
||||||
|
|
||||||
|
for (Move move : moves) {
|
||||||
|
if (move.equals(pieceMove) || !move.getFinish().equals(pieceMove.getFinish()))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Piece otherPiece = pieceAt(cmdExec, move.getStart());
|
||||||
|
|
||||||
|
// checking type of piece
|
||||||
|
if (otherPiece.hashCode() != movingPiece.hashCode())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String startPos = toString(pieceMove.getStart());
|
||||||
|
|
||||||
|
if (move.getStart().getX() != pieceMove.getStart().getX())
|
||||||
|
// not on the same column
|
||||||
|
return Character.toString(startPos.charAt(0));
|
||||||
|
else
|
||||||
|
return Character.toString(startPos.charAt(1));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String capture(MoveCommand move, Piece movingPiece) {
|
||||||
|
String result = "";
|
||||||
|
if (move.getDeadPiece() != null) {
|
||||||
|
if (movingPiece instanceof Pawn) {
|
||||||
|
result += toString(move.getMove().getStart()).charAt(0);
|
||||||
|
}
|
||||||
|
result += "x";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String promote(PlayerCommand nextCommand) {
|
||||||
|
if (nextCommand != null && nextCommand instanceof PromoteCommand promoteCommand) {
|
||||||
|
String result = "=";
|
||||||
|
result += switch (promoteCommand.getPromoteType()) {
|
||||||
|
case Bishop -> "B";
|
||||||
|
case Knight -> "N";
|
||||||
|
case Queen -> "Q";
|
||||||
|
case Rook -> "R";
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String castling(CastlingCommand castlingCommand) {
|
||||||
|
String result = "O-O";
|
||||||
|
if (castlingCommand.isBigCastling())
|
||||||
|
result += "-O";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String checkCheckMate(Game game) {
|
||||||
|
switch (game.checkGameStatus()) {
|
||||||
|
case CheckMate:
|
||||||
|
return "#";
|
||||||
|
|
||||||
|
case Check:
|
||||||
|
return "+";
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String printMove(PlayerCommand cmd, PlayerCommand nextCommand, Game virtualGame,
|
||||||
|
CommandExecutor executor) {
|
||||||
|
String result = "";
|
||||||
|
if (cmd instanceof MoveCommand move) {
|
||||||
|
|
||||||
|
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||||
|
|
||||||
|
assert movingPiece != null;
|
||||||
|
|
||||||
|
// piece name
|
||||||
|
result += piecePgnName.visit(movingPiece);
|
||||||
|
|
||||||
|
// ambiguious start
|
||||||
|
result += resolveAmbiguity(executor, move.getMove());
|
||||||
|
|
||||||
|
// capture
|
||||||
|
result += capture(move, movingPiece);
|
||||||
|
|
||||||
|
// end cell
|
||||||
|
result += toString(move.getMove().getFinish());
|
||||||
|
|
||||||
|
// promote
|
||||||
|
result += promote(nextCommand);
|
||||||
|
|
||||||
|
} else if (cmd instanceof CastlingCommand castlingCommand) {
|
||||||
|
result += castling(castlingCommand);
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.executeCommand(cmd);
|
||||||
|
|
||||||
|
// check or checkmate
|
||||||
|
result += checkCheckMate(virtualGame);
|
||||||
|
|
||||||
|
result += " ";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String exportGame(Game game) {
|
||||||
|
|
||||||
|
ChessBoard board = new ChessBoard();
|
||||||
|
Game virtualGame = new Game(board);
|
||||||
|
|
||||||
|
CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher());
|
||||||
|
executor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
List<PlayerCommand> commands = game.getMoves();
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
int tour = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < commands.size(); i++) {
|
||||||
|
PlayerCommand cmd = commands.get(i);
|
||||||
|
PlayerCommand nextCommand = null;
|
||||||
|
if (i != commands.size() - 1) {
|
||||||
|
nextCommand = commands.get(i + 1);
|
||||||
|
}
|
||||||
|
if (virtualGame.getPlayerTurn() == Color.White) {
|
||||||
|
result += tour + ".";
|
||||||
|
tour++;
|
||||||
|
}
|
||||||
|
result += printMove(cmd, nextCommand, virtualGame, executor);
|
||||||
|
}
|
||||||
|
return result + " " + gameEnd(virtualGame);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toString(Coordinate coordinate) {
|
||||||
|
String letters = "abcdefgh";
|
||||||
|
String numbers = "87654321";
|
||||||
|
return Character.toString(letters.charAt(coordinate.getX())) + numbers.charAt(coordinate.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
43
app/src/main/java/chess/pgn/PiecePgnName.java
Normal file
43
app/src/main/java/chess/pgn/PiecePgnName.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package chess.pgn;
|
||||||
|
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.Bishop;
|
||||||
|
import chess.model.pieces.King;
|
||||||
|
import chess.model.pieces.Knight;
|
||||||
|
import chess.model.pieces.Pawn;
|
||||||
|
import chess.model.pieces.Queen;
|
||||||
|
import chess.model.pieces.Rook;
|
||||||
|
|
||||||
|
public class PiecePgnName implements PieceVisitor<String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Bishop bishop) {
|
||||||
|
return "B";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(King king) {
|
||||||
|
return "K";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Knight knight) {
|
||||||
|
return "N";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Pawn pawn) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Queen queen) {
|
||||||
|
return "Q";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Rook rook) {
|
||||||
|
return "R";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
28
app/src/main/java/chess/simulator/FoolCheckMate.java
Normal file
28
app/src/main/java/chess/simulator/FoolCheckMate.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package chess.simulator;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class FoolCheckMate extends Simulator {
|
||||||
|
|
||||||
|
public FoolCheckMate(CommandExecutor commandExecutor) {
|
||||||
|
super(commandExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Move> getMoves() {
|
||||||
|
return Arrays.asList(
|
||||||
|
// white pawn
|
||||||
|
new Move(new Coordinate(5, 6), new Coordinate(5, 5)),
|
||||||
|
// black pawn
|
||||||
|
new Move(new Coordinate(4, 1), new Coordinate(4, 3)),
|
||||||
|
// 2nd white pawn
|
||||||
|
new Move(new Coordinate(6, 6), new Coordinate(6, 4)),
|
||||||
|
// black queen
|
||||||
|
new Move(new Coordinate(3, 0), new Coordinate(7, 4)));
|
||||||
|
}
|
||||||
|
}
|
||||||
40
app/src/main/java/chess/simulator/PromoteTest.java
Normal file
40
app/src/main/java/chess/simulator/PromoteTest.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package chess.simulator;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
|
public class PromoteTest extends Simulator{
|
||||||
|
|
||||||
|
public PromoteTest(CommandExecutor commandExecutor) {
|
||||||
|
super(commandExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<Move> getMoves() {
|
||||||
|
return Arrays.asList(
|
||||||
|
// white pawn
|
||||||
|
new Move(new Coordinate(5, 6), new Coordinate(5, 4)),
|
||||||
|
// black pawn
|
||||||
|
new Move(new Coordinate(4, 1), new Coordinate(4, 3)),
|
||||||
|
// white pawn capture
|
||||||
|
new Move(new Coordinate(5, 4), new Coordinate(4, 3)),
|
||||||
|
// black king
|
||||||
|
new Move(new Coordinate(4, 0), new Coordinate(4, 1)),
|
||||||
|
// white pawn moves
|
||||||
|
new Move(new Coordinate(4, 3), new Coordinate(4, 2)),
|
||||||
|
// black king
|
||||||
|
new Move(new Coordinate(4, 1), new Coordinate(5, 2)),
|
||||||
|
// white pawn moves
|
||||||
|
new Move(new Coordinate(4, 2), new Coordinate(4, 1)),
|
||||||
|
// black king
|
||||||
|
new Move(new Coordinate(5, 2), new Coordinate(6, 2))
|
||||||
|
// white pawn moves
|
||||||
|
// new Move(new Coordinate(4, 1), new Coordinate(4, 0))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
39
app/src/main/java/chess/simulator/Simulator.java
Normal file
39
app/src/main/java/chess/simulator/Simulator.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package chess.simulator;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.Move;
|
||||||
|
import common.Signal0;
|
||||||
|
|
||||||
|
public abstract class Simulator extends GameAdaptator {
|
||||||
|
|
||||||
|
protected final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
|
public final Signal0 onComplete = new Signal0();
|
||||||
|
private int currentMove = 0;
|
||||||
|
|
||||||
|
public Simulator(CommandExecutor commandExecutor) {
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
for (Move move : getMoves()) {
|
||||||
|
this.commandExecutor.executeCommand(new MoveCommand(move));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
currentMove++;
|
||||||
|
if (currentMove == getMoves().size()) {
|
||||||
|
onComplete.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract List<Move> getMoves();
|
||||||
|
|
||||||
|
}
|
||||||
38
app/src/main/java/chess/view/AssetManager.java
Normal file
38
app/src/main/java/chess/view/AssetManager.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package chess.view;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class AssetManager {
|
||||||
|
|
||||||
|
private static final String gradleBase = "app/src/main/resources/";
|
||||||
|
|
||||||
|
public static InputStream getResource(String name) {
|
||||||
|
// we first search it in files
|
||||||
|
InputStream inputStream = getFileInputStream(name);
|
||||||
|
if (inputStream != null)
|
||||||
|
return inputStream;
|
||||||
|
|
||||||
|
inputStream = getFileInputStream(gradleBase + name);
|
||||||
|
if (inputStream != null)
|
||||||
|
return inputStream;
|
||||||
|
// then in the jar
|
||||||
|
return ClassLoader.getSystemResourceAsStream(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static InputStream getFileInputStream(String path) {
|
||||||
|
File f = new File(path);
|
||||||
|
if (f.exists()) {
|
||||||
|
FileInputStream fis;
|
||||||
|
try {
|
||||||
|
fis = new FileInputStream(f);
|
||||||
|
return fis;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
38
app/src/main/java/chess/view/consolerender/Colors.java
Normal file
38
app/src/main/java/chess/view/consolerender/Colors.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package chess.view.consolerender;
|
||||||
|
|
||||||
|
public class Colors {
|
||||||
|
// Reset
|
||||||
|
public static final String RESET = "\u001B[0m"; // Text Reset
|
||||||
|
|
||||||
|
// Regular Colors
|
||||||
|
public static final String BLACK = "\033[38;2;0;0;0m";
|
||||||
|
public static final String WHITE = "\033[38;2;255;255;255m";
|
||||||
|
public static final String RED = "\033[38;2;255;0;0m";
|
||||||
|
public static final String GREEN = "\033[38;2;0;255;0m";
|
||||||
|
public static final String YELLOW = "\033[38;2;255;255;0m";
|
||||||
|
public static final String BLUE = "\033[38;2;0;0;255m";
|
||||||
|
public static final String PURPLE = "\033[38;2;255;0;255m";
|
||||||
|
public static final String CYAN = "\033[38;2;0;255;255m";
|
||||||
|
|
||||||
|
// Background
|
||||||
|
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
|
||||||
|
public static final String LIGHT_GRAY_BACKGROUND = "\033[48;2;180;180;180m";
|
||||||
|
public static final String DARK_GRAY_BACKGROUND = "\033[48;2;120;120;120m";
|
||||||
|
public static final String RED_BACKGROUND = "\033[41m"; // RED
|
||||||
|
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
|
||||||
|
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
|
||||||
|
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
|
||||||
|
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
|
||||||
|
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
|
||||||
|
public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE
|
||||||
|
|
||||||
|
// High Intensity backgrounds
|
||||||
|
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
|
||||||
|
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
|
||||||
|
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
|
||||||
|
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
|
||||||
|
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
|
||||||
|
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
|
||||||
|
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
|
||||||
|
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE
|
||||||
|
}
|
||||||
331
app/src/main/java/chess/view/consolerender/Console.java
Normal file
331
app/src/main/java/chess/view/consolerender/Console.java
Normal file
@@ -0,0 +1,331 @@
|
|||||||
|
package chess.view.consolerender;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.*;
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
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 java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class Console implements GameListener {
|
||||||
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
private final ConsolePieceName consolePieceName = new ConsolePieceName();
|
||||||
|
private boolean captureInput = false;
|
||||||
|
private final ExecutorService executor;
|
||||||
|
|
||||||
|
public Console(CommandExecutor commandExecutor) {
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
if (!captureInput)
|
||||||
|
return;
|
||||||
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
|
this.executor.submit(() -> {
|
||||||
|
boolean endTurn;
|
||||||
|
String line = "0";
|
||||||
|
do {
|
||||||
|
if (!line.isEmpty()) {
|
||||||
|
System.out.println("""
|
||||||
|
Pick your choice:
|
||||||
|
1 - Move
|
||||||
|
2 - Show potential moves
|
||||||
|
3 - Surrender
|
||||||
|
""");
|
||||||
|
System.out.flush();
|
||||||
|
}
|
||||||
|
line = scanner.nextLine();
|
||||||
|
endTurn = switch (line) {
|
||||||
|
case "1" -> playerPickedMove(color);
|
||||||
|
case "2" -> playerPickedShowMoves(color);
|
||||||
|
case "3" -> playerPickedSurrender(color);
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
} while (!endTurn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean playerPickedSurrender(Color color) {
|
||||||
|
sendCommand(new SurrenderCommand(color));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean playerPickedMove(Color color) {
|
||||||
|
try {
|
||||||
|
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)));
|
||||||
|
|
||||||
|
return switch (Objects.requireNonNull(result)) {
|
||||||
|
case Command.CommandResult.Moved, Command.CommandResult.ActionNeeded -> true;
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean playerPickedShowMoves(Color color) {
|
||||||
|
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 onWin(Color color) {
|
||||||
|
System.out.println(Colors.RED + "Victory of player " + color + Colors.RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
System.out.println("Pat! It's a draw!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color color) {
|
||||||
|
System.out.println("The " + color + " player has surrendered!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
System.out.println("Game start!");
|
||||||
|
onBoardUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
System.out.println("Thank you for playing!");
|
||||||
|
this.commandExecutor.close();
|
||||||
|
this.executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted.");
|
||||||
|
System.out.println("Enter 'B' to promote it into a Bishop, 'N' for a Knight, 'Q' for a Queen, 'R' for a Rook.");
|
||||||
|
System.out.flush();
|
||||||
|
this.executor.submit(() -> {
|
||||||
|
PromoteType newPiece;
|
||||||
|
boolean valid = false;
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
String promotion = scanner.next();
|
||||||
|
newPiece = switch (promotion) {
|
||||||
|
case "B", "b", "Bishop", "bishop" -> PromoteType.Bishop;
|
||||||
|
case "N", "n", "Knight", "knight" -> PromoteType.Knight;
|
||||||
|
case "Q", "q", "Queen", "queen" -> PromoteType.Queen;
|
||||||
|
case "R", "r", "Rook", "rook" -> PromoteType.Rook;
|
||||||
|
default -> throw new Exception();
|
||||||
|
};
|
||||||
|
valid = true;
|
||||||
|
sendCommand(new PromoteCommand(newPiece));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Invalid input!");
|
||||||
|
}
|
||||||
|
} while (!valid);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
if (!this.captureInput)
|
||||||
|
return;
|
||||||
|
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++) {
|
||||||
|
Piece p = pieceAt(j, i);
|
||||||
|
if ((i + j) % 2 == 0) {
|
||||||
|
string.append(Colors.LIGHT_GRAY_BACKGROUND);
|
||||||
|
} else {
|
||||||
|
string.append(Colors.DARK_GRAY_BACKGROUND);
|
||||||
|
}
|
||||||
|
if (p == null) {
|
||||||
|
string.append(" " + Colors.RESET);
|
||||||
|
} else {
|
||||||
|
string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
string.append("\n");
|
||||||
|
}
|
||||||
|
System.out.println(string);
|
||||||
|
System.out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
string.append(Colors.YELLOW_BACKGROUND);
|
||||||
|
} else {
|
||||||
|
if ((i + j) % 2 == 0) {
|
||||||
|
string.append(Colors.LIGHT_GRAY_BACKGROUND);
|
||||||
|
} else {
|
||||||
|
string.append(Colors.DARK_GRAY_BACKGROUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p == null) {
|
||||||
|
string.append(" " + Colors.RESET);
|
||||||
|
} else {
|
||||||
|
if (currentCell.equals(piece)) {
|
||||||
|
string.append(Colors.RED_BACKGROUND).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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
System.out.println("Move not allowed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
System.out.println("Repeated positions!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean onAskedCastling() {
|
||||||
|
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
|
||||||
|
sendCommand(cmd);
|
||||||
|
return switch (cmd.getCastlingResult()) {
|
||||||
|
case Small -> onSmallCastling();
|
||||||
|
case Big -> onBigCastling();
|
||||||
|
case 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCaptureInput(boolean captureInput) {
|
||||||
|
this.captureInput = captureInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package chess.view.consolerender;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public class ConsolePieceName implements PieceVisitor<String> {
|
||||||
|
|
||||||
|
public String getString(Piece piece){
|
||||||
|
if (piece.getColor()== Color.Black){
|
||||||
|
return Colors.BLACK + visit(piece);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Colors.WHITE + visit(piece);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Bishop bishop) {
|
||||||
|
return "B";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(King king) {
|
||||||
|
return "K";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Knight knight) {
|
||||||
|
return "N";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Pawn pawn) {
|
||||||
|
return "P";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Queen queen) {
|
||||||
|
return "Q";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Rook rook) {
|
||||||
|
return "R";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package chess.view.render;
|
|||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
|
|
||||||
|
import chess.model.Coordinate;
|
||||||
import chess.view.render.shader.BoardShader;
|
import chess.view.render.shader.BoardShader;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
import static org.lwjgl.opengl.GL30.*;
|
||||||
@@ -59,16 +60,42 @@ public class Renderer {
|
|||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Coordinate GetCellFromColor(Vector3f color) {
|
||||||
|
int offset = 1;
|
||||||
|
|
||||||
|
if (color.x > 0.5) {
|
||||||
|
color = new Vector3f(1.0f, 1.0f, 1.0f).sub(color);
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = (int) (color.x * 255.0f);
|
||||||
|
int g = (int) (color.y * 255.0f);
|
||||||
|
int b = (int) (color.z * 255.0f);
|
||||||
|
|
||||||
|
int index = (r + g + b) * 2 + offset;
|
||||||
|
|
||||||
|
return Coordinate.fromIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector3f GetCellColor(int x, int y) {
|
||||||
|
float index = (y * BOARD_WIDTH + x) / 2.0f;
|
||||||
|
float r = (int) (index / 3) / 255.0f;
|
||||||
|
float g = (int) ((index + 1) / 3) / 255.0f;
|
||||||
|
float b = (int) ((index + 2) / 3) / 255.0f;
|
||||||
|
if ((x + y) % 2 != 0) {
|
||||||
|
System.out.println(GetCellFromColor(new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f)));
|
||||||
|
return new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f);
|
||||||
|
} else {
|
||||||
|
System.out.println(GetCellFromColor(new Vector3f(r, g, b)));
|
||||||
|
return new Vector3f(r, g, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private float[] GetBoardColors() {
|
private float[] GetBoardColors() {
|
||||||
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
||||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
|
||||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||||
Vector3f color;
|
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||||
if ((i + j) % 2 != 0) {
|
Vector3f color = GetCellColor(i, j);
|
||||||
color = new Vector3f(1.0f, 1.0f, 1.0f);
|
|
||||||
} else {
|
|
||||||
color = new Vector3f(0.0f, 0.0f, 0.0f);
|
|
||||||
}
|
|
||||||
int squareIndex = i * BOARD_WIDTH + j;
|
int squareIndex = i * BOARD_WIDTH + j;
|
||||||
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
||||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
||||||
@@ -109,6 +136,12 @@ public class Renderer {
|
|||||||
this.vao.Unbind();
|
this.vao.Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Coordinate GetSelectedCell() {
|
||||||
|
float pixels[] = new float[3];
|
||||||
|
GL30.glReadPixels(500, 500, 1, 1, GL_RGB, GL_FLOAT, pixels);
|
||||||
|
return GetCellFromColor(new Vector3f(pixels[0], pixels[1], pixels[2]));
|
||||||
|
}
|
||||||
|
|
||||||
public void Render(Camera cam) {
|
public void Render(Camera cam) {
|
||||||
this.shader.Start();
|
this.shader.Start();
|
||||||
this.shader.SetCamMatrix(cam.getMatrix());
|
this.shader.SetCamMatrix(cam.getMatrix());
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ import org.lwjgl.glfw.*;
|
|||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
import org.lwjgl.system.*;
|
import org.lwjgl.system.*;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.Callbacks.*;
|
import static org.lwjgl.glfw.Callbacks.*;
|
||||||
@@ -13,17 +19,20 @@ import static org.lwjgl.opengl.GL11.*;
|
|||||||
import static org.lwjgl.system.MemoryStack.*;
|
import static org.lwjgl.system.MemoryStack.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
public class Window {
|
public class Window implements GameListener{
|
||||||
|
|
||||||
// The window handle
|
// The window handle
|
||||||
private long window;
|
private long window;
|
||||||
|
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
private Renderer renderer;
|
private Renderer renderer;
|
||||||
private Camera cam;
|
private Camera cam;
|
||||||
|
|
||||||
public Window() {
|
public Window(CommandExecutor commandExecutor) {
|
||||||
this.renderer = new Renderer();
|
this.renderer = new Renderer();
|
||||||
this.cam = new Camera();
|
this.cam = new Camera();
|
||||||
|
this.commandExecutor = new CommandExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -114,6 +123,8 @@ public class Window {
|
|||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
|
System.out.println(this.renderer.GetSelectedCell());
|
||||||
|
|
||||||
glfwSwapBuffers(window); // swap the color buffers
|
glfwSwapBuffers(window); // swap the color buffers
|
||||||
|
|
||||||
// Poll for window events. The key callback above will only be
|
// Poll for window events. The key callback above will only be
|
||||||
@@ -132,4 +143,82 @@ public class Window {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onBoardUpdate'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onDraw'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onGameEnd'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onGameStart'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onKingInCheck'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onKingInMat'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onMove'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onMoveNotAllowed'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPatSituation'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPlayerTurn'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPromotePawn'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color coward) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onSurrender'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color winner) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onWin'");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package chess.view.simplerender;
|
package chess.view.simplerender;
|
||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -16,24 +17,27 @@ import chess.model.pieces.Knight;
|
|||||||
import chess.model.pieces.Pawn;
|
import chess.model.pieces.Pawn;
|
||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
|
import chess.view.AssetManager;
|
||||||
|
|
||||||
public class PieceIcon implements PieceVisitor<String> {
|
public class PieceIcon implements PieceVisitor<String> {
|
||||||
|
|
||||||
private static final String basePath = "app/src/main/resources/pieces2D/";
|
private static final String basePath = "pieces2D/";
|
||||||
private static final Map<String, Icon> cache = new HashMap<>();
|
private static final Map<String, Icon> cache = new HashMap<>();
|
||||||
|
|
||||||
public Icon getIcon(Piece piece) {
|
public Icon getIcon(Piece piece) throws IOException {
|
||||||
if (piece == null)
|
if (piece == null)
|
||||||
return null;
|
return null;
|
||||||
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
||||||
return getIcon(path);
|
return getIcon(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Icon getIcon(String path) {
|
private Icon getIcon(String path) throws IOException {
|
||||||
Icon image = cache.get(path);
|
Icon image = cache.get(path);
|
||||||
if (image != null)
|
if (image != null)
|
||||||
return image;
|
return image;
|
||||||
image = new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
|
||||||
|
image = new ImageIcon(new ImageIcon(AssetManager.getResource(path).readAllBytes()).getImage()
|
||||||
|
.getScaledInstance(100, 100, Image.SCALE_SMOOTH));
|
||||||
cache.put(path, image);
|
cache.put(path, image);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package chess.view.simplerender;
|
package chess.view.simplerender;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
@@ -13,34 +18,51 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.CommandExecutor;
|
|
||||||
import chess.controller.OutputSystem;
|
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.Command.CommandResult;
|
||||||
import chess.controller.commands.GetAllowedMovesCommand;
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.CastlingCommand;
|
||||||
|
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||||
|
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.MoveCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class Window extends JFrame implements OutputSystem {
|
public class Window extends JFrame implements GameListener {
|
||||||
|
|
||||||
private final JLabel cells[][];
|
|
||||||
private final JLabel displayText;
|
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
private Coordinate lastClick = null;
|
private Coordinate lastClick = null;
|
||||||
|
|
||||||
public Window(CommandExecutor commandExecutor) {
|
private final JLabel cells[][];
|
||||||
|
private final JLabel displayText;
|
||||||
|
private final JButton castlingButton = new JButton("Roque");
|
||||||
|
private final JButton bigCastlingButton = new JButton("Grand Roque");
|
||||||
|
private final JButton undoButton = new JButton("Annuler le coup précédent");
|
||||||
|
|
||||||
|
private final boolean showPopups;
|
||||||
|
|
||||||
|
public Window(final CommandExecutor commandExecutor, boolean showPopups) {
|
||||||
this.cells = new JLabel[8][8];
|
this.cells = new JLabel[8][8];
|
||||||
this.displayText = new JLabel();
|
this.displayText = new JLabel();
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
setSize(800, 870);
|
this.showPopups = showPopups;
|
||||||
|
setSize(800, 910);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||||
|
addWindowListener(new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowClosed(WindowEvent e) {
|
||||||
|
commandExecutor.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private CommandResult sendCommand(Command command) {
|
private CommandResult sendCommand(Command command) {
|
||||||
@@ -48,15 +70,37 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Color getCellColor(int x, int y) {
|
private Color getCellColor(int x, int y) {
|
||||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
return ((x + y) % 2 == 1) ? Color.DARK_GRAY : Color.LIGHT_GRAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildButtons(JPanel bottom) {
|
||||||
|
castlingButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new CastlingCommand(false));
|
||||||
|
});
|
||||||
|
|
||||||
|
bigCastlingButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new CastlingCommand(true));
|
||||||
|
});
|
||||||
|
|
||||||
|
undoButton.addActionListener((event) -> {
|
||||||
|
sendCommand(new UndoCommand());
|
||||||
|
});
|
||||||
|
|
||||||
|
bottom.add(castlingButton);
|
||||||
|
bottom.add(bigCastlingButton);
|
||||||
|
bottom.add(undoButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildBoard() {
|
private void buildBoard() {
|
||||||
JPanel content = new JPanel();
|
JPanel content = new JPanel();
|
||||||
JPanel grid = new JPanel(new GridLayout(8, 8));
|
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||||
|
JPanel bottom = new JPanel();
|
||||||
|
|
||||||
|
buildButtons(bottom);
|
||||||
|
|
||||||
content.add(this.displayText);
|
content.add(this.displayText);
|
||||||
content.add(grid);
|
content.add(grid);
|
||||||
|
content.add(bottom);
|
||||||
|
|
||||||
setContentPane(content);
|
setContentPane(content);
|
||||||
|
|
||||||
@@ -98,13 +142,17 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
for (int y = 0; y < 8; y++) {
|
for (int y = 0; y < 8; y++) {
|
||||||
for (int x = 0; x < 8; x++) {
|
for (int x = 0; x < 8; x++) {
|
||||||
JLabel cell = this.cells[x][y];
|
JLabel cell = this.cells[x][y];
|
||||||
|
try {
|
||||||
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean previewMoves(int x, int y) {
|
private boolean previewMoves(int x, int y) {
|
||||||
GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(new Coordinate(x, y));
|
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(new Coordinate(x, y));
|
||||||
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -114,7 +162,9 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
|
|
||||||
for (Coordinate destCoord : allowedMoves) {
|
for (Coordinate destCoord : allowedMoves) {
|
||||||
JLabel cell = this.cells[destCoord.getX()][destCoord.getY()];
|
JLabel cell = this.cells[destCoord.getX()][destCoord.getY()];
|
||||||
cell.setBackground(Color.CYAN);
|
Graphics g = cell.getGraphics();
|
||||||
|
g.setColor(new Color(128, 128, 128, 128));
|
||||||
|
g.fillOval(25, 25, 50, 50);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -131,6 +181,7 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
for (int x = 0; x < 8; x++) {
|
for (int x = 0; x < 8; x++) {
|
||||||
JLabel cell = this.cells[x][y];
|
JLabel cell = this.cells[x][y];
|
||||||
cell.setBackground(getCellColor(x, y));
|
cell.setBackground(getCellColor(x, y));
|
||||||
|
cell.paint(cell.getGraphics());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,61 +198,73 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
}
|
}
|
||||||
if (!this.lastClick.equals(new Coordinate(x, y))) {
|
if (!this.lastClick.equals(new Coordinate(x, y))) {
|
||||||
Move move = new Move(lastClick, new Coordinate(x, y));
|
Move move = new Move(lastClick, new Coordinate(x, y));
|
||||||
if (sendCommand(new MoveCommand(move)) == CommandResult.NotAllowed) {
|
sendCommand(new MoveCommand(move));
|
||||||
drawInvalid(move);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.lastClick = null;
|
this.lastClick = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void updateButtons() {
|
||||||
public void playerTurn(chess.model.Color color) {
|
GetAllowedCastlingsCommand cmd = new GetAllowedCastlingsCommand();
|
||||||
this.displayText.setText("C'est au tour de " + color);
|
sendCommand(cmd);
|
||||||
|
this.castlingButton.setEnabled(
|
||||||
|
cmd.getCastlingResult() == CastlingResult.Small || cmd.getCastlingResult() == CastlingResult.Both);
|
||||||
|
this.bigCastlingButton.setEnabled(
|
||||||
|
cmd.getCastlingResult() == CastlingResult.Big || cmd.getCastlingResult() == CastlingResult.Both);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void winnerIs(chess.model.Color color) {
|
public void onPlayerTurn(chess.model.Color color) {
|
||||||
|
this.displayText.setText("Current turn: " + color);
|
||||||
|
updateButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(chess.model.Color color) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Victory of " + color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
if (!showPopups)
|
||||||
|
return;
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
JOptionPane.showMessageDialog(this, "Victoire de " + color);
|
JOptionPane.showMessageDialog(this, "Check!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, "Checkmate!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(chess.model.Color color) {
|
||||||
|
JOptionPane.showMessageDialog(this, color + " has surrendered.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
JOptionPane.showMessageDialog(this, "End of the game");
|
||||||
this.dispose();
|
this.dispose();
|
||||||
});
|
this.commandExecutor.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInCheck() {
|
public void onGameStart() {
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(this, "Échec !");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void kingIsInMat() {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(this, "Échec et mat !");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void patSituation() {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(this, "Pat. Égalité !");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void hasSurrendered(chess.model.Color color) {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(this, "Abandon de " + color);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void gameStarted() {
|
|
||||||
buildBoard();
|
buildBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
if (!showPopups)
|
||||||
|
return;
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
String result = null;
|
String result = null;
|
||||||
|
|
||||||
@@ -238,8 +301,21 @@ public class Window extends JFrame implements OutputSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateDisplay() {
|
public void onBoardUpdate() {
|
||||||
updateBoard();
|
updateBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
drawInvalid(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
JOptionPane.showMessageDialog(this, "Same position was repeated three times. It's a draw!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
app/src/main/resources/games/akopian.pgn
Normal file
15
app/src/main/resources/games/akopian.pgn
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[Event "URS-chT"]
|
||||||
|
[Site "Moscow"]
|
||||||
|
[Date "1963.??.??"]
|
||||||
|
[Round "?"]
|
||||||
|
[White "Listergarten, Leonid B"]
|
||||||
|
[Black "Akopian, Vladimir"]
|
||||||
|
[Result "1-0"]
|
||||||
|
[WhiteElo ""]
|
||||||
|
[BlackElo ""]
|
||||||
|
[ECO "B48"]
|
||||||
|
|
||||||
|
1.e4 c5 2.Nf3 e6 3.d4 cxd4 4.Nxd4 a6 5.Nc3 Qc7 6.Bd3 Nc6 7.Be3 b5 8.a3 Bb7
|
||||||
|
9.O-O Rc8 10.Nxc6 Qxc6 11.Qg4 Nf6 12.Qg3 h5 13.e5 Nd5 14.Ne4 h4 15.Qh3 Qc7
|
||||||
|
16.f4 Nxe3 17.Qxe3 h3 18.gxh3 f5 19.exf6 d5 20.Nf2 Kf7 21.Rae1 Re8 22.Qg3 g5
|
||||||
|
23.fxg5 Qxg3+ 24.hxg3 e5 25.g6+ Kxf6 26.Ng4+ Kg5 27.Rf5+ 1-0
|
||||||
@@ -3,12 +3,57 @@
|
|||||||
*/
|
*/
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import chess.ai.DumbAI;
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.*;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
import chess.simulator.Simulator;
|
||||||
|
import chess.view.simplerender.Window;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
class AppTest {
|
class AppTest {
|
||||||
@Test void appHasAGreeting() {
|
@Test void functionalRollback(){
|
||||||
// App classUnderTest = new App();
|
Game game = new Game(new ChessBoard());
|
||||||
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
|
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
|
commandExecutor.addListener(new GameAdaptator() {
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
|
||||||
|
Command.CommandResult result;
|
||||||
|
do {
|
||||||
|
result = commandExecutor.executeCommand(new UndoCommand());
|
||||||
|
} while (result != Command.CommandResult.NotAllowed);
|
||||||
|
|
||||||
|
Game initialGame = new Game(new ChessBoard());
|
||||||
|
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
assert(game.getBoard().equals(initialGame.getBoard()));
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void functionalRollbacks(){
|
||||||
|
for (int i=0; i<100; i++) {
|
||||||
|
functionalRollback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user