Compare commits
46 Commits
decorateur
...
8190090adc
| Author | SHA1 | Date | |
|---|---|---|---|
| 8190090adc | |||
| 0d3d77781f | |||
|
|
98b28c5fba | ||
| 17d8333342 | |||
| 3226e72d32 | |||
|
|
0fbea2ca0f | ||
|
|
746fa4d330 | ||
| 7aa129bbc6 | |||
|
|
19b45371bb | ||
| 4b84a30e07 | |||
| e8e79a1e9e | |||
| 6584c5cb91 | |||
| 58cc9f3f17 | |||
| 6eae7e386f | |||
| 6cb1dd826f | |||
| a2224cf618 | |||
| 65362677a5 | |||
| 381e5ed0b8 | |||
| d94f7d733b | |||
| 2ec7be27ca | |||
| 8c2c6946d7 | |||
| 9af06e36f8 | |||
| a0af8caf57 | |||
| 63a1e261e8 | |||
| 7b07423175 | |||
| 416cfadc9b | |||
| 810934aea1 | |||
| 48a215eae5 | |||
| 55ef180f57 | |||
| 5b006034ad | |||
| 873ffc05d3 | |||
| 55774b4605 | |||
| 0d72e015f1 | |||
| 927ba129f6 | |||
| 36e04376c3 | |||
| a81da804f0 | |||
| 9179b3cda9 | |||
| 97cafb903a | |||
| 1b9ff5bdd1 | |||
| 2c6b64fa7d | |||
| 5598d4f5eb | |||
| dcdf118274 | |||
| 0bef89c46f | |||
| dc2ea660ff | |||
| b98e1aaade | |||
| 0c35c38ccd |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,4 +4,6 @@
|
|||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
|
|
||||||
app/bin
|
app/bin
|
||||||
|
|
||||||
|
.vscode
|
||||||
@@ -17,7 +17,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def lwjgl_version = "3.3.6"
|
def lwjgl_version = "3.3.6"
|
||||||
def lwjgl_natives = "natives-windows"
|
def lwjgl_natives = "natives-linux"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
@@ -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,16 +0,0 @@
|
|||||||
/*
|
|
||||||
* This Java source file was generated by the Gradle 'init' task.
|
|
||||||
*/
|
|
||||||
package chess;
|
|
||||||
|
|
||||||
import chess.view.render2D.Window;
|
|
||||||
|
|
||||||
public class App {
|
|
||||||
public String getGreeting() {
|
|
||||||
return "Hello World!";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Window.main(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
24
app/src/main/java/chess/ConsoleMain.java
Normal file
24
app/src/main/java/chess/ConsoleMain.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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.view.consolerender.Console;
|
||||||
|
|
||||||
|
public class ConsoleMain {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor();
|
||||||
|
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
Console console = new Console(commandExecutor);
|
||||||
|
|
||||||
|
commandExecutor.setGame(game);
|
||||||
|
commandExecutor.addListener(console);
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/src/main/java/chess/SwingMain.java
Normal file
21
app/src/main/java/chess/SwingMain.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package chess;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.view.simplerender.Window;
|
||||||
|
|
||||||
|
public class SwingMain {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor();
|
||||||
|
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
Window window = new Window(commandExecutor);
|
||||||
|
|
||||||
|
commandExecutor.setGame(game);
|
||||||
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
}
|
||||||
|
}
|
||||||
22
app/src/main/java/chess/controller/Command.java
Normal file
22
app/src/main/java/chess/controller/Command.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package chess.controller;
|
||||||
|
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Game;
|
||||||
|
|
||||||
|
public abstract class Command {
|
||||||
|
|
||||||
|
public enum CommandResult {
|
||||||
|
/** The command was successfull. Should update display and switch player turn. */
|
||||||
|
Moved,
|
||||||
|
/** The command was successfull. Should not update anything */
|
||||||
|
NotMoved,
|
||||||
|
/** The command was successfull. Should only update display */
|
||||||
|
ActionNeeded,
|
||||||
|
/** The command was not successfull */
|
||||||
|
NotAllowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract CommandResult execute(Game game, GameListener outputSystem);
|
||||||
|
|
||||||
|
public void postExec(Game game, GameListener outputSystem) {}
|
||||||
|
}
|
||||||
97
app/src/main/java/chess/controller/CommandExecutor.java
Normal file
97
app/src/main/java/chess/controller/CommandExecutor.java
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package chess.controller;
|
||||||
|
|
||||||
|
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.GameStatus;
|
||||||
|
|
||||||
|
public class CommandExecutor {
|
||||||
|
|
||||||
|
private Game game;
|
||||||
|
private final GameDispatcher dispatcher;
|
||||||
|
|
||||||
|
public CommandExecutor() {
|
||||||
|
this.game = null;
|
||||||
|
this.dispatcher = new GameDispatcher();
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
assert result != CommandResult.Moved || command instanceof PlayerCommand || command instanceof UndoCommand;
|
||||||
|
|
||||||
|
processResult(command, result);
|
||||||
|
|
||||||
|
if (command instanceof PlayerCommand playerCommand && result != CommandResult.NotAllowed)
|
||||||
|
this.game.addAction(playerCommand);
|
||||||
|
|
||||||
|
command.postExec(game, dispatcher);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processResult(Command command, CommandResult result) {
|
||||||
|
switch (result) {
|
||||||
|
case NotAllowed:
|
||||||
|
case NotMoved:
|
||||||
|
return;
|
||||||
|
|
||||||
|
case ActionNeeded:
|
||||||
|
this.dispatcher.updateDisplay();
|
||||||
|
return;
|
||||||
|
|
||||||
|
case Moved:
|
||||||
|
if (checkGameStatus())
|
||||||
|
return;
|
||||||
|
switchPlayerTurn();
|
||||||
|
this.dispatcher.updateDisplay();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void switchPlayerTurn() {
|
||||||
|
this.game.switchPlayerTurn();
|
||||||
|
this.dispatcher.playerTurn(this.game.getPlayerTurn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return True if the game is over
|
||||||
|
*/
|
||||||
|
private boolean checkGameStatus() {
|
||||||
|
GameStatus gameStatus = this.game.checkGameStatus();
|
||||||
|
|
||||||
|
switch (gameStatus) {
|
||||||
|
case Check:
|
||||||
|
this.dispatcher.kingIsInCheck();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case CheckMate:
|
||||||
|
this.dispatcher.kingIsInMat();
|
||||||
|
this.dispatcher.winnerIs(this.game.getPlayerTurn());
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case OnGoing:
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case Pat:
|
||||||
|
this.dispatcher.patSituation();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(GameListener listener) {
|
||||||
|
this.dispatcher.addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGame(Game game) {
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package chess.controller;
|
|
||||||
|
|
||||||
import chess.model.Coordinates;
|
|
||||||
import chess.model.Game;
|
|
||||||
import chess.model.Move;
|
|
||||||
import chess.view.render2D.Window;
|
|
||||||
|
|
||||||
public class Controller {
|
|
||||||
private final Game game;
|
|
||||||
private final Window view;
|
|
||||||
|
|
||||||
public Controller(Game game, Window view) {
|
|
||||||
this.game = game;
|
|
||||||
this.view = view;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param from Coordinates: old coordinates
|
|
||||||
* @param to Coordinates: new coordinates
|
|
||||||
*/
|
|
||||||
public void sendMove(Coordinates from, Coordinates to) {
|
|
||||||
System.out.println("New move: " + from + " to " + to);
|
|
||||||
game.setMove(new Move(from, to));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
15
app/src/main/java/chess/controller/PlayerCommand.java
Normal file
15
app/src/main/java/chess/controller/PlayerCommand.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.controller;
|
||||||
|
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Game;
|
||||||
|
|
||||||
|
public abstract class PlayerCommand extends Command{
|
||||||
|
|
||||||
|
public CommandResult undo(Game game, GameListener outputSystem) {
|
||||||
|
CommandResult result = undoImpl(game, outputSystem);
|
||||||
|
game.updateLastMove();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract CommandResult undoImpl(Game game, GameListener outputSystem);
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
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.Move;
|
||||||
|
|
||||||
|
public class CastlingCommand extends PlayerCommand {
|
||||||
|
|
||||||
|
private Move kingMove;
|
||||||
|
private Move rookMove;
|
||||||
|
private final boolean bigCastling;
|
||||||
|
|
||||||
|
public CastlingCommand(boolean bigCastling) {
|
||||||
|
this.bigCastling = bigCastling;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
// we must promote the pending pawn before
|
||||||
|
if (board.pawnShouldBePromoted())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
if (bigCastling && !board.canBigCastle(game.getPlayerTurn()))
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
|
game.getBoard().undoMove(this.kingMove, null);
|
||||||
|
game.getBoard().undoMove(this.rookMove, null);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
public class GetAllowedMovesPieceCommand extends Command {
|
||||||
|
|
||||||
|
private final Coordinate start;
|
||||||
|
private List<Coordinate> destinations;
|
||||||
|
|
||||||
|
public GetAllowedMovesPieceCommand(Coordinate start) {
|
||||||
|
this.start = start;
|
||||||
|
this.destinations = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
Piece piece = board.pieceAt(start);
|
||||||
|
|
||||||
|
if (piece == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
if (piece.getColor() != game.getPlayerTurn())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
this.destinations = board.getAllowedMoves(start);
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Coordinate> getDestinations() {
|
||||||
|
return destinations;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
public class GetPieceAtCommand extends Command{
|
||||||
|
|
||||||
|
private final Coordinate pieceCoords;
|
||||||
|
private Piece piece;
|
||||||
|
|
||||||
|
public GetPieceAtCommand(Coordinate pieceCoords) {
|
||||||
|
this.pieceCoords = pieceCoords;
|
||||||
|
this.piece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
if (!pieceCoords.isValid())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
this.piece = game.getBoard().pieceAt(pieceCoords);
|
||||||
|
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece getPiece() {
|
||||||
|
return piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
82
app/src/main/java/chess/controller/commands/MoveCommand.java
Normal file
82
app/src/main/java/chess/controller/commands/MoveCommand.java
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.visitor.PiecePathChecker;
|
||||||
|
|
||||||
|
public class MoveCommand extends PlayerCommand {
|
||||||
|
|
||||||
|
private final Move move;
|
||||||
|
private Piece deadPiece;
|
||||||
|
|
||||||
|
public MoveCommand(Move move) {
|
||||||
|
this.move = move;
|
||||||
|
this.deadPiece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Move getMove() {
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
// we must promote the pending pawn before
|
||||||
|
if (board.pawnShouldBePromoted())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
Piece piece = board.pieceAt(move.getStart());
|
||||||
|
if (piece == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
if (piece.getColor() != game.getPlayerTurn())
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
boolean valid = new PiecePathChecker(board, move).isValid();
|
||||||
|
if (!valid)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
this.deadPiece = board.pieceAt(move.getDeadPieceCoords());
|
||||||
|
board.applyMove(move);
|
||||||
|
|
||||||
|
if (board.isKingInCheck(game.getPlayerTurn())) {
|
||||||
|
board.undoLastMove();
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (board.pawnShouldBePromoted())
|
||||||
|
return CommandResult.ActionNeeded;
|
||||||
|
|
||||||
|
board.setLastMove(this.move);
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
board.undoMove(move, deadPiece);
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postExec(Game game, GameListener outputSystem) {
|
||||||
|
tryPromote(game, outputSystem);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryPromote(Game game, GameListener outputSystem) {
|
||||||
|
Coordinate pawnPos = game.getBoard().pawnPromotePosition();
|
||||||
|
|
||||||
|
if (pawnPos == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
outputSystem.promotePawn(pawnPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Game;
|
||||||
|
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 NewGameCommand extends Command {
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
board.clearBoard();
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
board.pieceComes(new Pawn(Color.Black), new Coordinate(i, 1));
|
||||||
|
board.pieceComes(new Pawn(Color.White), new Coordinate(i, Coordinate.VALUE_MAX - 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
board.pieceComes(new Rook(Color.Black), new Coordinate(0, 0));
|
||||||
|
board.pieceComes(new Rook(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 1, 0));
|
||||||
|
|
||||||
|
board.pieceComes(new Rook(Color.White), new Coordinate(0, Coordinate.VALUE_MAX - 1));
|
||||||
|
board.pieceComes(new Rook(Color.White), new Coordinate(Coordinate.VALUE_MAX - 1, Coordinate.VALUE_MAX - 1));
|
||||||
|
|
||||||
|
board.pieceComes(new Knight(Color.Black), new Coordinate(1, 0));
|
||||||
|
board.pieceComes(new Knight(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 2, 0));
|
||||||
|
|
||||||
|
board.pieceComes(new Knight(Color.White), new Coordinate(1, Coordinate.VALUE_MAX - 1));
|
||||||
|
board.pieceComes(new Knight(Color.White), new Coordinate(Coordinate.VALUE_MAX - 2, Coordinate.VALUE_MAX - 1));
|
||||||
|
|
||||||
|
board.pieceComes(new Bishop(Color.Black), new Coordinate(2, 0));
|
||||||
|
board.pieceComes(new Bishop(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 3, 0));
|
||||||
|
|
||||||
|
board.pieceComes(new Bishop(Color.White), new Coordinate(2, Coordinate.VALUE_MAX - 1));
|
||||||
|
board.pieceComes(new Bishop(Color.White), new Coordinate(Coordinate.VALUE_MAX - 3, Coordinate.VALUE_MAX - 1));
|
||||||
|
|
||||||
|
board.pieceComes(new Queen(Color.Black), new Coordinate(3, 0));
|
||||||
|
board.pieceComes(new King(Color.Black), new Coordinate(4, 0));
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
game.resetPlayerTurn();
|
||||||
|
|
||||||
|
outputSystem.gameStarted();
|
||||||
|
outputSystem.playerTurn(game.getPlayerTurn());
|
||||||
|
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
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.Piece;
|
||||||
|
import chess.model.pieces.Bishop;
|
||||||
|
import chess.model.pieces.Knight;
|
||||||
|
import chess.model.pieces.Queen;
|
||||||
|
import chess.model.pieces.Rook;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
|
||||||
|
public class PromoteCommand extends PlayerCommand {
|
||||||
|
|
||||||
|
public enum PromoteType {
|
||||||
|
Queen,
|
||||||
|
Rook,
|
||||||
|
Bishop,
|
||||||
|
Knight
|
||||||
|
}
|
||||||
|
|
||||||
|
private final PromoteType promoteType;
|
||||||
|
private Coordinate pieceCoords;
|
||||||
|
private Piece oldPawn;
|
||||||
|
|
||||||
|
public PromoteCommand(PromoteType promoteType) {
|
||||||
|
this.promoteType = promoteType;
|
||||||
|
this.pieceCoords = null;
|
||||||
|
this.oldPawn = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
this.pieceCoords = board.pawnPromotePosition();
|
||||||
|
|
||||||
|
if (this.pieceCoords == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||||
|
if (!new PawnIdentifier(game.getPlayerTurn()).isPawn(pawn))
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
int destY = this.pieceCoords.getY();
|
||||||
|
|
||||||
|
int enemyLine = pawn.getColor() == Color.White ? 0 : 7;
|
||||||
|
|
||||||
|
if (destY != enemyLine)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
this.oldPawn = pawn;
|
||||||
|
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece createPiece(PromoteType promoteType, Color color) {
|
||||||
|
switch (promoteType) {
|
||||||
|
case Queen:
|
||||||
|
return new Queen(color);
|
||||||
|
|
||||||
|
case Bishop:
|
||||||
|
return new Bishop(color);
|
||||||
|
|
||||||
|
case Knight:
|
||||||
|
return new Knight(color);
|
||||||
|
|
||||||
|
case Rook:
|
||||||
|
return new Rook(color);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CommandResult undoImpl(Game game, GameListener outputSystem) {
|
||||||
|
final ChessBoard board = game.getBoard();
|
||||||
|
|
||||||
|
Piece promoted = board.pieceAt(this.pieceCoords);
|
||||||
|
|
||||||
|
assert promoted != null;
|
||||||
|
|
||||||
|
board.pieceComes(this.oldPawn, this.pieceCoords);
|
||||||
|
|
||||||
|
game.getLastAction().undo(game, outputSystem);
|
||||||
|
|
||||||
|
return CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Game;
|
||||||
|
|
||||||
|
public class SurrenderCommand extends Command {
|
||||||
|
|
||||||
|
private final Color player;
|
||||||
|
|
||||||
|
public SurrenderCommand(Color player) {
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
outputSystem.hasSurrendered(player);
|
||||||
|
outputSystem.winnerIs(Color.getEnemy(player));
|
||||||
|
return CommandResult.NotMoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
app/src/main/java/chess/controller/commands/UndoCommand.java
Normal file
19
app/src/main/java/chess/controller/commands/UndoCommand.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package chess.controller.commands;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.PlayerCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Game;
|
||||||
|
|
||||||
|
public class UndoCommand extends Command{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(Game game, GameListener outputSystem) {
|
||||||
|
PlayerCommand lastAction = game.getLastAction();
|
||||||
|
if (lastAction == null)
|
||||||
|
return CommandResult.NotAllowed;
|
||||||
|
|
||||||
|
return lastAction.undo(game, outputSystem);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
app/src/main/java/chess/controller/event/GameAdaptator.java
Normal file
35
app/src/main/java/chess/controller/event/GameAdaptator.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
|
||||||
|
public class GameAdaptator implements GameListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerTurn(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void winnerIs(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInCheck() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInMat() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void patSituation() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hasSurrendered(Color color) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void gameStarted() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void promotePawn(Coordinate pieceCoords) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDisplay() {}
|
||||||
|
|
||||||
|
}
|
||||||
68
app/src/main/java/chess/controller/event/GameDispatcher.java
Normal file
68
app/src/main/java/chess/controller/event/GameDispatcher.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
|
||||||
|
public class GameDispatcher implements GameListener{
|
||||||
|
|
||||||
|
private final List<GameListener> listeners;
|
||||||
|
|
||||||
|
public GameDispatcher() {
|
||||||
|
this.listeners = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(GameListener listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerTurn(Color color) {
|
||||||
|
this.listeners.forEach((l) -> l.playerTurn(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void winnerIs(Color color) {
|
||||||
|
this.listeners.forEach((l) -> l.winnerIs(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInCheck() {
|
||||||
|
this.listeners.forEach((l) -> l.kingIsInCheck());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInMat() {
|
||||||
|
this.listeners.forEach((l) -> l.kingIsInMat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void patSituation() {
|
||||||
|
this.listeners.forEach((l) -> l.patSituation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hasSurrendered(Color color) {
|
||||||
|
this.listeners.forEach((l) -> l.hasSurrendered(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void gameStarted() {
|
||||||
|
this.listeners.forEach((l) -> l.gameStarted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
|
this.listeners.forEach((l) -> l.promotePawn(pieceCoords));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDisplay() {
|
||||||
|
this.listeners.forEach((l) -> l.updateDisplay());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
25
app/src/main/java/chess/controller/event/GameListener.java
Normal file
25
app/src/main/java/chess/controller/event/GameListener.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package chess.controller.event;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
|
||||||
|
public interface GameListener {
|
||||||
|
|
||||||
|
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,17 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public abstract class AccessibleCellsDecorator {
|
|
||||||
public AccessibleCellsDecorator base;
|
|
||||||
public ArrayList<Cell> cells;
|
|
||||||
protected abstract ArrayList<Cell> getAccessibleCells();
|
|
||||||
public AccessibleCellsDecorator(AccessibleCellsDecorator base) {
|
|
||||||
this.base = base;
|
|
||||||
}
|
|
||||||
public AccessibleCellsDecorator() {
|
|
||||||
this(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
import chess.model.pieces.*;
|
|
||||||
|
|
||||||
public class Board {
|
|
||||||
private final Cell[][] cells;
|
|
||||||
private static final int WHITE = 0;
|
|
||||||
private static final int BLACK = 1;
|
|
||||||
|
|
||||||
public Board(int size) {
|
|
||||||
cells = new Cell[size][size];
|
|
||||||
for (int y = 0; y < size; y++) {
|
|
||||||
for (int x = 0; x < size; x++) {
|
|
||||||
cells[y][x] = new Cell();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setPieces();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board() {
|
|
||||||
this(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Cell getCell(Coordinates coordinates) {
|
|
||||||
return cells[coordinates.getX()][coordinates.getY()];
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() {
|
|
||||||
return cells.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPieces() throws Error {
|
|
||||||
if (this.getSize() == 8) {
|
|
||||||
setPiecesSize8();
|
|
||||||
} else {
|
|
||||||
throw new Error("Oops. This isn't implemented yet.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiecesSize8() {
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
cells[i][1].setPiece(new Pawn(BLACK, this, cells[i][1]));
|
|
||||||
cells[i][6].setPiece(new Pawn(WHITE, this, cells[i][6]));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i <= 7; i += 7) {
|
|
||||||
cells[i][0].setPiece(new Rook(BLACK, this, cells[i][0]));
|
|
||||||
cells[i][7].setPiece(new Rook(WHITE, this, cells[i][7]));
|
|
||||||
}
|
|
||||||
for (int i = 1; i <= 6; i += 5) {
|
|
||||||
cells[i][0].setPiece(new Knight(BLACK, this, cells[i][0]));
|
|
||||||
cells[i][7].setPiece(new Knight(WHITE, this, cells[i][7]));
|
|
||||||
}
|
|
||||||
for (int i = 2; i <= 5; i += 3) {
|
|
||||||
cells[i][0].setPiece(new Bishop(BLACK, this, cells[i][0]));
|
|
||||||
cells[i][7].setPiece(new Bishop(WHITE, this, cells[i][7]));
|
|
||||||
}
|
|
||||||
cells[3][0].setPiece(new Queen(BLACK, this, cells[3][0]));
|
|
||||||
cells[3][7].setPiece(new Queen(WHITE, this, cells[3][7]));
|
|
||||||
cells[4][0].setPiece(new King(BLACK, this, cells[4][0]));
|
|
||||||
cells[4][7].setPiece(new King(WHITE, this, cells[4][7]));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Piece getPiece(Coordinates coordinates) {
|
|
||||||
return getCell(coordinates).getPiece();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void movePiece(Move move) throws Error {
|
|
||||||
Piece movingPiece = getPiece(move.getStart());
|
|
||||||
Cell arrivalCell = getCell(move.getEnd());
|
|
||||||
getCell(move.getStart()).deletePiece();
|
|
||||||
arrivalCell.setPiece(movingPiece);
|
|
||||||
movingPiece.setPosition(arrivalCell);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Cell getRelativePosition(Coordinates coordinates) {
|
|
||||||
// todo
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
public class Cell {
|
|
||||||
private Piece piece = null;
|
|
||||||
|
|
||||||
public Cell(Piece piece) {
|
|
||||||
this.piece = piece;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Cell(){
|
|
||||||
this.piece = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiece(Piece piece) {
|
|
||||||
this.piece = piece;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deletePiece() {
|
|
||||||
this.piece = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Piece getPiece() {
|
|
||||||
return piece;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
300
app/src/main/java/chess/model/ChessBoard.java
Normal file
300
app/src/main/java/chess/model/ChessBoard.java
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.model.visitor.KingIdentifier;
|
||||||
|
import chess.model.visitor.PawnIdentifier;
|
||||||
|
import chess.model.visitor.PiecePathChecker;
|
||||||
|
|
||||||
|
public class ChessBoard {
|
||||||
|
public static class Cell {
|
||||||
|
private Piece piece;
|
||||||
|
|
||||||
|
public Cell() {
|
||||||
|
this.piece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece getPiece() {
|
||||||
|
return piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPiece(Piece piece) {
|
||||||
|
this.piece = piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Cell[][] cells;
|
||||||
|
private Move lastVirtualMove;
|
||||||
|
private Move lastMove;
|
||||||
|
private Piece lastEjectedPiece;
|
||||||
|
|
||||||
|
public ChessBoard() {
|
||||||
|
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
this.cells[i][j] = new Cell();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.lastVirtualMove = null;
|
||||||
|
this.lastMove = null;
|
||||||
|
this.lastEjectedPiece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyMove(Move move) {
|
||||||
|
assert move.isValid() : "Invalid move !";
|
||||||
|
Piece deadPiece = pieceAt(move.getDeadPieceCoords());
|
||||||
|
if (deadPiece != null) {
|
||||||
|
this.lastEjectedPiece = deadPiece;
|
||||||
|
} else {
|
||||||
|
this.lastEjectedPiece = null;
|
||||||
|
}
|
||||||
|
Piece movingPiece = pieceAt(move.getStart());
|
||||||
|
pieceLeaves(move.getDeadPieceCoords());
|
||||||
|
pieceLeaves(move.getStart());
|
||||||
|
pieceComes(movingPiece, move.getFinish());
|
||||||
|
movingPiece.move();
|
||||||
|
this.lastVirtualMove = move;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undoLastMove() {
|
||||||
|
assert this.lastVirtualMove != null : "Can't undo at the beginning!";
|
||||||
|
|
||||||
|
undoMove(this.lastVirtualMove, this.lastEjectedPiece);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undoMove(Move move, Piece deadPiece) {
|
||||||
|
Piece movingPiece = pieceAt(move.getFinish());
|
||||||
|
pieceComes(movingPiece, move.getStart());
|
||||||
|
pieceLeaves(move.getFinish());
|
||||||
|
pieceComes(deadPiece, move.getDeadPieceCoords());
|
||||||
|
movingPiece.unMove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCellEmpty(Coordinate coordinate) {
|
||||||
|
return pieceAt(coordinate) == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece pieceAt(Coordinate coordinate) {
|
||||||
|
if (!coordinate.isValid())
|
||||||
|
return null;
|
||||||
|
return cellAt(coordinate).getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearBoard() {
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
pieceLeaves(new Coordinate(i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Cell cellAt(Coordinate coordinate) {
|
||||||
|
return this.cells[coordinate.getX()][coordinate.getY()];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pieceComes(Piece piece, Coordinate coordinate) {
|
||||||
|
cellAt(coordinate).setPiece(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pieceLeaves(Coordinate coordinate) {
|
||||||
|
cellAt(coordinate).setPiece(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coordinate findKing(Color color) {
|
||||||
|
KingIdentifier kingIdentifier = new KingIdentifier(color);
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
Coordinate coordinate = new Coordinate(i, j);
|
||||||
|
Piece piece = pieceAt(coordinate);
|
||||||
|
if (kingIdentifier.isKing(piece)) {
|
||||||
|
return coordinate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert false : "No king found ?!";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isKingInCheck(Color color) {
|
||||||
|
Coordinate kingPos = findKing(color);
|
||||||
|
assert kingPos.isValid() : "King position is invalid!";
|
||||||
|
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
Coordinate attackCoords = new Coordinate(i, j);
|
||||||
|
Piece attackPiece = pieceAt(attackCoords);
|
||||||
|
if (attackPiece == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
PiecePathChecker checker = new PiecePathChecker(this, new Move(attackCoords, kingPos));
|
||||||
|
if (checker.isValid())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAllowedMoves(Color player) {
|
||||||
|
// for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
// for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
// Coordinate attackCoords = new Coordinate(i, j);
|
||||||
|
// Piece attackPiece = pieceAt(attackCoords);
|
||||||
|
// if (attackPiece == null)
|
||||||
|
// continue;
|
||||||
|
|
||||||
|
// if (attackPiece.getColor() != player)
|
||||||
|
// continue;
|
||||||
|
|
||||||
|
// if (!getAllowedMoves(attackCoords).isEmpty())
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
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 j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
Coordinate destination = new Coordinate(i, j);
|
||||||
|
Move move = new Move(start, destination);
|
||||||
|
|
||||||
|
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
||||||
|
move);
|
||||||
|
if (!piecePathChecker.isValid())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
applyMove(move);
|
||||||
|
if (!isKingInCheck(player))
|
||||||
|
result.add(move);
|
||||||
|
undoLastMove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Coordinate> getAllowedMoves(Coordinate pieceCoords) {
|
||||||
|
Piece piece = pieceAt(pieceCoords);
|
||||||
|
if (piece == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
Color player = piece.getColor();
|
||||||
|
|
||||||
|
List<Coordinate> result = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
|
Coordinate destination = new Coordinate(i, j);
|
||||||
|
Move move = new Move(pieceCoords, destination);
|
||||||
|
|
||||||
|
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
||||||
|
move);
|
||||||
|
if (!piecePathChecker.isValid())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
applyMove(move);
|
||||||
|
if (!isKingInCheck(player))
|
||||||
|
result.add(destination);
|
||||||
|
undoLastMove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Move getLastMove() {
|
||||||
|
return this.lastMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastMove(Move lastMove) {
|
||||||
|
this.lastMove = lastMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
10
app/src/main/java/chess/model/Color.java
Normal file
10
app/src/main/java/chess/model/Color.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
public enum Color {
|
||||||
|
White,
|
||||||
|
Black;
|
||||||
|
|
||||||
|
public static Color getEnemy(Color color) {
|
||||||
|
return color == White ? Black : White;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
app/src/main/java/chess/model/Coordinate.java
Normal file
41
app/src/main/java/chess/model/Coordinate.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
public class Coordinate {
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
|
||||||
|
public static int VALUE_MAX = 8;
|
||||||
|
|
||||||
|
public Coordinate(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
return 0 <= this.x && this.x < VALUE_MAX && 0 <= this.y && this.y < VALUE_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Coordinate fromIndex(int index) {
|
||||||
|
return new Coordinate(index % VALUE_MAX, index / VALUE_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int toIndex() {
|
||||||
|
return this.y * VALUE_MAX + this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Coordinate coo) {
|
||||||
|
return this.x == coo.x && this.y == coo.y;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
public class Coordinates {
|
|
||||||
private int x;
|
|
||||||
private int y;
|
|
||||||
|
|
||||||
public Coordinates(int x, int y) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
public int getX() {
|
|
||||||
return this.x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getY() {
|
|
||||||
return this.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return "(" + this.x + ", " + this.y + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
54
app/src/main/java/chess/model/Direction.java
Normal file
54
app/src/main/java/chess/model/Direction.java
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
public enum Direction {
|
||||||
|
|
||||||
|
Unset(65),
|
||||||
|
Front(-8), Back(8), Left(-1), Right(1),
|
||||||
|
FrontLeft(-9), FrontRight(-7), BackLeft(7), BackRight(9);
|
||||||
|
|
||||||
|
private final int indexOffset;
|
||||||
|
|
||||||
|
Direction(int indexOffset) {
|
||||||
|
this.indexOffset = indexOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIndexOffset() {
|
||||||
|
return indexOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Direction fromInt(int direction) {
|
||||||
|
for (Direction dir : Direction.values()) {
|
||||||
|
if (dir.getIndexOffset() == direction)
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Direction findDirection(Move move) {
|
||||||
|
assert move.isValid() : "Move is invalid!";
|
||||||
|
int diffX = move.getFinish().getX() - move.getStart().getX();
|
||||||
|
int diffY = move.getFinish().getY() - move.getStart().getY();
|
||||||
|
|
||||||
|
if (diffX == 0 && diffY < 0)
|
||||||
|
return Direction.Front;
|
||||||
|
if (diffX == 0 && diffY > 0)
|
||||||
|
return Direction.Back;
|
||||||
|
|
||||||
|
if (diffX < 0 && diffY == 0)
|
||||||
|
return Direction.Left;
|
||||||
|
if (diffX > 0 && diffY == 0)
|
||||||
|
return Direction.Right;
|
||||||
|
|
||||||
|
if (diffX < 0 && -diffX == diffY)
|
||||||
|
return Direction.BackLeft;
|
||||||
|
if (diffX > 0 && diffX == diffY)
|
||||||
|
return Direction.BackRight;
|
||||||
|
|
||||||
|
if (diffY < 0 && diffX == diffY)
|
||||||
|
return Direction.FrontLeft;
|
||||||
|
if (diffX > 0 && diffX == -diffY)
|
||||||
|
return Direction.FrontRight;
|
||||||
|
|
||||||
|
return Direction.Unset;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,87 +1,73 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Stack;
|
||||||
import java.util.Observable;
|
|
||||||
|
|
||||||
public class Game extends Observable implements Runnable {
|
import chess.controller.PlayerCommand;
|
||||||
private final Board board;
|
import chess.controller.commands.MoveCommand;
|
||||||
private Move move;
|
|
||||||
private final ArrayList<Player> players = new ArrayList<>();
|
|
||||||
|
|
||||||
public Game(int size) {
|
public class Game {
|
||||||
this.board = new Board(size);
|
private final ChessBoard board;
|
||||||
this.setChanged();
|
private Color playerTurn;
|
||||||
this.notifyObservers();
|
private final Stack<PlayerCommand> movesHistory;
|
||||||
}
|
|
||||||
public Game() {
|
|
||||||
this(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize(){
|
public enum GameStatus {
|
||||||
return board.getSize();
|
Check, CheckMate, OnGoing, Pat;
|
||||||
}
|
}
|
||||||
public Board getBoard() {
|
|
||||||
return board;
|
|
||||||
}
|
|
||||||
public Move getMove() {
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMove(Move m){
|
public Game(ChessBoard board) {
|
||||||
this.move = m;
|
this.board = board;
|
||||||
synchronized (this) {
|
this.movesHistory = new Stack<>();
|
||||||
this.notify();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean gameDone(){
|
public ChessBoard getBoard() {
|
||||||
// todo
|
return board;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Player getNextPlayer(){
|
public Color getPlayerTurn() {
|
||||||
return players.getFirst();
|
return playerTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePlayersList(){
|
public void resetPlayerTurn() {
|
||||||
Player player = players.getFirst();
|
this.playerTurn = Color.White;
|
||||||
players.remove(player);
|
}
|
||||||
players.add(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void playGame() {
|
public void switchPlayerTurn() {
|
||||||
players.add(new Player(this));
|
playerTurn = Color.getEnemy(playerTurn);
|
||||||
players.add(new Player(this));
|
}
|
||||||
while (!gameDone()){
|
|
||||||
Move m = getNextPlayer().getMove();
|
|
||||||
while (!applyMove(m)){
|
|
||||||
m = getNextPlayer().getMove();
|
|
||||||
}
|
|
||||||
updatePlayersList();
|
|
||||||
setChanged();
|
|
||||||
notifyObservers();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public GameStatus checkGameStatus() {
|
||||||
public void run(){
|
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||||
playGame();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean applyMove(Move m){
|
if (this.board.isKingInCheck(enemy))
|
||||||
if (board.getCell(m.getStart()).getPiece() == null){
|
if (this.board.hasAllowedMoves(enemy))
|
||||||
return false;
|
return GameStatus.Check;
|
||||||
}
|
else
|
||||||
try {
|
return GameStatus.CheckMate;
|
||||||
move(m);
|
|
||||||
}
|
if (!board.hasAllowedMoves(enemy))
|
||||||
catch (Exception e) {
|
return GameStatus.Pat;
|
||||||
return false;
|
|
||||||
}
|
return GameStatus.OnGoing;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
public void addAction(PlayerCommand command) {
|
||||||
|
this.movesHistory.add(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlayerCommand getLastAction() {
|
||||||
|
if (this.movesHistory.isEmpty())
|
||||||
|
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 move(Move move) throws Exception{
|
|
||||||
board.movePiece(move);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
import java.util.Observable;
|
|
||||||
|
|
||||||
public class Model2D extends Observable {
|
|
||||||
public int x;
|
|
||||||
public int y;
|
|
||||||
|
|
||||||
public void set(int i, int j){
|
|
||||||
this.x = i;
|
|
||||||
this.y = j;
|
|
||||||
setChanged();
|
|
||||||
notifyObservers();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +1,56 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
private Coordinates start;
|
private final Coordinate start;
|
||||||
private Coordinates end;
|
private final Coordinate finish;
|
||||||
|
private Coordinate deadPieceCoords;
|
||||||
|
|
||||||
public Move(Coordinates start, Coordinates end) {
|
public Move(Coordinate start, Coordinate finish) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end = end;
|
this.finish = finish;
|
||||||
}
|
this.deadPieceCoords = finish;
|
||||||
|
}
|
||||||
|
|
||||||
public Coordinates getEnd() {
|
public boolean isValid() {
|
||||||
return end;
|
return this.start.isValid() && this.finish.isValid() && !this.start.equals(this.finish);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates getStart() {
|
public Coordinate getStart() {
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateMove(Coordinates start, Coordinates end) {
|
public Coordinate getFinish() {
|
||||||
this.start = start;
|
return finish;
|
||||||
this.end = end;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public int traversedCells() {
|
||||||
|
assert isValid() : "Move is invalid!";
|
||||||
|
|
||||||
|
int diffX = getFinish().getX() - getStart().getX();
|
||||||
|
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!";
|
||||||
|
|
||||||
|
if (diffX == 0)
|
||||||
|
return Math.abs(diffY);
|
||||||
|
if (diffY == 0)
|
||||||
|
return Math.abs(diffX);
|
||||||
|
if (Math.abs(diffX) == Math.abs(diffY))
|
||||||
|
return Math.abs(diffX);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,31 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
import chess.view.render2D.Pieces;
|
|
||||||
|
|
||||||
public abstract class Piece {
|
public abstract class Piece {
|
||||||
public final int color;
|
|
||||||
public AccessibleCellsDecorator decorator;
|
|
||||||
public final Board board;
|
|
||||||
public Cell position;
|
|
||||||
|
|
||||||
public Piece(int color, Board board, Cell position) {
|
private final Color color;
|
||||||
this.color = color;
|
private int moved;
|
||||||
this.board = board;
|
|
||||||
this.position = position;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getColor() {
|
public Piece(Color color) {
|
||||||
return color;
|
this.color = color;
|
||||||
}
|
this.moved = 0;
|
||||||
|
}
|
||||||
|
|
||||||
public void setPosition(Cell cell) {
|
public void move() {
|
||||||
this.position = cell;
|
this.moved++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void accept(PieceVisitor pv);
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasMoved() {
|
||||||
|
return moved > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unMove() {
|
||||||
|
this.moved--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract <T> T accept(PieceVisitor<T> visitor);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,28 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
import chess.model.pieces.*;
|
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 interface PieceVisitor{
|
public interface PieceVisitor<T> {
|
||||||
default void visit(Piece p) {
|
|
||||||
p.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void visitPiece(Bishop p);
|
default T visit(Piece piece) {
|
||||||
void visitPiece(Knight p);
|
return piece.accept(this);
|
||||||
void visitPiece(Pawn p);
|
}
|
||||||
void visitPiece(Queen p);
|
|
||||||
void visitPiece(King p);
|
T visitPiece(Bishop bishop);
|
||||||
void visitPiece(Rook p);
|
|
||||||
|
T visitPiece(King king);
|
||||||
|
|
||||||
|
T visitPiece(Knight knight);
|
||||||
|
|
||||||
|
T visitPiece(Pawn pawn);
|
||||||
|
|
||||||
|
T visitPiece(Queen queen);
|
||||||
|
|
||||||
|
T visitPiece(Rook rook);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package chess.model;
|
|
||||||
|
|
||||||
public class Player {
|
|
||||||
private final Game game;
|
|
||||||
|
|
||||||
public Player(Game game) {
|
|
||||||
this.game = game;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Move getMove(){
|
|
||||||
synchronized(game){
|
|
||||||
try {
|
|
||||||
game.wait();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return game.getMove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package chess.model.decorators;
|
|
||||||
|
|
||||||
import chess.model.AccessibleCellsDecorator;
|
|
||||||
import chess.model.Cell;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class DiagonalDecorator extends AccessibleCellsDecorator {
|
|
||||||
public DiagonalDecorator(AccessibleCellsDecorator base) {
|
|
||||||
super(base);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DiagonalDecorator(){
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ArrayList<Cell> getAccessibleCells() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package chess.model.decorators;
|
|
||||||
|
|
||||||
import chess.model.AccessibleCellsDecorator;
|
|
||||||
import chess.model.Cell;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class LineDecorator extends AccessibleCellsDecorator {
|
|
||||||
public LineDecorator(AccessibleCellsDecorator base) {
|
|
||||||
super(base);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LineDecorator(){
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ArrayList<Cell> getAccessibleCells() {
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.Board;
|
import chess.model.Color;
|
||||||
import chess.model.Cell;
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
public class Bishop extends Piece {
|
public class Bishop extends Piece {
|
||||||
public Bishop(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Bishop(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.Board;
|
import chess.model.Color;
|
||||||
import chess.model.Cell;
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
public class King extends Piece {
|
public class King extends Piece {
|
||||||
public King(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public King(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.Board;
|
import chess.model.Color;
|
||||||
import chess.model.Cell;
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
public class Knight extends Piece {
|
public class Knight extends Piece {
|
||||||
public Knight(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Knight(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,21 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.Board;
|
import chess.model.Color;
|
||||||
import chess.model.Cell;
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
public class Pawn extends Piece {
|
public class Pawn extends Piece {
|
||||||
public Pawn(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Pawn(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int multiplier() {
|
||||||
|
return getColor() == Color.White ? 1 : -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.*;
|
import chess.model.Color;
|
||||||
import chess.model.decorators.DiagonalDecorator;
|
import chess.model.Piece;
|
||||||
import chess.model.decorators.LineDecorator;
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
public class Queen extends Piece {
|
public class Queen extends Piece {
|
||||||
public Queen(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Queen(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,17 @@
|
|||||||
package chess.model.pieces;
|
package chess.model.pieces;
|
||||||
|
|
||||||
import chess.model.Board;
|
import chess.model.Color;
|
||||||
import chess.model.Cell;
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.model.PieceVisitor;
|
import chess.model.PieceVisitor;
|
||||||
import chess.model.decorators.DiagonalDecorator;
|
|
||||||
import chess.model.decorators.LineDecorator;
|
|
||||||
|
|
||||||
public class Rook extends Piece {
|
public class Rook extends Piece {
|
||||||
public Rook(final int color, Board board, Cell cell) {
|
|
||||||
super(color, board, cell);
|
|
||||||
this.decorator = new LineDecorator();
|
|
||||||
// todo
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Rook(Color color) {
|
||||||
public void accept(PieceVisitor pv) {
|
super(color);
|
||||||
pv.visitPiece(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
|
return visitor.visitPiece(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
52
app/src/main/java/chess/model/visitor/KingIdentifier.java
Normal file
52
app/src/main/java/chess/model/visitor/KingIdentifier.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 KingIdentifier implements PieceVisitor<Boolean> {
|
||||||
|
|
||||||
|
private final Color color;
|
||||||
|
|
||||||
|
public KingIdentifier(Color color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isKing(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 king.getColor() == color;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Knight knight) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Pawn pawn) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Queen queen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
119
app/src/main/java/chess/model/visitor/PermissiveRuleChecker.java
Normal file
119
app/src/main/java/chess/model/visitor/PermissiveRuleChecker.java
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package chess.model.visitor;
|
||||||
|
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Direction;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
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 PermissiveRuleChecker implements PieceVisitor<Boolean> {
|
||||||
|
|
||||||
|
private final Move move;
|
||||||
|
|
||||||
|
public PermissiveRuleChecker(Move move) {
|
||||||
|
this.move = move;
|
||||||
|
assert move.isValid() : "Move is invalid!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidFor(Piece piece) {
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
|
Direction moveDirection = Direction.findDirection(this.move);
|
||||||
|
|
||||||
|
switch (moveDirection) {
|
||||||
|
case FrontLeft:
|
||||||
|
case BackLeft:
|
||||||
|
case FrontRight:
|
||||||
|
case BackRight:
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(King king) {
|
||||||
|
return this.move.traversedCells() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Knight knight) {
|
||||||
|
Coordinate piecePos = move.getStart();
|
||||||
|
final Coordinate[] positions = {
|
||||||
|
new Coordinate(piecePos.getX() - 1, piecePos.getY() - 2),
|
||||||
|
new Coordinate(piecePos.getX() - 1, piecePos.getY() + 2),
|
||||||
|
new Coordinate(piecePos.getX() + 1, piecePos.getY() - 2),
|
||||||
|
new Coordinate(piecePos.getX() + 1, piecePos.getY() + 2),
|
||||||
|
new Coordinate(piecePos.getX() + 2, piecePos.getY() - 1),
|
||||||
|
new Coordinate(piecePos.getX() + 2, piecePos.getY() + 1),
|
||||||
|
new Coordinate(piecePos.getX() - 2, piecePos.getY() - 1),
|
||||||
|
new Coordinate(piecePos.getX() - 2, piecePos.getY() + 1),
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < positions.length; i++) {
|
||||||
|
if (this.move.getFinish().equals(positions[i]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Pawn pawn) {
|
||||||
|
Direction moveDirection = Direction.findDirection(this.move);
|
||||||
|
int directionIndexOffset = moveDirection.getIndexOffset();
|
||||||
|
int distance = this.move.traversedCells();
|
||||||
|
|
||||||
|
// Revoke moving backwards
|
||||||
|
if (directionIndexOffset * pawn.multiplier() > 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Allowing straight moves
|
||||||
|
if (Math.abs(directionIndexOffset) == Math.abs(Direction.Front.getIndexOffset())) {
|
||||||
|
if (pawn.hasMoved())
|
||||||
|
return distance == 1;
|
||||||
|
return distance == 1 || distance == 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allowing small diagonal moves
|
||||||
|
if (directionIndexOffset * pawn.multiplier() == Direction.FrontLeft.getIndexOffset()
|
||||||
|
|| directionIndexOffset * pawn.multiplier() == Direction.FrontRight.getIndexOffset()) {
|
||||||
|
return distance == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Queen queen) {
|
||||||
|
Direction moveDirection = Direction.findDirection(this.move);
|
||||||
|
return moveDirection != Direction.Unset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
Direction moveDirection = Direction.findDirection(this.move);
|
||||||
|
|
||||||
|
switch (moveDirection) {
|
||||||
|
case Front:
|
||||||
|
case Back:
|
||||||
|
case Left:
|
||||||
|
case Right:
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
144
app/src/main/java/chess/model/visitor/PiecePathChecker.java
Normal file
144
app/src/main/java/chess/model/visitor/PiecePathChecker.java
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package chess.model.visitor;
|
||||||
|
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Direction;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
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 PiecePathChecker implements PieceVisitor<Boolean> {
|
||||||
|
|
||||||
|
private final ChessBoard board;
|
||||||
|
private final Move move;
|
||||||
|
|
||||||
|
public PiecePathChecker(ChessBoard board, Move move) {
|
||||||
|
this.move = move;
|
||||||
|
this.board = board;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
if (this.move.getStart().equals(this.move.getFinish()))
|
||||||
|
return false;
|
||||||
|
Piece piece = this.board.pieceAt(move.getStart());
|
||||||
|
if (piece == null)
|
||||||
|
return false;
|
||||||
|
return visit(piece);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Bishop bishop) {
|
||||||
|
return basicCheck(bishop);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(King king) {
|
||||||
|
return destCheck(king);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Knight knight) {
|
||||||
|
return destCheck(knight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Queen queen) {
|
||||||
|
return basicCheck(queen);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Rook rook) {
|
||||||
|
return basicCheck(rook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean visitPiece(Pawn pawn) {
|
||||||
|
if (!new PermissiveRuleChecker(this.move).isValidFor(pawn))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Direction moveDirection = Direction.fromInt(Direction.findDirection(move).getIndexOffset() * pawn.multiplier());
|
||||||
|
|
||||||
|
if (moveDirection == Direction.Front)
|
||||||
|
return testPath(pawn.getColor()) && this.board.pieceAt(this.move.getFinish()) == null;
|
||||||
|
|
||||||
|
assert moveDirection == Direction.FrontLeft || moveDirection == Direction.FrontRight;
|
||||||
|
|
||||||
|
if (checkEnPassant())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
Piece destPiece = this.board.pieceAt(this.move.getFinish());
|
||||||
|
if (destPiece == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return destPiece.getColor() != pawn.getColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkEnPassant() {
|
||||||
|
Move lastMove = this.board.getLastMove();
|
||||||
|
|
||||||
|
if (lastMove == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Piece pieceToEat = this.board.pieceAt(lastMove.getFinish());
|
||||||
|
|
||||||
|
if (pieceToEat == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Piece pawn = this.board.pieceAt(this.move.getStart());
|
||||||
|
|
||||||
|
if (pieceToEat.getColor() == pawn.getColor())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (lastMove.getMiddle().equals(this.move.getFinish())
|
||||||
|
&& new PawnIdentifier(pieceToEat.getColor()).isPawn(pieceToEat)) {
|
||||||
|
this.move.setDeadPieceCoords(lastMove.getFinish());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean destCheck(Piece piece) {
|
||||||
|
if (!new PermissiveRuleChecker(this.move).isValidFor(piece))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
Direction moveDirection = Direction.findDirection(this.move);
|
||||||
|
int distance = this.move.traversedCells();
|
||||||
|
int stepIndex = move.getStart().toIndex();
|
||||||
|
|
||||||
|
for (int step = 0; step < distance; step++) {
|
||||||
|
stepIndex += moveDirection.getIndexOffset();
|
||||||
|
|
||||||
|
if (Coordinate.fromIndex(stepIndex).equals(move.getFinish())) {
|
||||||
|
Piece pieceDest = this.board.pieceAt(move.getFinish());
|
||||||
|
if (pieceDest == null)
|
||||||
|
return true;
|
||||||
|
return pieceDest.getColor() != color;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.board.isCellEmpty(Coordinate.fromIndex(stepIndex)))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
36
app/src/main/java/chess/view/consolerender/Colors.java
Normal file
36
app/src/main/java/chess/view/consolerender/Colors.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
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 = "\u001B[31m";
|
||||||
|
public static final String GREEN = "\u001B[32m";
|
||||||
|
public static final String YELLOW = "\u001B[33m";
|
||||||
|
public static final String BLUE = "\u001B[34m";
|
||||||
|
public static final String PURPLE = "\u001B[35m";
|
||||||
|
public static final String CYAN = "\u001B[36m";
|
||||||
|
|
||||||
|
// Background
|
||||||
|
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
|
||||||
|
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
|
||||||
|
}
|
||||||
166
app/src/main/java/chess/view/consolerender/Console.java
Normal file
166
app/src/main/java/chess/view/consolerender/Console.java
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
package chess.view.consolerender;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.SurrenderCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Console implements GameListener {
|
||||||
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
private final ConsolePieceName consolePieceName = new ConsolePieceName();
|
||||||
|
|
||||||
|
public Console(CommandExecutor commandExecutor) {
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(int x, int y) {
|
||||||
|
return pieceAt(new Coordinate(x, y));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(Coordinate coordinate) {
|
||||||
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||||
|
this.commandExecutor.executeCommand(command);
|
||||||
|
return command.getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coordinate stringToCoordinate(String coordinates) {
|
||||||
|
char xPos = coordinates.charAt(0);
|
||||||
|
char yPos = coordinates.charAt(1);
|
||||||
|
int x = xPos - 'a';
|
||||||
|
int y = 7 - (yPos - '1');
|
||||||
|
return new Coordinate(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean playerPickedMove() {
|
||||||
|
System.out.println("Piece to move: ");
|
||||||
|
Coordinate start = stringToCoordinate(scanner.nextLine());
|
||||||
|
System.out.println("New position: ");
|
||||||
|
Coordinate end = stringToCoordinate(scanner.nextLine());
|
||||||
|
return this.commandExecutor.executeCommand(new MoveCommand(new Move(start, end))) == Command.CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean playerPickedSurrender(Color player) {
|
||||||
|
this.commandExecutor.executeCommand(new SurrenderCommand(player));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerTurn(Color color) {
|
||||||
|
updateDisplay();
|
||||||
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
|
boolean endTurn = false;
|
||||||
|
do {
|
||||||
|
System.out.println("""
|
||||||
|
Pick your move:
|
||||||
|
1 - Move
|
||||||
|
2 - Show potential moves
|
||||||
|
3 - Surrender
|
||||||
|
""");
|
||||||
|
endTurn = switch (scanner.nextLine()) {
|
||||||
|
case "1" -> playerPickedMove();
|
||||||
|
case "2" -> playerPickedShowMoves();
|
||||||
|
case "3" -> playerPickedSurrender(color);
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
} while (!endTurn);
|
||||||
|
System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean playerPickedShowMoves() {
|
||||||
|
System.out.println("Piece to examine: ");
|
||||||
|
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
||||||
|
// todo
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void winnerIs(Color color) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInCheck() {
|
||||||
|
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInMat() {
|
||||||
|
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void patSituation() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hasSurrendered(Color color) {
|
||||||
|
System.out.println("The " + color + " player has surrendered!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void gameStarted() {
|
||||||
|
System.out.println("Game start:");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void promotePawn(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.");
|
||||||
|
boolean valid = false;
|
||||||
|
PromoteCommand.PromoteType newPiece;
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
String promotion = scanner.next();
|
||||||
|
newPiece = switch (promotion) {
|
||||||
|
case ("B") -> PromoteCommand.PromoteType.Bishop;
|
||||||
|
case ("N") -> PromoteCommand.PromoteType.Knight;
|
||||||
|
case ("Q") -> PromoteCommand.PromoteType.Queen;
|
||||||
|
case ("R") -> PromoteCommand.PromoteType.Rook;
|
||||||
|
default -> throw new Exception();
|
||||||
|
};
|
||||||
|
valid = true;
|
||||||
|
this.commandExecutor.executeCommand(new PromoteCommand(newPiece));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Invalid input!");
|
||||||
|
}
|
||||||
|
} while (!valid);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDisplay() {
|
||||||
|
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.WHITE_BACKGROUND);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
string.append(Colors.BLACK_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
|
|
||||||
|
import chess.view.render.shader.BoardShader;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
import static org.lwjgl.opengl.GL30.*;
|
||||||
import chess.view.render3D.shader.BoardShader;
|
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
private BoardShader shader;
|
private BoardShader shader;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package chess.view.render;
|
||||||
|
|
||||||
|
public record VertexAttribPointer(int index, int size, int offset) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||||
|
|
||||||
@@ -41,9 +41,9 @@ public class VertexBuffer {
|
|||||||
|
|
||||||
public void BindVertexAttribs() {
|
public void BindVertexAttribs() {
|
||||||
for (VertexAttribPointer vertexAttribPointer : vertexAttribs) {
|
for (VertexAttribPointer vertexAttribPointer : vertexAttribs) {
|
||||||
GL30.glEnableVertexAttribArray(vertexAttribPointer.index);
|
GL30.glEnableVertexAttribArray(vertexAttribPointer.index());
|
||||||
GL30.glVertexAttribPointer(vertexAttribPointer.index, vertexAttribPointer.size, GL_FLOAT, false,
|
GL30.glVertexAttribPointer(vertexAttribPointer.index(), vertexAttribPointer.size(), GL_FLOAT, false,
|
||||||
this.dataStride * 4, vertexAttribPointer.offset);
|
this.dataStride * 4, vertexAttribPointer.offset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D;
|
package chess.view.render;
|
||||||
|
|
||||||
import org.lwjgl.*;
|
import org.lwjgl.*;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
@@ -56,7 +56,7 @@ public class Window {
|
|||||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
|
||||||
|
|
||||||
// Create the window
|
// Create the window
|
||||||
window = glfwCreateWindow(1000, 1000, "Chess4J", NULL, NULL);
|
window = glfwCreateWindow(1000, 1000, "3DChess", NULL, NULL);
|
||||||
if (window == NULL)
|
if (window == NULL)
|
||||||
throw new RuntimeException("Failed to create the GLFW window");
|
throw new RuntimeException("Failed to create the GLFW window");
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D.shader;
|
package chess.view.render.shader;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ public class BoardShader extends ShaderProgram {
|
|||||||
|
|
||||||
uniform mat4 camMatrix;
|
uniform mat4 camMatrix;
|
||||||
|
|
||||||
out vec3 pass_color;
|
flat out vec3 pass_color;
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
gl_Position = camMatrix * vec4(position, 1.0);
|
gl_Position = camMatrix * vec4(position, 1.0);
|
||||||
@@ -23,7 +23,7 @@ public class BoardShader extends ShaderProgram {
|
|||||||
private static String fragmentShader = """
|
private static String fragmentShader = """
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
in vec3 pass_color;
|
flat in vec3 pass_color;
|
||||||
|
|
||||||
out vec4 out_color;
|
out vec4 out_color;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.view.render3D.shader;
|
package chess.view.render.shader;
|
||||||
|
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package chess.view.render2D;
|
|
||||||
|
|
||||||
import chess.model.Piece;
|
|
||||||
import chess.model.PieceVisitor;
|
|
||||||
import chess.model.pieces.*;
|
|
||||||
|
|
||||||
public class PieceFileName implements PieceVisitor {
|
|
||||||
|
|
||||||
private String pieceName;
|
|
||||||
private static final String BASE = "app/src/main/resources/pieces2D/";
|
|
||||||
|
|
||||||
PieceFileName(Piece piece) {
|
|
||||||
visit(piece);
|
|
||||||
pieceName = colorToString(piece.getColor()) +"-"+ pieceName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String colorToString(int color) {
|
|
||||||
switch (color) {
|
|
||||||
case 0:
|
|
||||||
return "white";
|
|
||||||
case 1:
|
|
||||||
return "black";
|
|
||||||
default:
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(Bishop p) {
|
|
||||||
this.pieceName = "bishop";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(Knight p) {
|
|
||||||
this.pieceName = "knight";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(Pawn p) {
|
|
||||||
this.pieceName = "pawn";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(Queen p) {
|
|
||||||
this.pieceName = "queen";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(King p) {
|
|
||||||
this.pieceName = "king";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPiece(Rook p) {
|
|
||||||
this.pieceName = "rook";
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFileName() {
|
|
||||||
return BASE + pieceName + ".png";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package chess.view.render2D;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public enum Pieces {
|
|
||||||
WHITE_KING,
|
|
||||||
WHITE_QUEEN,
|
|
||||||
WHITE_KNIGHT,
|
|
||||||
WHITE_BISHOP,
|
|
||||||
WHITE_ROOK,
|
|
||||||
WHITE_PAWN,
|
|
||||||
BLACK_KING,
|
|
||||||
BLACK_QUEEN,
|
|
||||||
BLACK_KNIGHT,
|
|
||||||
BLACK_BISHOP,
|
|
||||||
BLACK_ROOK,
|
|
||||||
BLACK_PAWN;
|
|
||||||
|
|
||||||
public static ImageIcon getIcon(String path) {
|
|
||||||
return new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
package chess.view.render2D;
|
|
||||||
|
|
||||||
import chess.controller.Controller;
|
|
||||||
import chess.model.Coordinates;
|
|
||||||
import chess.model.Game;
|
|
||||||
import chess.model.Piece;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.MouseAdapter;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.util.Observable;
|
|
||||||
import java.util.Observer;
|
|
||||||
|
|
||||||
public class Window extends JFrame implements Observer {
|
|
||||||
private final Game game;
|
|
||||||
private final Controller controller;
|
|
||||||
private final JLabel[][] tab;
|
|
||||||
private static final String path = "app/src/main/resources/pieces2D/";
|
|
||||||
Coordinates mouseClick = null;
|
|
||||||
Coordinates mouseRelease = null;
|
|
||||||
|
|
||||||
public Window(Game game) {
|
|
||||||
this.game = game;
|
|
||||||
game.addObserver(this);
|
|
||||||
this.controller = new Controller(game, this);
|
|
||||||
tab = new JLabel[game.getSize()][game.getSize()];
|
|
||||||
build();
|
|
||||||
showBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args){
|
|
||||||
Game game = new Game();
|
|
||||||
Thread gameThread = new Thread(game);
|
|
||||||
gameThread.start();
|
|
||||||
Window f = new Window(game);
|
|
||||||
f.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a playing grid with alternating black & white cells
|
|
||||||
*/
|
|
||||||
private void build(){
|
|
||||||
JPanel jp = new JPanel(new GridLayout(game.getSize(), game.getSize()));
|
|
||||||
setContentPane(jp);
|
|
||||||
setTitle("Let's play chess!");
|
|
||||||
for (int y = 0; y < game.getSize(); y++){
|
|
||||||
for (int x = 0; x < game.getSize(); x++){
|
|
||||||
JLabel jl = new JLabel();
|
|
||||||
jl.setOpaque(true);
|
|
||||||
if (((y+x)%2)!=0) {
|
|
||||||
jl.setBackground(Color.BLACK);
|
|
||||||
}
|
|
||||||
final int yy = y;
|
|
||||||
final int xx = x;
|
|
||||||
|
|
||||||
jl.addMouseListener(new MouseAdapter() {
|
|
||||||
public void mousePressed(MouseEvent e) {
|
|
||||||
if (mouseClick == null) {
|
|
||||||
mouseClick = new Coordinates(xx, yy);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mouseRelease = new Coordinates(xx, yy);
|
|
||||||
if (!mouseClick.equals(mouseRelease)) {
|
|
||||||
controller.sendMove(mouseClick, mouseRelease);
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
mouseClick = null;
|
|
||||||
mouseRelease = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.tab[x][y]=jl;
|
|
||||||
jp.add(jl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setSize(800,800);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showBoard(){
|
|
||||||
for (int y = 0; y < game.getSize(); y++){
|
|
||||||
for (int x = 0; x < game.getSize(); x++){
|
|
||||||
Piece p = game.getBoard().getCell(new Coordinates(x,y)).getPiece();
|
|
||||||
if (p != null){
|
|
||||||
this.tab[x][y].setIcon(Pieces.getIcon(new PieceFileName(p).getFileName()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.tab[x][y].setIcon(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
// todo
|
|
||||||
showBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(Observable o, Object arg) {
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package chess.view.render3D;
|
|
||||||
|
|
||||||
public class VertexAttribPointer {
|
|
||||||
public int index;
|
|
||||||
public int size;
|
|
||||||
public int offset;
|
|
||||||
|
|
||||||
public VertexAttribPointer(int index, int size, int offset) {
|
|
||||||
this.index = index;
|
|
||||||
this.size = size;
|
|
||||||
this.offset = offset;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
79
app/src/main/java/chess/view/simplerender/PieceIcon.java
Normal file
79
app/src/main/java/chess/view/simplerender/PieceIcon.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package chess.view.simplerender;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.swing.Icon;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Piece;
|
||||||
|
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;
|
||||||
|
import chess.view.AssetManager;
|
||||||
|
|
||||||
|
public class PieceIcon implements PieceVisitor<String> {
|
||||||
|
|
||||||
|
private static final String basePath = "pieces2D/";
|
||||||
|
private static final Map<String, Icon> cache = new HashMap<>();
|
||||||
|
|
||||||
|
public Icon getIcon(Piece piece) throws IOException {
|
||||||
|
if (piece == null)
|
||||||
|
return null;
|
||||||
|
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
||||||
|
return getIcon(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Icon getIcon(String path) throws IOException {
|
||||||
|
Icon image = cache.get(path);
|
||||||
|
if (image != null)
|
||||||
|
return image;
|
||||||
|
|
||||||
|
image = new ImageIcon(new ImageIcon(AssetManager.getResource(path).readAllBytes()).getImage()
|
||||||
|
.getScaledInstance(100, 100, Image.SCALE_SMOOTH));
|
||||||
|
cache.put(path, image);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colorToString(Color color) {
|
||||||
|
return color == Color.Black ? "black" : "white";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Bishop bishop) {
|
||||||
|
return "bishop";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(King king) {
|
||||||
|
return "king";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Knight knight) {
|
||||||
|
return "knight";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Pawn pawn) {
|
||||||
|
return "pawn";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Queen queen) {
|
||||||
|
return "queen";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitPiece(Rook rook) {
|
||||||
|
return "rook";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
295
app/src/main/java/chess/view/simplerender/Window.java
Normal file
295
app/src/main/java/chess/view/simplerender/Window.java
Normal file
@@ -0,0 +1,295 @@
|
|||||||
|
package chess.view.simplerender;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
|
import chess.controller.Command.CommandResult;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.CastlingCommand;
|
||||||
|
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
||||||
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GetPlayerMovesCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
|
public class Window extends JFrame implements GameListener {
|
||||||
|
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
|
private Coordinate lastClick = null;
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
public Window(CommandExecutor commandExecutor) {
|
||||||
|
this.cells = new JLabel[8][8];
|
||||||
|
this.displayText = new JLabel();
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
|
setSize(800, 910);
|
||||||
|
setVisible(true);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommandResult sendCommand(Command command) {
|
||||||
|
return this.commandExecutor.executeCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color getCellColor(int x, int y) {
|
||||||
|
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() {
|
||||||
|
JPanel content = new JPanel();
|
||||||
|
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||||
|
JPanel bottom = new JPanel();
|
||||||
|
|
||||||
|
buildButtons(bottom);
|
||||||
|
|
||||||
|
content.add(this.displayText);
|
||||||
|
content.add(grid);
|
||||||
|
content.add(bottom);
|
||||||
|
|
||||||
|
setContentPane(content);
|
||||||
|
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
JLabel label = new JLabel();
|
||||||
|
label.setOpaque(true);
|
||||||
|
label.setBackground(getCellColor(x, y));
|
||||||
|
this.cells[x][y] = label;
|
||||||
|
|
||||||
|
final int xx = x;
|
||||||
|
final int yy = y;
|
||||||
|
|
||||||
|
label.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
onCellClicked(xx, yy);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
grid.add(label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCellEmpty(int x, int y) {
|
||||||
|
return pieceAt(x, y) == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(int x, int y) {
|
||||||
|
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
|
||||||
|
sendCommand(command);
|
||||||
|
return command.getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateBoard() {
|
||||||
|
PieceIcon pieceIcon = new PieceIcon();
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
JLabel cell = this.cells[x][y];
|
||||||
|
try {
|
||||||
|
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean previewMoves(int x, int y) {
|
||||||
|
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(new Coordinate(x, y));
|
||||||
|
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
List<Coordinate> allowedMoves = movesCommand.getDestinations();
|
||||||
|
if (allowedMoves.isEmpty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (Coordinate destCoord : allowedMoves) {
|
||||||
|
JLabel cell = this.cells[destCoord.getX()][destCoord.getY()];
|
||||||
|
Graphics g = cell.getGraphics();
|
||||||
|
g.setColor(new Color(128, 128, 128, 128));
|
||||||
|
g.fillOval(25, 25, 50, 50);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawInvalid(Move move) {
|
||||||
|
JLabel from = this.cells[move.getStart().getX()][move.getStart().getY()];
|
||||||
|
JLabel to = this.cells[move.getFinish().getX()][move.getFinish().getY()];
|
||||||
|
from.setBackground(Color.RED);
|
||||||
|
to.setBackground(Color.RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearMoves() {
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
JLabel cell = this.cells[x][y];
|
||||||
|
cell.setBackground(getCellColor(x, y));
|
||||||
|
cell.paint(cell.getGraphics());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onCellClicked(int x, int y) {
|
||||||
|
clearMoves();
|
||||||
|
if (this.lastClick == null) {
|
||||||
|
if (isCellEmpty(x, y))
|
||||||
|
return;
|
||||||
|
if (!previewMoves(x, y))
|
||||||
|
return;
|
||||||
|
this.lastClick = new Coordinate(x, y);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.lastClick.equals(new Coordinate(x, y))) {
|
||||||
|
Move move = new Move(lastClick, new Coordinate(x, y));
|
||||||
|
|
||||||
|
if (sendCommand(new MoveCommand(move)) == CommandResult.NotAllowed) {
|
||||||
|
drawInvalid(move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.lastClick = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerTurn(chess.model.Color color) {
|
||||||
|
this.displayText.setText("Current turn: " + color);
|
||||||
|
|
||||||
|
// dumb IA
|
||||||
|
if (color == chess.model.Color.Black) {
|
||||||
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||||
|
sendCommand(cmd);
|
||||||
|
List<Move> moves = cmd.getMoves();
|
||||||
|
int random = new Random().nextInt(moves.size());
|
||||||
|
sendCommand(new MoveCommand(moves.get(random)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void winnerIs(chess.model.Color color) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, "Victory of " + color);
|
||||||
|
this.dispose();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInCheck() {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, "Check!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void kingIsInMat() {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, "Checkmate!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void patSituation() {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hasSurrendered(chess.model.Color color) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
JOptionPane.showMessageDialog(this, color + " has surrendered.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void gameStarted() {
|
||||||
|
buildBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
Object[] possibilities = new Object[PromoteType.values().length];
|
||||||
|
int i = 0;
|
||||||
|
for (PromoteType type : PromoteType.values()) {
|
||||||
|
possibilities[i] = type.name();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (result == null || result.isEmpty()) {
|
||||||
|
result = (String) JOptionPane.showInputDialog(
|
||||||
|
this,
|
||||||
|
"Choose the type of piece to upgrade the pawn",
|
||||||
|
"Promote Dialog",
|
||||||
|
JOptionPane.PLAIN_MESSAGE,
|
||||||
|
null,
|
||||||
|
possibilities,
|
||||||
|
possibilities[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
PromoteType choosedType = null;
|
||||||
|
|
||||||
|
for (PromoteType type : PromoteType.values()) {
|
||||||
|
if (type.name().equals(result)) {
|
||||||
|
choosedType = type;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (choosedType != null)
|
||||||
|
sendCommand(new PromoteCommand(choosedType));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDisplay() {
|
||||||
|
updateBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
26
app/src/main/java/common/Signal0.java
Normal file
26
app/src/main/java/common/Signal0.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Signal0 {
|
||||||
|
private final List<Runnable> handlers;
|
||||||
|
|
||||||
|
public Signal0() {
|
||||||
|
this.handlers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(Runnable handler) {
|
||||||
|
this.handlers.add(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect(Runnable handler) {
|
||||||
|
this.handlers.remove(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void emit() {
|
||||||
|
for (Runnable handler : this.handlers) {
|
||||||
|
handler.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
app/src/main/java/common/Signal1.java
Normal file
27
app/src/main/java/common/Signal1.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class Signal1<T> {
|
||||||
|
private final List<Consumer<T>> handlers;
|
||||||
|
|
||||||
|
public Signal1() {
|
||||||
|
this.handlers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(Consumer<T> handler) {
|
||||||
|
this.handlers.add(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect(Consumer<T> handler) {
|
||||||
|
this.handlers.remove(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void emit(T arg) {
|
||||||
|
for (Consumer<T> handler : this.handlers) {
|
||||||
|
handler.accept(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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
|
||||||
@@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
class AppTest {
|
class AppTest {
|
||||||
@Test void appHasAGreeting() {
|
@Test void appHasAGreeting() {
|
||||||
App classUnderTest = new App();
|
// App classUnderTest = new App();
|
||||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user