class documentation - a shitload of it
All checks were successful
Linux arm64 / Build (push) Successful in 33s

This commit is contained in:
2025-05-18 20:08:22 +02:00
parent 523eb094e1
commit 97950403a5
85 changed files with 378 additions and 94 deletions

View File

@@ -4,6 +4,15 @@ import java.util.Scanner;
import chess.view.consolerender.Colors;
/**
* Main class of the chess game. Asks the user for the version to use and runs the according main.
* @author Grenier Lilas
* @author Pribylski Simon
* @see ConsoleMain
* @see SwingMain
* @see OpenGLMain
*/
public class App {
public static void main(String[] args) {
System.out.println(Colors.RED + "Credits: Grenier Lilas, Pribylski Simon." + Colors.RESET);

View File

@@ -3,6 +3,9 @@
*/
package chess;
/**
* Main class for the console version of the game.
*/
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.model.Game;

View File

@@ -6,6 +6,9 @@ import chess.model.Game;
import chess.pgn.PgnFileSimulator;
import chess.view.DDDrender.DDDView;
/**
* Main class for the 3D Window version of the game.
*/
public class OpenGLMain {
public static void main(String[] args) {
Game game = new Game();

View File

@@ -1,7 +1,7 @@
package chess;
import chess.ai.minimax.AlphaBetaAI;
import chess.ai.minimax.AlphaBetaConsolePrinter;
import chess.ai.ais.AlphaBetaAI;
import chess.ai.alphabeta.AlphaBetaConsolePrinter;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.controller.event.GameAdapter;
@@ -11,6 +11,9 @@ import chess.pgn.PgnExport;
import chess.view.audio.GameAudio;
import chess.view.simplerender.Window;
/**
* Main class for the 2D window version of the game.
*/
public class SwingMain {
public static void main(String[] args) {
Game game = new Game();

View File

@@ -10,7 +10,10 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Piece;
public abstract class AI extends GameAdapter {
/**
* Abstract class, used to code bots.
*/
public abstract class AI extends GameAdapter implements AIActions {
protected final CommandExecutor commandExecutor;
protected final Color color;
@@ -30,12 +33,9 @@ public abstract class AI extends GameAdapter {
play();
}
protected List<AIAction> getAllowedActions() {
return AIActions.getAllowedActions(this.commandExecutor);
}
protected Piece pieceAt(Coordinate coordinate) {
return AIActions.pieceAt(coordinate, this.commandExecutor);
@Override
public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
}
}

View File

@@ -1,4 +1,4 @@
package chess.ai.minimax;
package chess.ai;
import java.util.List;
@@ -14,15 +14,19 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Move;
import chess.model.PermissiveGame;
import chess.model.LightGame;
public class GameSimulation extends GameAdapter implements CommandSender {
/**
* Emulates the moves of a bot on a secondary board.
*/
public class GameSimulation extends GameAdapter implements AIActions {
private final CommandExecutor simulation;
private final Game gameSimulation;
public GameSimulation() {
this.gameSimulation = new PermissiveGame();
this.gameSimulation = new LightGame();
this.simulation = new CommandExecutor(gameSimulation, new EmptyGameDispatcher());
}
@@ -55,6 +59,7 @@ public class GameSimulation extends GameAdapter implements CommandSender {
sendUndo();
}
@Override
public CommandExecutor getCommandExecutor() {
return simulation;
}
@@ -71,10 +76,6 @@ public class GameSimulation extends GameAdapter implements CommandSender {
return this.gameSimulation.getPlayerTurn();
}
public List<AIAction> getAllowedActions() {
return AIActions.getAllowedActions(this.simulation);
}
public void close() {
this.simulation.close();
}

View File

@@ -10,6 +10,10 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Evaluate the cost of pieces for AI algorithm.
*/
public class PieceCost implements PieceVisitor<Float> {
private final Color player;

View File

@@ -16,6 +16,10 @@ import chess.model.pieces.Rook;
public class PiecePosCost implements PieceVisitor<List<Float>> {
/**
* Evaluate the cost of position for AI algorithm.
*/
private final Color color;
private static final List<Float> BISHOP = Arrays.asList(

View File

@@ -4,6 +4,9 @@ import chess.controller.CommandExecutor;
import chess.controller.CommandSender;
import chess.controller.commands.UndoCommand;
/**
* Abstract class, manage the possible actions of a bot.
*/
public abstract class AIAction implements CommandSender {
private final CommandExecutor commandExecutor;

View File

@@ -3,6 +3,9 @@ package chess.ai.actions;
import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
/**
* Manage the transition between model and bots when it comes to Castling command.
*/
public class AIActionCastling extends AIAction{
private final boolean bigCastling;

View File

@@ -4,6 +4,9 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.MoveCommand;
import chess.model.Move;
/**
* Manage the transition between model and bots when it comes to Move command.
*/
public class AIActionMove extends AIAction{
private final Move move;

View File

@@ -6,6 +6,9 @@ import chess.controller.commands.PromoteCommand;
import chess.controller.commands.PromoteCommand.PromoteType;
import chess.model.Move;
/**
* Manage the transition between model and bots when it comes to Move and Promotion command.
*/
public class AIActionMoveAndPromote extends AIAction{
private final Move move;

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import chess.controller.Command;
import chess.controller.CommandSender;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor;
import chess.controller.commands.GetAllowedCastlingsCommand;
@@ -17,41 +18,45 @@ import chess.model.Move;
import chess.model.Piece;
import chess.model.pieces.Pawn;
public class AIActions {
public static List<AIAction> getAllowedActions(CommandExecutor commandExecutor) {
List<Move> moves = getAllowedMoves(commandExecutor);
CastlingResult castlingResult = getAllowedCastlings(commandExecutor);
/**
* Search and compiles the different actions possible to a bot.
*/
public interface AIActions extends CommandSender {
default List<AIAction> getAllowedActions() {
List<Move> moves = getPlayerMoves();
CastlingResult castlingResult = getAllowedCastlings();
List<AIAction> actions = new ArrayList<>(moves.size() + 10);
for (Move move : moves) {
Piece movingPiece = pieceAt(move.getStart(), commandExecutor);
Piece movingPiece = getPieceAt(move.getStart());
if (movingPiece instanceof Pawn) {
int enemyLineY = movingPiece.getColor() == Color.White ? 0 : 7;
if (move.getFinish().getY() == enemyLineY) {
PromoteType[] promotes = PromoteType.values();
for (PromoteType promote : promotes) {
actions.add(new AIActionMoveAndPromote(commandExecutor, move, promote));
actions.add(new AIActionMoveAndPromote(getCommandExecutor(), move, promote));
}
continue;
}
}
actions.add(new AIActionMove(commandExecutor, move));
actions.add(new AIActionMove(getCommandExecutor(), move));
}
switch (castlingResult) {
case Both:
actions.add(new AIActionCastling(commandExecutor, true));
actions.add(new AIActionCastling(commandExecutor, false));
actions.add(new AIActionCastling(getCommandExecutor(), true));
actions.add(new AIActionCastling(getCommandExecutor(), false));
break;
case Small:
actions.add(new AIActionCastling(commandExecutor, false));
actions.add(new AIActionCastling(getCommandExecutor(), false));
break;
case Big:
actions.add(new AIActionCastling(commandExecutor, true));
actions.add(new AIActionCastling(getCommandExecutor(), true));
break;
case None:
@@ -61,27 +66,4 @@ public class AIActions {
return actions;
}
private static CastlingResult getAllowedCastlings(CommandExecutor commandExecutor) {
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2, commandExecutor);
return cmd2.getCastlingResult();
}
private static List<Move> getAllowedMoves(CommandExecutor commandExecutor) {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd, commandExecutor);
return cmd.getMoves();
}
private static CommandResult sendCommand(Command command, CommandExecutor commandExecutor) {
CommandResult result = commandExecutor.executeCommand(command);
assert result != CommandResult.NotAllowed : "Command not allowed!";
return result;
}
public static Piece pieceAt(Coordinate coordinate, CommandExecutor commandExecutor) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
commandExecutor.executeCommand(command);
return command.getPiece();
}
}

View File

@@ -1,4 +1,4 @@
package chess.ai.minimax;
package chess.ai.ais;
import java.util.ArrayList;
import java.util.List;
@@ -7,12 +7,17 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import chess.ai.AI;
import chess.ai.actions.AIAction;
import chess.ai.*;
import chess.ai.actions.*;
import chess.ai.alphabeta.*;
import chess.controller.CommandExecutor;
import chess.model.Color;
import common.Signal1;
/**
* Extends AI. Bot based on the alpha-beta method of resolution.
*/
public class AlphaBetaAI extends AI {
private final int searchDepth;
@@ -80,4 +85,9 @@ public class AlphaBetaAI extends AI {
move.applyAction();
}
@Override
public CommandExecutor getCommandExecutor() {
return super.getCommandExecutor();
}
}

View File

@@ -1,12 +1,17 @@
package chess.ai;
package chess.ai.ais;
import java.util.List;
import java.util.Random;
import chess.ai.*;
import chess.ai.actions.AIAction;
import chess.controller.CommandExecutor;
import chess.model.Color;
/**
* Bot, takes a random decision among his possible moves.
*/
public class DumbAI extends AI {
private final Random random = new Random();

View File

@@ -1,4 +1,4 @@
package chess.ai;
package chess.ai.ais;
import java.util.ArrayList;
import java.util.List;
@@ -10,6 +10,16 @@ import chess.controller.CommandExecutor;
import chess.model.Color;
import chess.model.Move;
import chess.model.Piece;
import chess.ai.*;
import chess.ai.actions.*;
import chess.ai.actions.AIActions;
/**
* Bot, takes the optimal piece when possible, or make a random move if no piece can be taken during the turn.
* @see PieceCost
* @see PiecePosCost
*/
public class HungryAI extends AI {
@@ -23,7 +33,7 @@ public class HungryAI extends AI {
}
private int getMoveCost(Move move) {
Piece piece = pieceAt(move.getDeadPieceCoords());
Piece piece = getPieceAt(move.getDeadPieceCoords());
return -(int) pieceCost.getCost(piece);
}

View File

@@ -1,5 +1,10 @@
package chess.ai.minimax;
package chess.ai.alphabeta;
import chess.ai.ais.AlphaBetaAI;
/**
* Print the action of an alpha-beta bot on the console.
* @see AlphaBetaAI
*/
public class AlphaBetaConsolePrinter {
private final AlphaBetaAI ai;

View File

@@ -1,4 +1,4 @@
package chess.ai.minimax;
package chess.ai.alphabeta;
import java.util.ArrayList;
import java.util.Collections;
@@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import chess.ai.GameSimulation;
import chess.ai.PieceCost;
import chess.ai.PiecePosCost;
import chess.ai.actions.AIAction;
@@ -14,6 +15,11 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Piece;
/**
* Manage the threads for an alpha-beta bot.
* @see AlphaBetaAI
*/
public class AlphaBetaThread extends Thread {
private final GameSimulation simulation;

View File

@@ -1,11 +1,16 @@
package chess.ai.minimax;
package chess.ai.alphabeta;
import java.util.concurrent.ThreadFactory;
import chess.ai.GameSimulation;
import chess.ai.actions.AIAction;
import chess.controller.CommandExecutor;
import chess.model.Color;
/**
* Create the threads for an alpha-beta bot.
* @see AlphaBetaAI
*/
public class AlphaBetaThreadCreator implements ThreadFactory{
private final Color color;

View File

@@ -3,18 +3,22 @@ package chess.controller;
import chess.controller.event.GameListener;
import chess.model.Game;
/**
* Abstract class, manage the execution and results of commands.
*/
public abstract class Command {
public enum CommandResult {
/**
* The command was successfull. Should update display and switch player turn.
* The command was successful. Should update display and switch player turn.
*/
Moved,
/** The command was successfull. Should not update anything */
/** The command was successful. Should not update anything */
NotMoved,
/** The command was successfull. Should only update display */
/** The command was successful. Should only update display */
ActionNeeded,
/** The command was not successfull */
/** The command was not successful */
NotAllowed;
}

View File

@@ -10,6 +10,9 @@ import chess.controller.event.GameListener;
import chess.model.Game;
import chess.model.Game.GameStatus;
/**
* Execute commands.
*/
public class CommandExecutor {
private Game game;

View File

@@ -20,6 +20,10 @@ import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
/**
* Interface for sending commands to the game, make long string of commands easier.
*/
public interface CommandSender {
CommandExecutor getCommandExecutor();

View File

@@ -3,6 +3,9 @@ package chess.controller;
import chess.controller.event.GameListener;
import chess.model.Game;
/**
* Abstract class, manage commands given by the player, which are the commands that can be undone.
*/
public abstract class PlayerCommand extends Command{
public CommandResult undo(Game game, GameListener outputSystem) {

View File

@@ -8,6 +8,9 @@ import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Move;
/**
* Command to execute a Castling or Big Castling.
*/
public class CastlingCommand extends PlayerCommand {
private Move kingMove;

View File

@@ -4,6 +4,9 @@ import chess.controller.Command;
import chess.controller.event.GameListener;
import chess.model.Game;
/**
* Command to check the possible Castling.
*/
public class GetAllowedCastlingsCommand extends Command{
public enum CastlingResult {

View File

@@ -10,6 +10,9 @@ import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Piece;
/**
* Command to check the Castlings that are possible.
*/
public class GetAllowedMovesPieceCommand extends Command {
private final Coordinate start;

View File

@@ -6,6 +6,9 @@ import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Piece;
/**
* Command to check the possible moves for a given piece.
*/
public class GetPieceAtCommand extends Command{
private final Coordinate pieceCoords;

View File

@@ -7,6 +7,11 @@ import chess.controller.event.GameListener;
import chess.model.Game;
import chess.model.Move;
/**
* Command to get the possible moves of a player. Castling and Promotion not included.
* @see CastlingCommand
* @see PromoteCommand
*/
public class GetPlayerMovesCommand extends Command {
private List<Move> moves;

View File

@@ -9,6 +9,11 @@ import chess.model.Move;
import chess.model.Piece;
import chess.model.rules.PiecePathChecker;
/*
* Command to move a piece. Castling not included.
*
* @see CastlingCommand
*/
public class MoveCommand extends PlayerCommand {
private final Move move;

View File

@@ -13,6 +13,9 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Command to create a new game.
*/
public class NewGameCommand extends Command {
public CommandResult execute(Game game, GameListener outputSystem) {
final ChessBoard board = game.getBoard();

View File

@@ -13,6 +13,9 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/*
* Command to promote a pawn.
*/
public class PromoteCommand extends PlayerCommand {
public enum PromoteType {

View File

@@ -5,6 +5,9 @@ import chess.controller.event.GameListener;
import chess.model.Color;
import chess.model.Game;
/**
* Command to surrender a game.
*/
public class SurrenderCommand extends Command {
private final Color player;

View File

@@ -5,6 +5,9 @@ import chess.controller.PlayerCommand;
import chess.controller.event.GameListener;
import chess.model.Game;
/**
* Command to undo the last action.
*/
public class UndoCommand extends Command{
@Override

View File

@@ -11,6 +11,9 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Create a thread in which the notifications such as Move Not Allowed, Check, Checkmate, Promotion, Surrender, etc. will be sent to update the players.
*/
public class AsyncGameDispatcher extends GameDispatcher {
private final List<GameListener> listeners;

View File

@@ -5,6 +5,9 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Dispatcher for bots, does nothing.
*/
public class EmptyGameDispatcher extends GameDispatcher {
@Override

View File

@@ -5,6 +5,10 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Abstract class, provides default implementation of GameListener methods.
* @see GameListener
*/
public abstract class GameAdapter implements GameListener {
@Override

View File

@@ -1,5 +1,8 @@
package chess.controller.event;
/**
* Abstract class, provides a dispatcher for game events.
*/
public abstract class GameDispatcher extends GameAdapter {
public abstract void addListener(GameListener listener);

View File

@@ -5,6 +5,9 @@ import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Interface for events to listen.
*/
public interface GameListener {
/**

View File

@@ -8,7 +8,13 @@ import chess.model.pieces.King;
import chess.model.pieces.Pawn;
import chess.model.rules.PiecePathChecker;
/**
* Chess board entity, composed of cells.
*/
public class ChessBoard {
/**
* Cell of the chess board.
*/
public static class Cell {
private Piece piece;

View File

@@ -1,5 +1,8 @@
package chess.model;
/**
* Enumeration of colors for player - either black or white
*/
public enum Color {
White,
Black;

View File

@@ -2,6 +2,9 @@ package chess.model;
import java.util.Objects;
/**
* Coordinate of a cell on the chess board.
*/
public class Coordinate {
private final int x;
private final int y;

View File

@@ -1,5 +1,9 @@
package chess.model;
/**
* Enumation of the different directions from a cell in a chessboard, and how to navigate the cells.
*/
public enum Direction {
Unset(65),

View File

@@ -8,6 +8,10 @@ import java.util.Stack;
import chess.controller.PlayerCommand;
import chess.controller.commands.MoveCommand;
/**
* Game class that represents a chess game.
*/
public class Game {
private final ChessBoard board;
private Color playerTurn;

View File

@@ -1,6 +1,12 @@
package chess.model;
public class PermissiveGame extends Game {
/**
* Game where the most ressource-consuming functions are empty.
* Mostly used by bots.
* @see GameSimulation
*/
public class LightGame extends Game {
@Override
public GameStatus checkGameStatus(Color color) {

View File

@@ -1,5 +1,9 @@
package chess.model;
/**
* Chess move, composed of a starting position, a finishing position and the eventual enemy pieces taken during the move.
*/
public class Move {
private final Coordinate start;
private final Coordinate finish;

View File

@@ -1,5 +1,9 @@
package chess.model;
/**
* Piece of a game. Posesses a color and the number of time it has been moved.
*/
public abstract class Piece {
private final Color color;

View File

@@ -7,6 +7,10 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Visitor interface for pieces.
*/
public interface PieceVisitor<T> {
default T visit(Piece piece) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* Bishop piece.
*/
public class Bishop extends Piece {
public Bishop(Color color) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* King piece.
*/
public class King extends Piece {
public King(Color color) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* Knight piece.
*/
public class Knight extends Piece {
public Knight(Color color) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* Pawn piece.
*/
public class Pawn extends Piece {
public Pawn(Color color) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* Queen piece.
*/
public class Queen extends Piece {
public Queen(Color color) {

View File

@@ -4,6 +4,9 @@ import chess.model.Color;
import chess.model.Piece;
import chess.model.PieceVisitor;
/**
* Rook piece.
*/
public class Rook extends Piece {
public Rook(Color color) {

View File

@@ -12,6 +12,10 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Check if a move can be done.
*/
public class PermissiveRuleChecker implements PieceVisitor<Boolean> {
private final Move move;

View File

@@ -14,6 +14,9 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Check if there are pieces in the path of a move.
*/
public class PiecePathChecker implements PieceVisitor<Boolean> {
private final ChessBoard board;

View File

@@ -19,6 +19,10 @@ import chess.model.Move;
import chess.model.Piece;
import chess.model.pieces.Pawn;
/**
* Export a game to PGN format.
*/
public class PgnExport {
// public static void main(String[] args) {

View File

@@ -3,6 +3,10 @@ package chess.pgn;
import chess.controller.CommandExecutor;
import common.AssetManager;
/**
* Apply moves from a pgn file at the start of a game.
*/
public class PgnFileSimulator extends PgnSimulator{
public PgnFileSimulator(CommandExecutor commandExecutor, String fileName) {

View File

@@ -25,6 +25,10 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Import a game from PGN format.
*/
public class PgnImport {
private static final Map<String, Class<? extends Piece>> pieceMap = Map.of(

View File

@@ -8,6 +8,10 @@ import chess.controller.PlayerCommand;
import chess.controller.event.GameAdapter;
import common.Signal0;
/**
* Apply moves from a pgn string at the start of a game.
*/
public class PgnSimulator extends GameAdapter {
private final CommandExecutor commandExecutor;

View File

@@ -8,6 +8,10 @@ import chess.model.pieces.Pawn;
import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
/**
* Visitor that returns the PGN name of a piece.
*/
public class PiecePgnName implements PieceVisitor<String> {
@Override

View File

@@ -5,6 +5,9 @@ import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector4f;
/**
* 3D camera
*/
public class Camera {
public static final float fov = 70.0f;
public static final float zNear = 0.01f;
@@ -66,6 +69,9 @@ public class Camera {
return new Matrix4f().lookAt(pos, center, up);
}
/**
* Performs a ray casting to find the selected cell by the cursor
*/
public Vector2f getCursorWorldFloorPos(Vector2f screenPos, int windowWidth, int windowHeight) {
float relativeX = (screenPos.x / (float) windowWidth * 2.0f) - 1.0f;
float relativeY = 1.0f - (screenPos.y / (float) windowHeight * 2.0f);

View File

@@ -6,6 +6,9 @@ import java.util.List;
import chess.view.DDDrender.opengl.VertexArray;
/**
* OpenGL model.
*/
public class DDDModel implements Closeable {
private final List<VertexArray> vaos;

View File

@@ -4,7 +4,10 @@ import org.joml.Vector2f;
import chess.model.Coordinate;
class DDDPlacement {
/**
* Helper functions to convert from 2D to 3D coordinates systems.
*/
public class DDDPlacement {
public static Vector2f coordinatesToVector(Coordinate coo) {
return coordinatesToVector(coo.getX(), coo.getY());
}

View File

@@ -73,46 +73,38 @@ public class DDDView extends GameAdapter implements CommandSender {
if (this.click == null) { // case: first click
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty()) { // case: no movement possible for piece
System.out.println("This piece cannot be moved at the moment.");
return;
}
setClick(coordinate);
previewMoves(coordinate);
// this.boardEntity.setCellColor(coordinate, BLUE);
System.out.println("First click on " + coordinate);
return;
}
// case: second click
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(this.click);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move
cancelPreview(this.click);
System.out.println("Nothing to do here.");
cancelClick();
return;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) { // case: no movement possible for piece
cancelPreview(this.click);
System.out.println("This piece cannot be moved at the moment.");
cancelClick();
return;
}
if (allowedMoves.contains(coordinate)) { // case: valid attempt to move
System.out.println("Move on " + coordinate);
cancelPreview(this.click);
sendMove(new Move(click, coordinate));
cancelClick();
return;
}
if (!(coordinate == this.click)) {
System.out.println("New click on " + coordinate); // cases: invalid move, selecting another piece
if (coordinate != this.click) { // cases: invalid move, selecting another piece
cancelPreview(this.click);
previewMoves(coordinate);
setClick(coordinate);
return;
}
System.out.println("Cancelling click."); // case: cancelling previous click
cancelClick();
cancelClick(); // case: cancelling previous click
}
private void previewMoves(Coordinate coordinate) {

View File

@@ -15,6 +15,9 @@ import chess.view.DDDrender.shader.BoardShader;
import chess.view.DDDrender.shader.PieceShader;
import chess.view.DDDrender.shader.ShaderProgram;
/**
* Basic 3D renderer
*/
public class Renderer implements Closeable {
private BoardShader boardShader;
private PieceShader pieceShader;

View File

@@ -37,6 +37,9 @@ import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
/**
* GLFW window.
*/
public class Window implements Closeable {
// The window handle

View File

@@ -15,6 +15,9 @@ import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
import chess.view.DDDrender.DDDModel;
/**
* Visitor that returns the 3d model of a piece.
*/
public class Piece3DModel implements PieceVisitor<String> {
private static final String basePath = "3d/";

View File

@@ -11,6 +11,9 @@ import chess.view.DDDrender.loader.BoardModelLoader;
import chess.view.DDDrender.opengl.VertexArray;
import chess.view.DDDrender.opengl.VertexBuffer;
/**
* Abstraction of the 3D model of the board.
*/
public class BoardEntity extends Entity {
private final VertexArray vao;

View File

@@ -6,6 +6,9 @@ import org.joml.Matrix4f;
import chess.view.DDDrender.Renderer;
/**
* Abstract class for all entities in the 3D view.
*/
public abstract class Entity implements Closeable{
public abstract void render(Renderer renderer);

View File

@@ -8,6 +8,9 @@ import org.joml.Vector3f;
import chess.view.DDDrender.DDDModel;
import chess.view.DDDrender.Renderer;
/**
* Generic type for 3D model that can be rendered.
*/
public class ModelEntity extends Entity {
protected Vector3f position;

View File

@@ -7,6 +7,9 @@ import org.joml.Vector3f;
import chess.model.Piece;
import chess.view.DDDrender.loader.Piece3DModel;
/**
* Model entity of a piece. Loads the correct model of the resources files for the kind of piece.
*/
public class PieceEntity extends ModelEntity {
private static final Piece3DModel modelLoader = new Piece3DModel();

View File

@@ -9,6 +9,9 @@ import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
/**
* Contains all 3D entities.
*/
public class World implements Closeable{
/** Renderable entity list */
private final List<Entity> entites;

View File

@@ -10,6 +10,10 @@ import java.net.URISyntaxException;
import common.AssetManager;
/**
* Manage audio files: download, save, and load them.
*/
public class AudioFiles {
private static final String baseURL = "https://images.chesscomfiles.com/chess-themes/sounds/_WAV_/default/";

View File

@@ -8,6 +8,9 @@ import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/**
* Audio player class to play sound files.
*/
public class AudioPlayer {
public static void playSound(InputStream audio) {
new Thread(new Runnable() {

View File

@@ -5,6 +5,9 @@ import chess.controller.event.GameAdapter;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Class to handle audio events in the game.
*/
public class GameAudio extends GameAdapter {
private final boolean functional;

View File

@@ -1,5 +1,8 @@
package chess.view.consolerender;
/**
* Colors class for console output, contains ANSI escape codes.
*/
public class Colors {
// Reset
public static final String RESET = "\u001B[0m"; // Text Reset
@@ -15,24 +18,24 @@ public class Colors {
public static final String CYAN = "\033[38;2;0;255;255m";
// Background
public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK
public static final String BLACK_BACKGROUND = "\033[40m";
public static final String LIGHT_GRAY_BACKGROUND = "\033[48;2;180;180;180m";
public static final String DARK_GRAY_BACKGROUND = "\033[48;2;120;120;120m";
public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE
public static final String RED_BACKGROUND = "\033[41m";
public static final String GREEN_BACKGROUND = "\033[42m";
public static final String YELLOW_BACKGROUND = "\033[43m";
public static final String BLUE_BACKGROUND = "\033[44m";
public static final String PURPLE_BACKGROUND = "\033[45m";
public static final String CYAN_BACKGROUND = "\033[46m";
public static final String WHITE_BACKGROUND = "\033[47m";
// 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
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m";
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m";
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m";
}

View File

@@ -17,6 +17,10 @@ import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Console renderer.
*/
public class Console extends GameAdapter implements CommandSender {
private final Scanner scanner = new Scanner(System.in);
private final CommandExecutor commandExecutor;

View File

@@ -5,6 +5,10 @@ import chess.model.Piece;
import chess.model.PieceVisitor;
import chess.model.pieces.*;
/**
* Manage appearance of pieces in the console.
*/
public class ConsolePieceName implements PieceVisitor<String> {
public String getString(Piece piece){

View File

@@ -19,6 +19,10 @@ import chess.model.pieces.Queen;
import chess.model.pieces.Rook;
import common.AssetManager;
/**
* Visitor that returns the png icon of a piece.
*/
public class PieceIcon implements PieceVisitor<String> {
private static final String basePath = "pieces2D/";

View File

@@ -24,6 +24,9 @@ import chess.controller.event.GameListener;
import chess.model.Coordinate;
import chess.model.Move;
/**
* Window for the 2D chess game.
*/
public class Window extends JFrame implements GameListener, CommandSender {
private final CommandExecutor commandExecutor;