Compare commits
20 Commits
decorateur
...
810934aea1
| Author | SHA1 | Date | |
|---|---|---|---|
| 810934aea1 | |||
| 48a215eae5 | |||
| 55ef180f57 | |||
| 5b006034ad | |||
| 873ffc05d3 | |||
| 55774b4605 | |||
| 0d72e015f1 | |||
| 927ba129f6 | |||
| 36e04376c3 | |||
| a81da804f0 | |||
| 9179b3cda9 | |||
| 97cafb903a | |||
| 1b9ff5bdd1 | |||
| 2c6b64fa7d | |||
| 5598d4f5eb | |||
| dcdf118274 | |||
| 0bef89c46f | |||
| dc2ea660ff | |||
| b98e1aaade | |||
| 0c35c38ccd |
4
.gitignore
vendored
@@ -3,3 +3,7 @@
|
||||
|
||||
# Ignore Gradle build output directory
|
||||
build
|
||||
|
||||
app/bin
|
||||
|
||||
.vscode
|
||||
@@ -3,14 +3,22 @@
|
||||
*/
|
||||
package chess;
|
||||
|
||||
import chess.render.*;
|
||||
import chess.io.CommandExecutor;
|
||||
import chess.io.commands.NewGameCommand;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Game;
|
||||
import chess.simplerender.Window;
|
||||
|
||||
public class App {
|
||||
public String getGreeting() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Window().run();
|
||||
CommandExecutor commandExecutor = new CommandExecutor();
|
||||
|
||||
Game game = new Game(new ChessBoard());
|
||||
Window window = new Window(commandExecutor);
|
||||
|
||||
commandExecutor.setGame(game);
|
||||
commandExecutor.setOutputSystem(window);
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
}
|
||||
}
|
||||
|
||||
7
app/src/main/java/chess/io/Command.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Game;
|
||||
|
||||
public abstract class Command {
|
||||
public abstract CommandResult execute(Game game, OutputSystem outputSystem);
|
||||
}
|
||||
90
app/src/main/java/chess/io/CommandExecutor.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.io.commands.MoveCommand;
|
||||
import chess.model.Game;
|
||||
import chess.model.Game.GameStatus;
|
||||
|
||||
public class CommandExecutor {
|
||||
|
||||
private Game game;
|
||||
private OutputSystem outputSystem;
|
||||
|
||||
public CommandExecutor() {
|
||||
this.game = null;
|
||||
this.outputSystem = null;
|
||||
}
|
||||
|
||||
public CommandResult executeCommand(Command command) {
|
||||
assert this.game != null : "No input game specified !";
|
||||
assert this.outputSystem != null : "No output system specified !";
|
||||
|
||||
CommandResult result = command.execute(this.game, this.outputSystem);
|
||||
|
||||
// non player commands are not supposed to return move result
|
||||
assert result != CommandResult.Moved || command instanceof PlayerCommand;
|
||||
|
||||
processResult(command, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void processResult(Command command, CommandResult result) {
|
||||
if (result != CommandResult.Moved)
|
||||
return;
|
||||
|
||||
if (command instanceof MoveCommand move) {
|
||||
boolean needsPromote = this.game.pawnShouldBePromoted();
|
||||
if (needsPromote)
|
||||
this.outputSystem.promotePawn(move.getMove().getFinish());
|
||||
|
||||
if (checkGameStatus())
|
||||
return;
|
||||
|
||||
if(!needsPromote)
|
||||
switchPlayerTurn();
|
||||
} else if (command instanceof PlayerCommand) {
|
||||
switchPlayerTurn();
|
||||
}
|
||||
}
|
||||
|
||||
private void switchPlayerTurn() {
|
||||
this.game.switchPlayerTurn();
|
||||
this.outputSystem.playerTurn(this.game.getPlayerTurn());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return True if the game is over
|
||||
*/
|
||||
private boolean checkGameStatus() {
|
||||
GameStatus gameStatus = this.game.checkGameStatus();
|
||||
|
||||
switch (gameStatus) {
|
||||
case Check:
|
||||
this.outputSystem.kingIsInCheck();
|
||||
return false;
|
||||
|
||||
case CheckMate:
|
||||
this.outputSystem.kingIsInMat();
|
||||
this.outputSystem.winnerIs(this.game.getPlayerTurn());
|
||||
return true;
|
||||
|
||||
case OnGoing:
|
||||
return false;
|
||||
|
||||
case Pat:
|
||||
this.outputSystem.patSituation();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setOutputSystem(OutputSystem outputSystem) {
|
||||
this.outputSystem = outputSystem;
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
}
|
||||
5
app/src/main/java/chess/io/CommandResult.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package chess.io;
|
||||
|
||||
public enum CommandResult {
|
||||
Moved, NotMoved, NotAllowed;
|
||||
}
|
||||
23
app/src/main/java/chess/io/OutputSystem.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
|
||||
public interface OutputSystem {
|
||||
|
||||
void playerTurn(Color color);
|
||||
|
||||
void winnerIs(Color color);
|
||||
|
||||
void kingIsInCheck();
|
||||
|
||||
void kingIsInMat();
|
||||
|
||||
void patSituation();
|
||||
|
||||
void hasSurrendered(Color color);
|
||||
|
||||
void gameStarted();
|
||||
|
||||
void promotePawn(Coordinate pieceCoords);
|
||||
}
|
||||
7
app/src/main/java/chess/io/PlayerCommand.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Game;
|
||||
|
||||
public abstract class PlayerCommand extends Command{
|
||||
public abstract void undo(Game game, OutputSystem outputSystem);
|
||||
}
|
||||
21
app/src/main/java/chess/io/commands/CastlingCommand.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.PlayerCommand;
|
||||
import chess.model.Game;
|
||||
|
||||
public class CastlingCommand extends PlayerCommand{
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo(Game game, OutputSystem outputSystem) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Piece;
|
||||
|
||||
public class GetAllowedMovesCommand extends Command {
|
||||
|
||||
private final Coordinate start;
|
||||
private List<Coordinate> destinations;
|
||||
|
||||
public GetAllowedMovesCommand(Coordinate start) {
|
||||
this.start = start;
|
||||
this.destinations = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem 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;
|
||||
}
|
||||
}
|
||||
34
app/src/main/java/chess/io/commands/GetPieceAtCommand.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
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, OutputSystem outputSystem) {
|
||||
if (!pieceCoords.isValid())
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
this.piece = game.getBoard().pieceAt(pieceCoords);
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
public Piece getPiece() {
|
||||
return piece;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.PlayerCommand;
|
||||
import chess.model.Game;
|
||||
|
||||
public class GrandCastlingCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo(Game game, OutputSystem outputSystem) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'undo'");
|
||||
}
|
||||
|
||||
}
|
||||
63
app/src/main/java/chess/io/commands/MoveCommand.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.PlayerCommand;
|
||||
import chess.model.ChessBoard;
|
||||
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, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
// we must promote the pending pawn before
|
||||
if (game.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.getFinish());
|
||||
board.applyMove(move);
|
||||
|
||||
if (board.isKingInCheck(game.getPlayerTurn())) {
|
||||
board.undoLastMove();
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
board.undoMove(move, deadPiece);
|
||||
}
|
||||
|
||||
}
|
||||
59
app/src/main/java/chess/io/commands/NewGameCommand.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
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, OutputSystem 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;
|
||||
}
|
||||
}
|
||||
89
app/src/main/java/chess/io/commands/PromoteCommand.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.PlayerCommand;
|
||||
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.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
|
||||
public class PromoteCommand extends PlayerCommand {
|
||||
|
||||
public enum PromoteType {
|
||||
Queen,
|
||||
Rook,
|
||||
Bishop,
|
||||
Knight
|
||||
}
|
||||
|
||||
private final PromoteType promoteType;
|
||||
private Coordinate pieceCoords;
|
||||
|
||||
public PromoteCommand(PromoteType promoteType) {
|
||||
this.promoteType = promoteType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
this.pieceCoords = game.pawnPromotePosition();
|
||||
|
||||
if (this.pieceCoords == null)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
Piece pawn = board.pieceAt(this.pieceCoords);
|
||||
if (pawn == null || !(pawn instanceof Pawn))
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
int destY = this.pieceCoords.getY();
|
||||
|
||||
int enemyLine = pawn.getColor() == Color.White ? 0 : 7;
|
||||
|
||||
if (destY != enemyLine)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
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
|
||||
public void undo(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
Piece promoted = board.pieceAt(this.pieceCoords);
|
||||
|
||||
assert promoted != null;
|
||||
|
||||
Color player = promoted.getColor();
|
||||
board.pieceComes(new Pawn(player), this.pieceCoords);
|
||||
}
|
||||
|
||||
}
|
||||
24
app/src/main/java/chess/io/commands/SurrenderCommand.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
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, OutputSystem outputSystem) {
|
||||
outputSystem.hasSurrendered(player);
|
||||
outputSystem.winnerIs(Color.getEnemy(player));
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
}
|
||||
16
app/src/main/java/chess/io/commands/UndoCommand.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.Game;
|
||||
|
||||
public class UndoCommand extends Command{
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
//TODO
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
}
|
||||
180
app/src/main/java/chess/model/ChessBoard.java
Normal file
@@ -0,0 +1,180 @@
|
||||
package chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import chess.model.visitor.KingIdentifier;
|
||||
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 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.lastMove = null;
|
||||
this.lastEjectedPiece = null;
|
||||
}
|
||||
|
||||
public void applyMove(Move move) {
|
||||
assert move.isValid() : "Invalid move !";
|
||||
Piece deadPiece = pieceAt(move.getFinish());
|
||||
if (deadPiece != null) {
|
||||
this.lastEjectedPiece = deadPiece;
|
||||
} else {
|
||||
this.lastEjectedPiece = null;
|
||||
}
|
||||
Piece movingPiece = pieceAt(move.getStart());
|
||||
pieceComes(movingPiece, move.getFinish());
|
||||
pieceLeaves(move.getStart());
|
||||
movingPiece.move();
|
||||
this.lastMove = move;
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
assert this.lastMove != null: "Can't undo at the beginning!";
|
||||
|
||||
undoMove(this.lastMove, this.lastEjectedPiece);
|
||||
}
|
||||
|
||||
public void undoMove(Move move, Piece deadPiece) {
|
||||
Piece movingPiece = pieceAt(move.getFinish());
|
||||
pieceComes(movingPiece, move.getStart());
|
||||
pieceComes(deadPiece, move.getFinish());
|
||||
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 (piece != null && kingIdentifier.visit(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 false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
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
@@ -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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
83
app/src/main/java/chess/model/Game.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.pieces.Pawn;
|
||||
|
||||
public class Game {
|
||||
private final ChessBoard board;
|
||||
private Color playerTurn;
|
||||
|
||||
public enum GameStatus {
|
||||
Check, CheckMate, OnGoing, Pat;
|
||||
}
|
||||
|
||||
public Game(ChessBoard board) {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public ChessBoard getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
public Color getPlayerTurn() {
|
||||
return playerTurn;
|
||||
}
|
||||
|
||||
public void resetPlayerTurn() {
|
||||
this.playerTurn = Color.White;
|
||||
}
|
||||
|
||||
public void switchPlayerTurn() {
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
}
|
||||
|
||||
public GameStatus checkGameStatus() {
|
||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||
|
||||
if (this.board.isKingInCheck(enemy))
|
||||
if (this.board.hasAllowedMoves(enemy))
|
||||
return GameStatus.Check;
|
||||
else
|
||||
return GameStatus.CheckMate;
|
||||
|
||||
if (!board.hasAllowedMoves(enemy))
|
||||
return GameStatus.Pat;
|
||||
|
||||
return GameStatus.OnGoing;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||
Coordinate pieceCoords = new Coordinate(x, enemyLineY);
|
||||
Piece piece = getBoard().pieceAt(pieceCoords);
|
||||
|
||||
if (piece == null || !(piece instanceof Pawn) || piece.getColor() != color)
|
||||
continue;
|
||||
|
||||
return pieceCoords;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
42
app/src/main/java/chess/model/Move.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package chess.model;
|
||||
|
||||
public class Move {
|
||||
private final Coordinate start;
|
||||
private final Coordinate finish;
|
||||
|
||||
public Move(Coordinate start, Coordinate finish) {
|
||||
this.start = start;
|
||||
this.finish = finish;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return this.start.isValid() && this.finish.isValid() && !this.start.equals(this.finish);
|
||||
}
|
||||
|
||||
public Coordinate getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Coordinate getFinish() {
|
||||
return finish;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
31
app/src/main/java/chess/model/Piece.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package chess.model;
|
||||
|
||||
public abstract class Piece {
|
||||
|
||||
private final Color color;
|
||||
private int moved;
|
||||
|
||||
public Piece(Color color) {
|
||||
this.color = color;
|
||||
this.moved = 0;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
this.moved++;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public boolean hasMoved() {
|
||||
return moved > 0;
|
||||
}
|
||||
|
||||
public void unMove() {
|
||||
this.moved--;
|
||||
}
|
||||
|
||||
public abstract <T> T accept(PieceVisitor<T> visitor);
|
||||
|
||||
}
|
||||
28
app/src/main/java/chess/model/PieceVisitor.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package chess.model;
|
||||
|
||||
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<T> {
|
||||
|
||||
default T visit(Piece piece) {
|
||||
return piece.accept(this);
|
||||
}
|
||||
|
||||
T visitPiece(Bishop bishop);
|
||||
|
||||
T visitPiece(King king);
|
||||
|
||||
T visitPiece(Knight knight);
|
||||
|
||||
T visitPiece(Pawn pawn);
|
||||
|
||||
T visitPiece(Queen queen);
|
||||
|
||||
T visitPiece(Rook rook);
|
||||
|
||||
}
|
||||
18
app/src/main/java/chess/model/pieces/Bishop.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class Bishop extends Piece {
|
||||
|
||||
public Bishop(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
}
|
||||
17
app/src/main/java/chess/model/pieces/King.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class King extends Piece {
|
||||
|
||||
public King(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/chess/model/pieces/Knight.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class Knight extends Piece {
|
||||
|
||||
public Knight(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
}
|
||||
21
app/src/main/java/chess/model/pieces/Pawn.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class Pawn extends Piece {
|
||||
|
||||
public Pawn(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
|
||||
public int multiplier() {
|
||||
return getColor() == Color.White ? 1 : -1;
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/chess/model/pieces/Queen.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class Queen extends Piece {
|
||||
|
||||
public Queen(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/chess/model/pieces/Rook.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package chess.model.pieces;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
|
||||
public class Rook extends Piece {
|
||||
|
||||
public Rook(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T accept(PieceVisitor<T> visitor) {
|
||||
return visitor.visitPiece(this);
|
||||
}
|
||||
}
|
||||
45
app/src/main/java/chess/model/visitor/KingIdentifier.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package chess.model.visitor;
|
||||
|
||||
import chess.model.Color;
|
||||
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;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
119
app/src/main/java/chess/model/visitor/PiecePathChecker.java
Normal file
@@ -0,0 +1,119 @@
|
||||
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) {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(bishop))
|
||||
return false;
|
||||
return testPath(bishop.getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean visitPiece(King king) {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(king))
|
||||
return false;
|
||||
|
||||
Piece destPiece = board.pieceAt(this.move.getFinish());
|
||||
if (destPiece == null)
|
||||
return true;
|
||||
return destPiece.getColor() != king.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean visitPiece(Knight knight) {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(knight))
|
||||
return false;
|
||||
|
||||
Piece destPiece = board.pieceAt(this.move.getFinish());
|
||||
if (destPiece == null)
|
||||
return true;
|
||||
return destPiece.getColor() != knight.getColor();
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
Piece destPiece = this.board.pieceAt(this.move.getFinish());
|
||||
if (destPiece == null)
|
||||
return false;
|
||||
|
||||
return destPiece.getColor() != pawn.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean visitPiece(Queen queen) {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(queen))
|
||||
return false;
|
||||
return testPath(queen.getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean visitPiece(Rook rook) {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(rook))
|
||||
return false;
|
||||
return testPath(rook.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;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,5 @@
|
||||
package chess.render;
|
||||
|
||||
public class VertexAttribPointer {
|
||||
public int index;
|
||||
public int size;
|
||||
public int offset;
|
||||
public record VertexAttribPointer(int index, int size, int offset) {
|
||||
|
||||
public VertexAttribPointer(int index, int size, int offset) {
|
||||
this.index = index;
|
||||
this.size = size;
|
||||
this.offset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package chess.render;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -42,9 +41,9 @@ public class VertexBuffer {
|
||||
|
||||
public void BindVertexAttribs() {
|
||||
for (VertexAttribPointer vertexAttribPointer : vertexAttribs) {
|
||||
GL30.glEnableVertexAttribArray(vertexAttribPointer.index);
|
||||
GL30.glVertexAttribPointer(vertexAttribPointer.index, vertexAttribPointer.size, GL_FLOAT, false,
|
||||
this.dataStride * 4, vertexAttribPointer.offset);
|
||||
GL30.glEnableVertexAttribArray(vertexAttribPointer.index());
|
||||
GL30.glVertexAttribPointer(vertexAttribPointer.index(), vertexAttribPointer.size(), GL_FLOAT, false,
|
||||
this.dataStride * 4, vertexAttribPointer.offset());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,6 @@ import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import chess.render.Camera;
|
||||
import chess.render.Renderer;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
@@ -59,7 +56,7 @@ public class Window {
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
|
||||
|
||||
// Create the window
|
||||
window = glfwCreateWindow(1000, 1000, "Chess4J", NULL, NULL);
|
||||
window = glfwCreateWindow(1000, 1000, "3DChess", NULL, NULL);
|
||||
if (window == NULL)
|
||||
throw new RuntimeException("Failed to create the GLFW window");
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ public class BoardShader extends ShaderProgram {
|
||||
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out vec3 pass_color;
|
||||
flat out vec3 pass_color;
|
||||
|
||||
void main(void){
|
||||
gl_Position = camMatrix * vec4(position, 1.0);
|
||||
@@ -23,7 +23,7 @@ public class BoardShader extends ShaderProgram {
|
||||
private static String fragmentShader = """
|
||||
#version 330
|
||||
|
||||
in vec3 pass_color;
|
||||
flat in vec3 pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
|
||||
75
app/src/main/java/chess/simplerender/PieceIcon.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package chess.simplerender;
|
||||
|
||||
import java.awt.Image;
|
||||
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;
|
||||
|
||||
public class PieceIcon implements PieceVisitor<String> {
|
||||
|
||||
private static final String basePath = "app/src/main/resources/pieces2D/";
|
||||
private static final Map<String, Icon> cache = new HashMap<>();
|
||||
|
||||
public Icon getIcon(Piece piece) {
|
||||
if (piece == null)
|
||||
return null;
|
||||
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
||||
return getIcon(path);
|
||||
}
|
||||
|
||||
private Icon getIcon(String path) {
|
||||
Icon image = cache.get(path);
|
||||
if (image != null)
|
||||
return image;
|
||||
image = new ImageIcon(new ImageIcon(path).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";
|
||||
}
|
||||
|
||||
}
|
||||
244
app/src/main/java/chess/simplerender/Window.java
Normal file
@@ -0,0 +1,244 @@
|
||||
package chess.simplerender;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandExecutor;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.io.commands.GetAllowedMovesCommand;
|
||||
import chess.io.commands.GetPieceAtCommand;
|
||||
import chess.io.commands.MoveCommand;
|
||||
import chess.io.commands.PromoteCommand;
|
||||
import chess.io.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
|
||||
public class Window extends JFrame implements OutputSystem {
|
||||
|
||||
private final JLabel cells[][];
|
||||
private final JLabel displayText;
|
||||
private final CommandExecutor commandExecutor;
|
||||
|
||||
private Coordinate lastClick = null;
|
||||
|
||||
public Window(CommandExecutor commandExecutor) {
|
||||
this.cells = new JLabel[8][8];
|
||||
this.displayText = new JLabel();
|
||||
this.commandExecutor = commandExecutor;
|
||||
setSize(800, 870);
|
||||
setVisible(true);
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private CommandResult sendCommand(Command command) {
|
||||
CommandResult result = this.commandExecutor.executeCommand(command);
|
||||
if (result == CommandResult.Moved) {
|
||||
updateBoard();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Color getCellColor(int x, int y) {
|
||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
||||
}
|
||||
|
||||
private void buildBoard() {
|
||||
JPanel content = new JPanel();
|
||||
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||
|
||||
content.add(this.displayText);
|
||||
content.add(grid);
|
||||
|
||||
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];
|
||||
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean previewMoves(int x, int y) {
|
||||
GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(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()];
|
||||
cell.setBackground(Color.CYAN);
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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("C'est au tour de " + color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void winnerIs(chess.model.Color color) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Victoire de " + color);
|
||||
this.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInCheck() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Échec !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInMat() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Échec et mat !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void patSituation() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Pat. Égalité !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hasSurrendered(chess.model.Color color) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Abandon de " + color);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gameStarted() {
|
||||
buildBoard();
|
||||
}
|
||||
|
||||
@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));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
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
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
app/src/main/resources/pieces2D/black-bishop.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
BIN
app/src/main/resources/pieces2D/black-king.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
app/src/main/resources/pieces2D/black-knight.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
app/src/main/resources/pieces2D/black-pawn.png
Normal file
|
After Width: | Height: | Size: 626 B |
BIN
app/src/main/resources/pieces2D/black-queen.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
app/src/main/resources/pieces2D/black-rook.png
Normal file
|
After Width: | Height: | Size: 589 B |
BIN
app/src/main/resources/pieces2D/white-bishop.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
app/src/main/resources/pieces2D/white-king.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
BIN
app/src/main/resources/pieces2D/white-knight.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
app/src/main/resources/pieces2D/white-pawn.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
app/src/main/resources/pieces2D/white-queen.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
app/src/main/resources/pieces2D/white-rook.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
@@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AppTest {
|
||||
@Test void appHasAGreeting() {
|
||||
App classUnderTest = new App();
|
||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
// App classUnderTest = new App();
|
||||
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
}
|
||||
}
|
||||
|
||||