Compare commits
10 Commits
87992b181e
...
failed
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ac7b4ad65 | |||
| 78d48fafc6 | |||
| ae55e0d94c | |||
| f54ddc1f59 | |||
|
|
ebb013cae5 | ||
|
|
076288a400 | ||
| 3f06a9eb17 | |||
| 2502d2c5d3 | |||
| 577ef0545f | |||
| edf95a1691 |
33
app/src/main/java/chess/OpenGLMain.java
Normal file
33
app/src/main/java/chess/OpenGLMain.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package chess;
|
||||
|
||||
import chess.ai.DumbAI;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.NewGameCommand;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Game;
|
||||
import chess.pgn.PgnExport;
|
||||
import chess.view.render.Window;
|
||||
|
||||
public class OpenGLMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Game game = new Game(new ChessBoard());
|
||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
Window window = new Window(commandExecutor);
|
||||
commandExecutor.addListener(window);
|
||||
|
||||
// DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||
// commandExecutor.addListener(ai);
|
||||
|
||||
// DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai2);
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
|
||||
window.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package chess;
|
||||
import chess.ai.DumbAI;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.NewGameCommand;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Game;
|
||||
import chess.pgn.PgnExport;
|
||||
import chess.view.simplerender.Window;
|
||||
|
||||
public class SwingMain {
|
||||
@@ -22,6 +24,13 @@ public class SwingMain {
|
||||
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||
commandExecutor.addListener(ai2);
|
||||
|
||||
commandExecutor.addListener(new GameAdaptator(){
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
System.out.println(PgnExport.exportGame(game));
|
||||
}
|
||||
});
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,13 @@ import java.util.Random;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.CastlingCommand;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.GetPlayerMovesCommand;
|
||||
import chess.controller.commands.MoveCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.Color;
|
||||
@@ -34,9 +37,46 @@ public class DumbAI extends GameAdaptator {
|
||||
|
||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||
sendCommand(cmd);
|
||||
|
||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd2);
|
||||
|
||||
CastlingResult castlings = cmd2.getCastlingResult();
|
||||
List<Move> moves = cmd.getMoves();
|
||||
|
||||
switch (castlings) {
|
||||
case Both: {
|
||||
int randomMove = this.random.nextInt(moves.size() + 2);
|
||||
if (randomMove < moves.size() - 2)
|
||||
break;
|
||||
this.commandExecutor.executeCommand(new CastlingCommand(randomMove == moves.size()));
|
||||
return;
|
||||
}
|
||||
|
||||
case Small: {
|
||||
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||
if (randomMove != moves.size())
|
||||
break;
|
||||
this.commandExecutor.executeCommand(new CastlingCommand(false));
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
case Big: {
|
||||
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||
if (randomMove != moves.size())
|
||||
break;
|
||||
this.commandExecutor.executeCommand(new CastlingCommand(true));
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
int randomMove = this.random.nextInt(moves.size());
|
||||
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,10 +90,10 @@ public class DumbAI extends GameAdaptator {
|
||||
}
|
||||
|
||||
private Piece pieceAt(Coordinate coordinate) {
|
||||
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||
sendCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||
sendCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
|
||||
private void sendCommand(Command command) {
|
||||
this.commandExecutor.executeCommand(command);
|
||||
|
||||
@@ -70,6 +70,7 @@ public class ChessBoard {
|
||||
pieceComes(movingPiece, move.getStart());
|
||||
pieceLeaves(move.getFinish());
|
||||
pieceComes(deadPiece, move.getDeadPieceCoords());
|
||||
assert movingPiece != null;
|
||||
movingPiece.unMove();
|
||||
}
|
||||
|
||||
@@ -279,14 +280,15 @@ public class ChessBoard {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int hashPlayerPieces(Color color) {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 0;
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Piece piece = pieceAt(new Coordinate(i, j));
|
||||
if (piece == null || piece.getColor() != color)
|
||||
if (piece == null)
|
||||
continue;
|
||||
result = Objects.hash(result, new Coordinate(i, j), piece);
|
||||
result = Objects.hash(result, piece.getColor(), new Coordinate(i, j), piece);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -300,4 +302,11 @@ public class ChessBoard {
|
||||
this.lastMove = lastMove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if (obj instanceof ChessBoard board)
|
||||
return board.hashCode() == this.hashCode();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class Game {
|
||||
private final ChessBoard board;
|
||||
private Color playerTurn;
|
||||
private final Stack<PlayerCommand> movesHistory;
|
||||
private final Map<Color, Map<Integer, Integer>> traitsPos;
|
||||
private final Map<Integer, Integer> traitsPos;
|
||||
|
||||
private static final int DRAW_REPETITONS = 3;
|
||||
|
||||
@@ -37,13 +37,6 @@ public class Game {
|
||||
public void reset() {
|
||||
resetPlayerTurn();
|
||||
this.traitsPos.clear();
|
||||
initPlayer(Color.Black);
|
||||
initPlayer(Color.White);
|
||||
}
|
||||
|
||||
private void initPlayer(Color color) {
|
||||
this.traitsPos.put(color, new HashMap<>());
|
||||
saveTraitPiecesPos(color);
|
||||
}
|
||||
|
||||
public void resetPlayerTurn() {
|
||||
@@ -55,32 +48,18 @@ public class Game {
|
||||
* @param color the current player turn
|
||||
* @return true if a draw should be declared
|
||||
*/
|
||||
private void saveTraitPiecesPos(Color color) {
|
||||
int piecesHash = this.board.hashPlayerPieces(color);
|
||||
Integer count = this.traitsPos.get(color).get(piecesHash);
|
||||
this.traitsPos.get(color).put(piecesHash, count == null ? 1 : count + 1);
|
||||
}
|
||||
|
||||
public void saveTraitPiecesPos() {
|
||||
saveTraitPiecesPos(playerTurn);
|
||||
int piecesHash = this.board.hashCode();
|
||||
Integer count = this.traitsPos.get(piecesHash);
|
||||
this.traitsPos.put(piecesHash, count == null ? 1 : count + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param player the trait to check
|
||||
* @return true if a position is repeated DRAW_REPETITONS times
|
||||
*/
|
||||
private boolean checkDraw(Color player) {
|
||||
return this.traitsPos.get(player).containsValue(DRAW_REPETITONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the game is in a draw situation
|
||||
*/
|
||||
private boolean checkDraw() {
|
||||
if (checkDraw(Color.White))
|
||||
return true;
|
||||
return checkDraw(Color.Black);
|
||||
return this.traitsPos.containsValue(DRAW_REPETITONS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,15 +108,11 @@ public class Game {
|
||||
}
|
||||
}
|
||||
|
||||
private void undoTraitPiecesPos(Color color) {
|
||||
int piecesHash = this.board.hashPlayerPieces(color);
|
||||
Integer count = this.traitsPos.get(color).get(piecesHash);
|
||||
if (count != null)
|
||||
this.traitsPos.get(color).put(piecesHash, count - 1);
|
||||
}
|
||||
|
||||
public void undoTraitPiecesPos() {
|
||||
undoTraitPiecesPos(Color.getEnemy(playerTurn));
|
||||
int piecesHash = this.board.hashCode();
|
||||
Integer count = this.traitsPos.get(piecesHash);
|
||||
if (count != null)
|
||||
this.traitsPos.put(piecesHash, count - 1);
|
||||
}
|
||||
|
||||
public List<PlayerCommand> getMoves() {
|
||||
|
||||
@@ -30,7 +30,7 @@ public class Move {
|
||||
int diffY = getFinish().getY() - getStart().getY();
|
||||
|
||||
assert Math.abs(diffX) < Coordinate.VALUE_MAX : "Move is too big!";
|
||||
assert Math.abs(diffX) < Coordinate.VALUE_MAX : "Move is too big!";
|
||||
assert Math.abs(diffY) < Coordinate.VALUE_MAX : "Move is too big!";
|
||||
|
||||
if (diffX == 0)
|
||||
return Math.abs(diffY);
|
||||
@@ -53,4 +53,11 @@ public class Move {
|
||||
return deadPieceCoords;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Move other)
|
||||
return this.start.equals(other.start) && this.finish.equals(other.finish);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,4 +28,7 @@ public abstract class Piece {
|
||||
|
||||
public abstract <T> T accept(PieceVisitor<T> visitor);
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object other);
|
||||
|
||||
}
|
||||
|
||||
@@ -20,4 +20,8 @@ public class Bishop extends Piece {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Bishop && ((Bishop) obj).getColor() == this.getColor());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ public class King extends Piece {
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof King && ((King) obj).getColor() == this.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ public class Knight extends Piece {
|
||||
public int hashCode() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Knight && ((Knight) obj).getColor() == this.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,8 @@ public class Pawn extends Piece {
|
||||
public int hashCode() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Pawn && ((Pawn) obj).getColor() == this.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ public class Queen extends Piece {
|
||||
public int hashCode() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Queen && ((Queen) obj).getColor() == this.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ public class Rook extends Piece {
|
||||
public int hashCode() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
return (other instanceof Rook && ((Rook) other).getColor() == this.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
||||
|
||||
Piece pieceToEat = this.board.pieceAt(lastMove.getFinish());
|
||||
|
||||
if (pieceToEat == null)
|
||||
if (pieceToEat == null || !(pieceToEat instanceof Pawn))
|
||||
return false;
|
||||
|
||||
Piece pawn = this.board.pieceAt(this.move.getStart());
|
||||
@@ -96,7 +96,14 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
||||
if (pieceToEat.getColor() == pawn.getColor())
|
||||
return false;
|
||||
|
||||
if (lastMove.getMiddle().equals(this.move.getFinish())
|
||||
Direction lastMoveDir = Direction.findDirection(lastMove);
|
||||
|
||||
if ((lastMoveDir != Direction.Front && lastMoveDir != Direction.Back) || lastMove.traversedCells() != 2)
|
||||
return false;
|
||||
|
||||
Coordinate middle = lastMove.getMiddle();
|
||||
|
||||
if (middle.equals(this.move.getFinish())
|
||||
&& new PawnIdentifier(pieceToEat.getColor()).isPawn(pieceToEat)) {
|
||||
this.move.setDeadPieceCoords(lastMove.getFinish());
|
||||
return true;
|
||||
|
||||
@@ -4,41 +4,188 @@ import java.util.List;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.PlayerCommand;
|
||||
import chess.controller.commands.CastlingCommand;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.GetPlayerMovesCommand;
|
||||
import chess.controller.commands.MoveCommand;
|
||||
import chess.controller.commands.NewGameCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.event.EmptyGameDispatcher;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
import chess.simulator.FoolCheckMate;
|
||||
import chess.model.pieces.Pawn;
|
||||
|
||||
public class PgnExport {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Game game = new Game(new ChessBoard());
|
||||
final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
// public static void main(String[] args) {
|
||||
// final Game game = new Game(new ChessBoard());
|
||||
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
FoolCheckMate foolCheckMate = new FoolCheckMate(commandExecutor);
|
||||
commandExecutor.addListener(foolCheckMate);
|
||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai1);
|
||||
|
||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||
// commandExecutor.addListener(ai1);
|
||||
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
|
||||
// commandExecutor.addListener(ai2);
|
||||
|
||||
// DumbAI ai2 = new DumbAI(commandExecutor, Color.Black);
|
||||
// commandExecutor.addListener(ai2);
|
||||
// commandExecutor.addListener(new GameAdaptator() {
|
||||
// @Override
|
||||
// public void onGameEnd() {
|
||||
// System.out.println(exportGame(game));
|
||||
// commandExecutor.close();
|
||||
// }
|
||||
// });
|
||||
|
||||
commandExecutor.addListener(new GameAdaptator() {
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
System.out.println(exportGame(game));
|
||||
commandExecutor.close();
|
||||
// commandExecutor.executeCommand(new NewGameCommand());
|
||||
// }
|
||||
|
||||
private static final PiecePgnName piecePgnName = new PiecePgnName();
|
||||
|
||||
private static Piece pieceAt(CommandExecutor commandExecutor, Coordinate coordinate) {
|
||||
GetPieceAtCommand cmd = new GetPieceAtCommand(coordinate);
|
||||
commandExecutor.executeCommand(cmd);
|
||||
return cmd.getPiece();
|
||||
}
|
||||
|
||||
private static List<Move> playerMoves(CommandExecutor commandExecutor) {
|
||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||
commandExecutor.executeCommand(cmd);
|
||||
return cmd.getMoves();
|
||||
}
|
||||
|
||||
private static String gameEnd(Game game) {
|
||||
switch (game.checkGameStatus()) {
|
||||
case Draw:
|
||||
case Pat:
|
||||
return "1/2-1/2";
|
||||
|
||||
case CheckMate:
|
||||
if (game.getPlayerTurn() == Color.White)
|
||||
return "1-0";
|
||||
return "0-1";
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String resolveAmbiguity(CommandExecutor cmdExec, Move pieceMove) {
|
||||
Piece movingPiece = pieceAt(cmdExec, pieceMove.getStart());
|
||||
|
||||
assert movingPiece != null;
|
||||
|
||||
if (movingPiece instanceof Pawn)
|
||||
return "";
|
||||
|
||||
List<Move> moves = playerMoves(cmdExec);
|
||||
|
||||
for (Move move : moves) {
|
||||
if (move.equals(pieceMove) || !move.getFinish().equals(pieceMove.getFinish()))
|
||||
continue;
|
||||
|
||||
Piece otherPiece = pieceAt(cmdExec, move.getStart());
|
||||
|
||||
// checking type of piece
|
||||
if (otherPiece.hashCode() != movingPiece.hashCode())
|
||||
continue;
|
||||
|
||||
String startPos = toString(pieceMove.getStart());
|
||||
|
||||
if (move.getStart().getX() != pieceMove.getStart().getX())
|
||||
// not on the same column
|
||||
return Character.toString(startPos.charAt(0));
|
||||
else
|
||||
return Character.toString(startPos.charAt(1));
|
||||
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String capture(MoveCommand move, Piece movingPiece) {
|
||||
String result = "";
|
||||
if (move.getDeadPiece() != null) {
|
||||
if (movingPiece instanceof Pawn) {
|
||||
result += toString(move.getMove().getStart()).charAt(0);
|
||||
}
|
||||
});
|
||||
result += "x";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
private static String promote(PlayerCommand nextCommand) {
|
||||
if (nextCommand != null && nextCommand instanceof PromoteCommand promoteCommand) {
|
||||
String result = "=";
|
||||
result += switch (promoteCommand.getPromoteType()) {
|
||||
case Bishop -> "B";
|
||||
case Knight -> "N";
|
||||
case Queen -> "Q";
|
||||
case Rook -> "R";
|
||||
};
|
||||
return result;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String castling(CastlingCommand castlingCommand) {
|
||||
String result = "O-O";
|
||||
if (castlingCommand.isBigCastling())
|
||||
result += "-O";
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String checkCheckMate(Game game) {
|
||||
switch (game.checkGameStatus()) {
|
||||
case CheckMate:
|
||||
return "#";
|
||||
|
||||
case Check:
|
||||
return "+";
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String printMove(PlayerCommand cmd, PlayerCommand nextCommand, Game virtualGame,
|
||||
CommandExecutor executor) {
|
||||
String result = "";
|
||||
if (cmd instanceof MoveCommand move) {
|
||||
|
||||
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||
|
||||
assert movingPiece != null;
|
||||
|
||||
// piece name
|
||||
result += piecePgnName.visit(movingPiece);
|
||||
|
||||
// ambiguious start
|
||||
result += resolveAmbiguity(executor, move.getMove());
|
||||
|
||||
// capture
|
||||
result += capture(move, movingPiece);
|
||||
|
||||
// end cell
|
||||
result += toString(move.getMove().getFinish());
|
||||
|
||||
// promote
|
||||
result += promote(nextCommand);
|
||||
|
||||
} else if (cmd instanceof CastlingCommand castlingCommand) {
|
||||
result += castling(castlingCommand);
|
||||
}
|
||||
|
||||
executor.executeCommand(cmd);
|
||||
|
||||
// check or checkmate
|
||||
result += checkCheckMate(virtualGame);
|
||||
|
||||
result += " ";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String exportGame(Game game) {
|
||||
@@ -50,40 +197,23 @@ public class PgnExport {
|
||||
executor.executeCommand(new NewGameCommand());
|
||||
|
||||
List<PlayerCommand> commands = game.getMoves();
|
||||
PiecePgnName piecePgnName = new PiecePgnName();
|
||||
String result = "";
|
||||
|
||||
int tour = 1;
|
||||
|
||||
for (int i = 0; i < commands.size(); i++) {
|
||||
PlayerCommand cmd = commands.get(i);
|
||||
if (cmd instanceof MoveCommand move) {
|
||||
if (virtualGame.getPlayerTurn() == Color.White) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
Piece movingPiece = virtualGame.getBoard().pieceAt(move.getMove().getStart());
|
||||
result += piecePgnName.visit(movingPiece);
|
||||
if (move.getDeadPiece() != null)
|
||||
result += "x";
|
||||
result += toString(move.getMove().getFinish());
|
||||
PlayerCommand nextCommand = null;
|
||||
if (i != commands.size() - 1) {
|
||||
nextCommand = commands.get(i + 1);
|
||||
}
|
||||
executor.executeCommand(cmd);
|
||||
switch (virtualGame.checkGameStatus()) {
|
||||
case CheckMate:
|
||||
result += "#";
|
||||
break;
|
||||
|
||||
case Check:
|
||||
result += "+";
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
if (virtualGame.getPlayerTurn() == Color.White) {
|
||||
result += tour + ".";
|
||||
tour++;
|
||||
}
|
||||
result += " ";
|
||||
result += printMove(cmd, nextCommand, virtualGame, executor);
|
||||
}
|
||||
return result;
|
||||
return result + " " + gameEnd(virtualGame);
|
||||
}
|
||||
|
||||
public static String toString(Coordinate coordinate) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package chess.view.render;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
import chess.view.render.shader.BoardShader;
|
||||
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
@@ -59,16 +60,42 @@ public class Renderer {
|
||||
return positions;
|
||||
}
|
||||
|
||||
private Coordinate GetCellFromColor(Vector3f color) {
|
||||
int offset = 1;
|
||||
|
||||
if (color.x > 0.5) {
|
||||
color = new Vector3f(1.0f, 1.0f, 1.0f).sub(color);
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
int r = (int) (color.x * 255.0f);
|
||||
int g = (int) (color.y * 255.0f);
|
||||
int b = (int) (color.z * 255.0f);
|
||||
|
||||
int index = (r + g + b) * 2 + offset;
|
||||
|
||||
return Coordinate.fromIndex(index);
|
||||
}
|
||||
|
||||
private Vector3f GetCellColor(int x, int y) {
|
||||
float index = (y * BOARD_WIDTH + x) / 2.0f;
|
||||
float r = (int) (index / 3) / 255.0f;
|
||||
float g = (int) ((index + 1) / 3) / 255.0f;
|
||||
float b = (int) ((index + 2) / 3) / 255.0f;
|
||||
if ((x + y) % 2 != 0) {
|
||||
System.out.println(GetCellFromColor(new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f)));
|
||||
return new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f);
|
||||
} else {
|
||||
System.out.println(GetCellFromColor(new Vector3f(r, g, b)));
|
||||
return new Vector3f(r, g, b);
|
||||
}
|
||||
}
|
||||
|
||||
private float[] GetBoardColors() {
|
||||
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||
Vector3f color;
|
||||
if ((i + j) % 2 != 0) {
|
||||
color = new Vector3f(1.0f, 1.0f, 1.0f);
|
||||
} else {
|
||||
color = new Vector3f(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||
Vector3f color = GetCellColor(i, j);
|
||||
int squareIndex = i * BOARD_WIDTH + j;
|
||||
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
||||
@@ -109,6 +136,12 @@ public class Renderer {
|
||||
this.vao.Unbind();
|
||||
}
|
||||
|
||||
public Coordinate GetSelectedCell() {
|
||||
float pixels[] = new float[3];
|
||||
GL30.glReadPixels(500, 500, 1, 1, GL_RGB, GL_FLOAT, pixels);
|
||||
return GetCellFromColor(new Vector3f(pixels[0], pixels[1], pixels[2]));
|
||||
}
|
||||
|
||||
public void Render(Camera cam) {
|
||||
this.shader.Start();
|
||||
this.shader.SetCamMatrix(cam.getMatrix());
|
||||
|
||||
@@ -5,6 +5,12 @@ import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.event.GameListener;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
@@ -13,17 +19,20 @@ import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
public class Window {
|
||||
public class Window implements GameListener{
|
||||
|
||||
// The window handle
|
||||
private long window;
|
||||
|
||||
private final CommandExecutor commandExecutor;
|
||||
|
||||
private Renderer renderer;
|
||||
private Camera cam;
|
||||
|
||||
public Window() {
|
||||
public Window(CommandExecutor commandExecutor) {
|
||||
this.renderer = new Renderer();
|
||||
this.cam = new Camera();
|
||||
this.commandExecutor = new CommandExecutor();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -114,6 +123,8 @@ public class Window {
|
||||
|
||||
render();
|
||||
|
||||
System.out.println(this.renderer.GetSelectedCell());
|
||||
|
||||
glfwSwapBuffers(window); // swap the color buffers
|
||||
|
||||
// Poll for window events. The key callback above will only be
|
||||
@@ -132,4 +143,82 @@ public class Window {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBoardUpdate() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onBoardUpdate'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onDraw'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onGameEnd'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGameStart() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onGameStart'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInCheck() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onKingInCheck'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKingInMat() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onKingInMat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMove(Move move) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onMove'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoveNotAllowed(Move move) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onMoveNotAllowed'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatSituation() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onPatSituation'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onPlayerTurn'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPromotePawn(Coordinate pieceCoords) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onPromotePawn'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurrender(Color coward) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onSurrender'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWin(Color winner) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'onWin'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,12 +3,57 @@
|
||||
*/
|
||||
package chess;
|
||||
|
||||
import chess.ai.DumbAI;
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.NewGameCommand;
|
||||
import chess.controller.commands.UndoCommand;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
import chess.model.*;
|
||||
import chess.model.pieces.*;
|
||||
import chess.simulator.Simulator;
|
||||
import chess.view.simplerender.Window;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class AppTest {
|
||||
@Test void appHasAGreeting() {
|
||||
// App classUnderTest = new App();
|
||||
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
@Test void functionalRollback(){
|
||||
Game game = new Game(new ChessBoard());
|
||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||
commandExecutor.addListener(ai);
|
||||
|
||||
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||
commandExecutor.addListener(ai2);
|
||||
|
||||
commandExecutor.addListener(new GameAdaptator() {
|
||||
@Override
|
||||
public void onGameEnd() {
|
||||
|
||||
Command.CommandResult result;
|
||||
do {
|
||||
result = commandExecutor.executeCommand(new UndoCommand());
|
||||
} while (result != Command.CommandResult.NotAllowed);
|
||||
|
||||
Game initialGame = new Game(new ChessBoard());
|
||||
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
|
||||
assert(game.getBoard().equals(initialGame.getBoard()));
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
|
||||
}
|
||||
|
||||
@Test void functionalRollbacks(){
|
||||
for (int i=0; i<100; i++) {
|
||||
functionalRollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user