Compare commits
1 Commits
dd6e033528
...
failed
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ac7b4ad65 |
@@ -5,17 +5,26 @@ package chess;
|
|||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
|
import chess.simulator.PromoteTest;
|
||||||
import chess.view.consolerender.Console;
|
import chess.view.consolerender.Console;
|
||||||
|
|
||||||
public class ConsoleMain {
|
public class ConsoleMain {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Game game = new Game();
|
Game game = new Game(new ChessBoard());
|
||||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
PromoteTest promoteTest = new PromoteTest(commandExecutor);
|
||||||
|
commandExecutor.addListener(promoteTest);
|
||||||
|
|
||||||
Console console = new Console(commandExecutor);
|
Console console = new Console(commandExecutor);
|
||||||
commandExecutor.addListener(console);
|
commandExecutor.addListener(console);
|
||||||
|
|
||||||
|
promoteTest.onComplete.connect(() -> {
|
||||||
|
console.setCaptureInput(true);
|
||||||
|
});
|
||||||
|
|
||||||
commandExecutor.executeCommand(new NewGameCommand());
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
app/src/main/java/chess/OpenGLMain.java
Normal file
33
app/src/main/java/chess/OpenGLMain.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package chess;
|
||||||
|
|
||||||
|
import chess.ai.DumbAI;
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.commands.NewGameCommand;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.pgn.PgnExport;
|
||||||
|
import chess.view.render.Window;
|
||||||
|
|
||||||
|
public class OpenGLMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Game game = new Game(new ChessBoard());
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
Window window = new Window(commandExecutor);
|
||||||
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
|
// DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
// commandExecutor.addListener(ai);
|
||||||
|
|
||||||
|
// DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
// commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
window.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import chess.ai.AlphaBetaAI;
|
|
||||||
import chess.ai.DumbAI;
|
import chess.ai.DumbAI;
|
||||||
import chess.ai.HungryAI;
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.pgn.PgnExport;
|
import chess.pgn.PgnExport;
|
||||||
@@ -13,21 +12,18 @@ import chess.view.simplerender.Window;
|
|||||||
|
|
||||||
public class SwingMain {
|
public class SwingMain {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Game game = new Game();
|
Game game = new Game(new ChessBoard());
|
||||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
Window window = new Window(commandExecutor, false);
|
Window window = new Window(commandExecutor, false);
|
||||||
commandExecutor.addListener(window);
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
DumbAI ai = new DumbAI(commandExecutor, Color.White);
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
commandExecutor.addListener(ai);
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
AlphaBetaAI ai2 = new AlphaBetaAI(commandExecutor, Color.Black, 3);
|
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
commandExecutor.addListener(ai2);
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
// Window window2 = new Window(ai2.getSimulation(), false);
|
|
||||||
// ai2.getSimulation().addListener(window2);
|
|
||||||
|
|
||||||
commandExecutor.addListener(new GameAdaptator(){
|
commandExecutor.addListener(new GameAdaptator(){
|
||||||
@Override
|
@Override
|
||||||
public void onGameEnd() {
|
public void onGameEnd() {
|
||||||
|
|||||||
@@ -1,81 +0,0 @@
|
|||||||
package chess.ai;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import chess.controller.Command;
|
|
||||||
import chess.controller.CommandExecutor;
|
|
||||||
import chess.controller.Command.CommandResult;
|
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
|
||||||
import chess.controller.commands.GetPlayerMovesCommand;
|
|
||||||
import chess.controller.commands.GetAllowedCastlingsCommand;
|
|
||||||
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
|
||||||
import chess.controller.event.GameAdaptator;
|
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Coordinate;
|
|
||||||
import chess.model.Move;
|
|
||||||
import chess.model.Piece;
|
|
||||||
|
|
||||||
public abstract class AI extends GameAdaptator{
|
|
||||||
|
|
||||||
protected final CommandExecutor commandExecutor;
|
|
||||||
protected final Color color;
|
|
||||||
|
|
||||||
public AI(CommandExecutor commandExecutor, Color color) {
|
|
||||||
this.commandExecutor = commandExecutor;
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void play();
|
|
||||||
protected abstract void promote(Coordinate pawnCoords);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPlayerTurn(Color color, boolean undone) {
|
|
||||||
if (this.color != color || undone)
|
|
||||||
return;
|
|
||||||
|
|
||||||
play();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPromotePawn(Coordinate pieceCoords) {
|
|
||||||
Piece pawn = pieceAt(pieceCoords);
|
|
||||||
if (pawn.getColor() != this.color)
|
|
||||||
return;
|
|
||||||
promote(pieceCoords);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Piece pieceAt(Coordinate coordinate) {
|
|
||||||
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
|
||||||
sendCommand(command);
|
|
||||||
return command.getPiece();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected List<Move> getAllowedMoves() {
|
|
||||||
return getAllowedMoves(this.commandExecutor);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected List<Move> getAllowedMoves(CommandExecutor commandExecutor) {
|
|
||||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
|
||||||
sendCommand(cmd, commandExecutor);
|
|
||||||
return cmd.getMoves();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected CastlingResult getAllowedCastlings() {
|
|
||||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
|
||||||
sendCommand(cmd2);
|
|
||||||
return cmd2.getCastlingResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected CommandResult sendCommand(Command command) {
|
|
||||||
return sendCommand(command, this.commandExecutor);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected CommandResult sendCommand(Command command, CommandExecutor commandExecutor) {
|
|
||||||
CommandResult result = commandExecutor.executeCommand(command);
|
|
||||||
if(result == CommandResult.NotAllowed){
|
|
||||||
System.out.println("eeeeee");
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,162 +0,0 @@
|
|||||||
package chess.ai;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
|
||||||
import chess.controller.commands.CastlingCommand;
|
|
||||||
import chess.controller.commands.MoveCommand;
|
|
||||||
import chess.controller.commands.NewGameCommand;
|
|
||||||
import chess.controller.commands.PromoteCommand;
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.controller.event.EmptyGameDispatcher;
|
|
||||||
import chess.controller.commands.UndoCommand;
|
|
||||||
import chess.model.ChessBoard;
|
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Coordinate;
|
|
||||||
import chess.model.Game;
|
|
||||||
import chess.model.Move;
|
|
||||||
|
|
||||||
public class AlphaBetaAI extends AI {
|
|
||||||
|
|
||||||
private final int searchDepth;
|
|
||||||
private final PieceCost pieceCost;
|
|
||||||
private final CommandExecutor simulation;
|
|
||||||
private final Game gameSimulation;
|
|
||||||
|
|
||||||
private final int GREAT_MOVE = -9999;
|
|
||||||
private final int HORRIBLE_MOVE = -GREAT_MOVE;
|
|
||||||
|
|
||||||
public AlphaBetaAI(CommandExecutor commandExecutor, Color color, int searchDepth) {
|
|
||||||
super(commandExecutor, color);
|
|
||||||
this.searchDepth = searchDepth;
|
|
||||||
this.pieceCost = new PieceCost(color);
|
|
||||||
this.gameSimulation = new Game();
|
|
||||||
this.simulation = new CommandExecutor(this.gameSimulation, new EmptyGameDispatcher());
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandExecutor getSimulation() {
|
|
||||||
return simulation;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getBoardEvaluation() {
|
|
||||||
final ChessBoard board = this.gameSimulation.getBoard();
|
|
||||||
int result = 0;
|
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
|
||||||
result += pieceCost.getCost(board.pieceAt(new Coordinate(i, j)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.gameSimulation.getPlayerTurn() != color)
|
|
||||||
return -result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getEndGameEvaluation() {
|
|
||||||
Color currentTurn = this.gameSimulation.getPlayerTurn();
|
|
||||||
if (currentTurn == this.color) {
|
|
||||||
return HORRIBLE_MOVE;
|
|
||||||
} else {
|
|
||||||
if (this.gameSimulation.getBoard().isKingInCheck(currentTurn))
|
|
||||||
return GREAT_MOVE;
|
|
||||||
return getBoardEvaluation() - PieceCost.PAWN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void simulateMove(Move move) {
|
|
||||||
sendCommand(new MoveCommand(move), this.simulation);
|
|
||||||
if (this.gameSimulation.getBoard().pawnShouldBePromoted())
|
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void simulateUndo() {
|
|
||||||
sendCommand(new UndoCommand(), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int negaMax(int depth, int alpha, int beta) {
|
|
||||||
if (depth == 0)
|
|
||||||
return getBoardEvaluation();
|
|
||||||
|
|
||||||
int value = Integer.MIN_VALUE;
|
|
||||||
|
|
||||||
List<Move> moves = getAllowedMoves(this.simulation);
|
|
||||||
|
|
||||||
if (moves.isEmpty())
|
|
||||||
return getEndGameEvaluation();
|
|
||||||
|
|
||||||
for (Move move : moves) {
|
|
||||||
simulateMove(move);
|
|
||||||
value = Integer.max(value, -negaMax(depth - 1, -beta, -alpha));
|
|
||||||
simulateUndo();
|
|
||||||
alpha = Integer.max(alpha, value);
|
|
||||||
if (alpha >= beta)
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Move getBestMove() {
|
|
||||||
List<Move> moves = getAllowedMoves(this.simulation);
|
|
||||||
int bestMove = Integer.MIN_VALUE;
|
|
||||||
List<Move> bestMoves = new ArrayList<>(20);
|
|
||||||
|
|
||||||
for (Move move : moves) {
|
|
||||||
simulateMove(move);
|
|
||||||
int value = -negaMax(this.searchDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
|
||||||
simulateUndo();
|
|
||||||
if (value > bestMove) {
|
|
||||||
bestMove = value;
|
|
||||||
bestMoves.clear();
|
|
||||||
bestMoves.add(move);
|
|
||||||
} else if (value == bestMove) {
|
|
||||||
bestMoves.add(move);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Best move : " + bestMove + " count : " + bestMoves.size());
|
|
||||||
|
|
||||||
return bestMoves.get(new Random().nextInt(bestMoves.size()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGameStart() {
|
|
||||||
sendCommand(new NewGameCommand(), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGameEnd() {
|
|
||||||
this.simulation.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {
|
|
||||||
sendCommand(new PromoteCommand(promotion), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {
|
|
||||||
sendCommand(new CastlingCommand(bigCastling), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMove(Move move) {
|
|
||||||
sendCommand(new MoveCommand(move), this.simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void play() {
|
|
||||||
long current = System.currentTimeMillis();
|
|
||||||
Move move = getBestMove();
|
|
||||||
long elapsed = System.currentTimeMillis() - current;
|
|
||||||
System.out.println("Took " + elapsed + "ms");
|
|
||||||
sendCommand(new MoveCommand(move));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void promote(Coordinate pawnCoords) {
|
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -3,28 +3,46 @@ package chess.ai;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.CastlingCommand;
|
import chess.controller.commands.CastlingCommand;
|
||||||
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||||
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.GetPlayerMovesCommand;
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.MoveCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
|
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
|
import chess.controller.event.GameAdaptator;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class DumbAI extends AI {
|
public class DumbAI extends GameAdaptator {
|
||||||
|
|
||||||
|
private final Color player;
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
|
|
||||||
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
||||||
super(commandExecutor, color);
|
this.player = color;
|
||||||
|
this.commandExecutor = commandExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void play() {
|
public void onPlayerTurn(Color color) {
|
||||||
CastlingResult castlings = getAllowedCastlings();
|
if (color != player)
|
||||||
List<Move> moves = getAllowedMoves();
|
return;
|
||||||
|
|
||||||
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||||
|
sendCommand(cmd);
|
||||||
|
|
||||||
|
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||||
|
sendCommand(cmd2);
|
||||||
|
|
||||||
|
CastlingResult castlings = cmd2.getCastlingResult();
|
||||||
|
List<Move> moves = cmd.getMoves();
|
||||||
|
|
||||||
switch (castlings) {
|
switch (castlings) {
|
||||||
case Both: {
|
case Both: {
|
||||||
@@ -35,12 +53,20 @@ public class DumbAI extends AI {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
case Small:
|
case Small: {
|
||||||
|
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||||
|
if (randomMove != moves.size())
|
||||||
|
break;
|
||||||
|
this.commandExecutor.executeCommand(new CastlingCommand(false));
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
case Big: {
|
case Big: {
|
||||||
int randomMove = this.random.nextInt(moves.size() + 1);
|
int randomMove = this.random.nextInt(moves.size() + 1);
|
||||||
if (randomMove != moves.size())
|
if (randomMove != moves.size())
|
||||||
break;
|
break;
|
||||||
this.commandExecutor.executeCommand(new CastlingCommand(castlings == CastlingResult.Big));
|
this.commandExecutor.executeCommand(new CastlingCommand(true));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,12 +76,27 @@ public class DumbAI extends AI {
|
|||||||
|
|
||||||
int randomMove = this.random.nextInt(moves.size());
|
int randomMove = this.random.nextInt(moves.size());
|
||||||
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void promote(Coordinate pawnCoords) {
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
Piece pawn = pieceAt(pieceCoords);
|
||||||
|
if (pawn.getColor() != this.player)
|
||||||
|
return;
|
||||||
|
|
||||||
int promote = this.random.nextInt(PromoteType.values().length);
|
int promote = this.random.nextInt(PromoteType.values().length);
|
||||||
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
|
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(Coordinate coordinate) {
|
||||||
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||||
|
sendCommand(command);
|
||||||
|
return command.getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCommand(Command command) {
|
||||||
|
this.commandExecutor.executeCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
package chess.ai;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
|
||||||
import chess.controller.commands.MoveCommand;
|
|
||||||
import chess.controller.commands.PromoteCommand;
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Coordinate;
|
|
||||||
import chess.model.Move;
|
|
||||||
import chess.model.Piece;
|
|
||||||
|
|
||||||
public class HungryAI extends AI {
|
|
||||||
|
|
||||||
private final PieceCost pieceCost;
|
|
||||||
private final Random random;
|
|
||||||
|
|
||||||
public HungryAI(CommandExecutor commandExecutor, Color color) {
|
|
||||||
super(commandExecutor, color);
|
|
||||||
this.pieceCost = new PieceCost(color);
|
|
||||||
this.random = new Random();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getMoveCost(Move move) {
|
|
||||||
Piece piece = pieceAt(move.getDeadPieceCoords());
|
|
||||||
return pieceCost.getCost(piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Move> getBestMoves() {
|
|
||||||
List<Move> moves = getAllowedMoves();
|
|
||||||
List<Move> bestMoves = new ArrayList<>();
|
|
||||||
int bestCost = 0;
|
|
||||||
for (Move move : moves) {
|
|
||||||
int moveCost = getMoveCost(move);
|
|
||||||
if (moveCost == bestCost) {
|
|
||||||
bestMoves.add(move);
|
|
||||||
} else if (moveCost > bestCost) {
|
|
||||||
bestMoves.clear();
|
|
||||||
bestMoves.add(move);
|
|
||||||
bestCost = moveCost;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bestMoves;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void play() {
|
|
||||||
List<Move> bestMoves = getBestMoves();
|
|
||||||
int randomMove = this.random.nextInt(bestMoves.size());
|
|
||||||
this.commandExecutor.executeCommand(new MoveCommand(bestMoves.get(randomMove)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void promote(Coordinate pawnCoords) {
|
|
||||||
sendCommand(new PromoteCommand(PromoteType.Queen));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package chess.ai;
|
|
||||||
|
|
||||||
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 PieceCost implements PieceVisitor<Integer> {
|
|
||||||
|
|
||||||
private final Color player;
|
|
||||||
|
|
||||||
public static final int BISHOP = 3;
|
|
||||||
public static final int KING = 90;
|
|
||||||
public static final int KNIGHT = 3;
|
|
||||||
public static final int PAWN = 1;
|
|
||||||
public static final int QUEEN = 9;
|
|
||||||
public static final int ROOK = 5;
|
|
||||||
|
|
||||||
public PieceCost(Color color) {
|
|
||||||
this.player = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCost(Piece piece) {
|
|
||||||
if (piece == null)
|
|
||||||
return 0;
|
|
||||||
int cost = visit(piece);
|
|
||||||
if (piece.getColor() != player)
|
|
||||||
cost = -cost;
|
|
||||||
return cost;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(Bishop bishop) {
|
|
||||||
return BISHOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(King king) {
|
|
||||||
return KING;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(Knight knight) {
|
|
||||||
return KNIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(Pawn pawn) {
|
|
||||||
return PAWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(Queen queen) {
|
|
||||||
return QUEEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer visitPiece(Rook rook) {
|
|
||||||
return ROOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -52,21 +52,19 @@ public class CommandExecutor {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
case Moved:
|
case Moved:
|
||||||
boolean notifyPlayerTurn = true;
|
|
||||||
this.dispatcher.onBoardUpdate();
|
this.dispatcher.onBoardUpdate();
|
||||||
if (checkGameStatus()) {
|
if (checkGameStatus()) {
|
||||||
this.dispatcher.onGameEnd();
|
this.dispatcher.onGameEnd();
|
||||||
notifyPlayerTurn = false;
|
return;
|
||||||
}
|
}
|
||||||
switchPlayerTurn(command instanceof UndoCommand, notifyPlayerTurn);
|
switchPlayerTurn();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void switchPlayerTurn(boolean undone, boolean notifyPlayerTurn) {
|
private void switchPlayerTurn() {
|
||||||
this.game.switchPlayerTurn();
|
this.game.switchPlayerTurn();
|
||||||
if (notifyPlayerTurn)
|
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn());
|
||||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -49,8 +49,6 @@ public class CastlingCommand extends PlayerCommand {
|
|||||||
board.applyMove(this.kingMove);
|
board.applyMove(this.kingMove);
|
||||||
board.applyMove(this.rookMove);
|
board.applyMove(this.rookMove);
|
||||||
|
|
||||||
outputSystem.onCastling(this.bigCastling);
|
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
return result;
|
return result;
|
||||||
|
|
||||||
case Moved:
|
case Moved:
|
||||||
|
outputSystem.onMove(this.move);
|
||||||
game.saveTraitPiecesPos();
|
game.saveTraitPiecesPos();
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
@@ -75,14 +76,11 @@ public class MoveCommand extends PlayerCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (tryPromote(game, outputSystem)) {
|
if (tryPromote(game, outputSystem)) {
|
||||||
outputSystem.onMove(this.move);
|
|
||||||
return CommandResult.ActionNeeded;
|
return CommandResult.ActionNeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
board.setLastMove(this.move);
|
board.setLastMove(this.move);
|
||||||
|
|
||||||
outputSystem.onMove(this.move);
|
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public class NewGameCommand extends Command {
|
|||||||
game.reset();
|
game.reset();
|
||||||
|
|
||||||
outputSystem.onGameStart();
|
outputSystem.onGameStart();
|
||||||
outputSystem.onPlayerTurn(game.getPlayerTurn(), false);
|
outputSystem.onPlayerTurn(game.getPlayerTurn());
|
||||||
|
|
||||||
return CommandResult.NotMoved;
|
return CommandResult.NotMoved;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,8 +55,6 @@ public class PromoteCommand extends PlayerCommand {
|
|||||||
this.oldPawn = pawn;
|
this.oldPawn = pawn;
|
||||||
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
board.pieceComes(createPiece(this.promoteType, pawn.getColor()), this.pieceCoords);
|
||||||
|
|
||||||
outputSystem.onPawnPromoted(this.promoteType);
|
|
||||||
|
|
||||||
return CommandResult.Moved;
|
return CommandResult.Moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.controller.event;
|
package chess.controller.event;
|
||||||
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
@@ -44,7 +43,7 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color, boolean undone) {
|
public void onPlayerTurn(Color color) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -59,12 +58,4 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
|||||||
public void onWin(Color winner) {
|
public void onWin(Color winner) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.controller.event;
|
package chess.controller.event;
|
||||||
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
@@ -8,7 +7,7 @@ import chess.model.Move;
|
|||||||
public abstract class GameAdaptator implements GameListener {
|
public abstract class GameAdaptator implements GameListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color, boolean undone) {}
|
public void onPlayerTurn(Color color) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onWin(Color color) {}
|
public void onWin(Color color) {}
|
||||||
@@ -46,10 +45,4 @@ public abstract class GameAdaptator implements GameListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onDraw() {}
|
public void onDraw() {}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.util.concurrent.ExecutorService;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
@@ -30,8 +29,8 @@ public class GameDispatcher implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color, boolean undone) {
|
public void onPlayerTurn(Color color) {
|
||||||
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
asyncForEachCall((l) -> l.onPlayerTurn(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -94,16 +93,6 @@ public class GameDispatcher implements GameListener {
|
|||||||
asyncForEachCall((l) -> l.onDraw());
|
asyncForEachCall((l) -> l.onDraw());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {
|
|
||||||
asyncForEachCall((l) -> l.onCastling(bigCastling));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {
|
|
||||||
asyncForEachCall((l) -> l.onPawnPromoted(promotion));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopService() {
|
public void stopService() {
|
||||||
this.executor.shutdown();
|
this.executor.shutdown();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.controller.event;
|
package chess.controller.event;
|
||||||
|
|
||||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
@@ -39,14 +38,12 @@ public interface GameListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when a valid move on the board occurs
|
* Invoked when a valid move on the board occurs
|
||||||
*
|
|
||||||
* @param move the move to be processed
|
* @param move the move to be processed
|
||||||
*/
|
*/
|
||||||
void onMove(Move move);
|
void onMove(Move move);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when a sent move is not allowed
|
* Invoked when a sent move is not allowed
|
||||||
*
|
|
||||||
* @param move the move to be processed
|
* @param move the move to be processed
|
||||||
*/
|
*/
|
||||||
void onMoveNotAllowed(Move move);
|
void onMoveNotAllowed(Move move);
|
||||||
@@ -58,46 +55,26 @@ public interface GameListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when it's the player turn
|
* Invoked when it's the player turn
|
||||||
*
|
|
||||||
* @param color the color of the player who should play
|
* @param color the color of the player who should play
|
||||||
* @param undone true if it's a result of an undo command
|
|
||||||
*/
|
*/
|
||||||
void onPlayerTurn(Color color, boolean undone);
|
void onPlayerTurn(Color color);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when a pawn should be promoted
|
* Invoked when a pawn should be promoted
|
||||||
*
|
|
||||||
* @param pieceCoords the coordinates of the pawn
|
* @param pieceCoords the coordinates of the pawn
|
||||||
*/
|
*/
|
||||||
void onPromotePawn(Coordinate pieceCoords);
|
void onPromotePawn(Coordinate pieceCoords);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when a players surrenders
|
* Invoked when a players surrenders
|
||||||
*
|
|
||||||
* @param coward the player who gave up
|
* @param coward the player who gave up
|
||||||
*/
|
*/
|
||||||
void onSurrender(Color coward);
|
void onSurrender(Color coward);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked when a player wins (by checkmate or if the other one surrenders)
|
* Invoked when a player wins (by checkmate or if the other one surrenders)
|
||||||
*
|
|
||||||
* @param winner
|
* @param winner
|
||||||
*/
|
*/
|
||||||
void onWin(Color winner);
|
void onWin(Color winner);
|
||||||
|
|
||||||
/**
|
|
||||||
* Invoked when a castling is done
|
|
||||||
*
|
|
||||||
* @param bigCastling if it's queen side castling
|
|
||||||
*/
|
|
||||||
void onCastling(boolean bigCastling);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invoked when a pawn is promoted
|
|
||||||
*
|
|
||||||
* @param promotion the type of promotion
|
|
||||||
* @param player the player who promoted the pawns
|
|
||||||
*/
|
|
||||||
void onPawnPromoted(PromoteType promotion);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ public class Game {
|
|||||||
Draw, Check, CheckMate, OnGoing, Pat;
|
Draw, Check, CheckMate, OnGoing, Pat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Game() {
|
public Game(ChessBoard board) {
|
||||||
this.board = new ChessBoard();
|
this.board = board;
|
||||||
this.movesHistory = new Stack<>();
|
this.movesHistory = new Stack<>();
|
||||||
this.traitsPos = new HashMap<>();
|
this.traitsPos = new HashMap<>();
|
||||||
}
|
}
|
||||||
@@ -70,26 +70,24 @@ public class Game {
|
|||||||
playerTurn = Color.getEnemy(playerTurn);
|
playerTurn = Color.getEnemy(playerTurn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameStatus checkGameStatus(Color color) {
|
public GameStatus checkGameStatus() {
|
||||||
|
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||||
|
|
||||||
if (checkDraw())
|
if (checkDraw())
|
||||||
return GameStatus.Draw;
|
return GameStatus.Draw;
|
||||||
|
|
||||||
if (this.board.isKingInCheck(color))
|
if (this.board.isKingInCheck(enemy))
|
||||||
if (this.board.hasAllowedMoves(color))
|
if (this.board.hasAllowedMoves(enemy))
|
||||||
return GameStatus.Check;
|
return GameStatus.Check;
|
||||||
else
|
else
|
||||||
return GameStatus.CheckMate;
|
return GameStatus.CheckMate;
|
||||||
|
|
||||||
if (!board.hasAllowedMoves(color))
|
if (!board.hasAllowedMoves(enemy))
|
||||||
return GameStatus.Pat;
|
return GameStatus.Pat;
|
||||||
|
|
||||||
return GameStatus.OnGoing;
|
return GameStatus.OnGoing;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameStatus checkGameStatus() {
|
|
||||||
return checkGameStatus(Color.getEnemy(getPlayerTurn()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addAction(PlayerCommand command) {
|
public void addAction(PlayerCommand command) {
|
||||||
this.movesHistory.add(command);
|
this.movesHistory.add(command);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import chess.controller.commands.MoveCommand;
|
|||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.controller.commands.PromoteCommand;
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.controller.event.EmptyGameDispatcher;
|
import chess.controller.event.EmptyGameDispatcher;
|
||||||
|
import chess.model.ChessBoard;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
@@ -21,7 +22,7 @@ import chess.model.pieces.Pawn;
|
|||||||
public class PgnExport {
|
public class PgnExport {
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
// public static void main(String[] args) {
|
||||||
// final Game game = new Game();
|
// final Game game = new Game(new ChessBoard());
|
||||||
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
// final CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
// DumbAI ai1 = new DumbAI(commandExecutor, Color.White);
|
||||||
@@ -137,7 +138,7 @@ public class PgnExport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String checkCheckMate(Game game) {
|
private static String checkCheckMate(Game game) {
|
||||||
switch (game.checkGameStatus(game.getPlayerTurn())) {
|
switch (game.checkGameStatus()) {
|
||||||
case CheckMate:
|
case CheckMate:
|
||||||
return "#";
|
return "#";
|
||||||
|
|
||||||
@@ -189,7 +190,8 @@ public class PgnExport {
|
|||||||
|
|
||||||
public static String exportGame(Game game) {
|
public static String exportGame(Game game) {
|
||||||
|
|
||||||
Game virtualGame = new Game();
|
ChessBoard board = new ChessBoard();
|
||||||
|
Game virtualGame = new Game(board);
|
||||||
|
|
||||||
CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher());
|
CommandExecutor executor = new CommandExecutor(virtualGame, new EmptyGameDispatcher());
|
||||||
executor.executeCommand(new NewGameCommand());
|
executor.executeCommand(new NewGameCommand());
|
||||||
|
|||||||
@@ -20,17 +20,12 @@ public class Console implements GameListener {
|
|||||||
private final Scanner scanner = new Scanner(System.in);
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
private final ConsolePieceName consolePieceName = new ConsolePieceName();
|
private final ConsolePieceName consolePieceName = new ConsolePieceName();
|
||||||
private boolean captureInput;
|
private boolean captureInput = false;
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
|
|
||||||
public Console(CommandExecutor commandExecutor, boolean captureInput) {
|
public Console(CommandExecutor commandExecutor) {
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
this.executor = Executors.newSingleThreadExecutor();
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
this.captureInput = captureInput;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Console(CommandExecutor commandExecutor) {
|
|
||||||
this(commandExecutor, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Piece pieceAt(int x, int y) {
|
private Piece pieceAt(int x, int y) {
|
||||||
@@ -66,7 +61,7 @@ public class Console implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color, boolean undone) {
|
public void onPlayerTurn(Color color) {
|
||||||
if (!captureInput)
|
if (!captureInput)
|
||||||
return;
|
return;
|
||||||
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
@@ -333,11 +328,4 @@ public class Console implements GameListener {
|
|||||||
this.captureInput = captureInput;
|
this.captureInput = captureInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package chess.view.render;
|
|||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
|
|
||||||
|
import chess.model.Coordinate;
|
||||||
import chess.view.render.shader.BoardShader;
|
import chess.view.render.shader.BoardShader;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
import static org.lwjgl.opengl.GL30.*;
|
||||||
@@ -59,16 +60,42 @@ public class Renderer {
|
|||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Coordinate GetCellFromColor(Vector3f color) {
|
||||||
|
int offset = 1;
|
||||||
|
|
||||||
|
if (color.x > 0.5) {
|
||||||
|
color = new Vector3f(1.0f, 1.0f, 1.0f).sub(color);
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = (int) (color.x * 255.0f);
|
||||||
|
int g = (int) (color.y * 255.0f);
|
||||||
|
int b = (int) (color.z * 255.0f);
|
||||||
|
|
||||||
|
int index = (r + g + b) * 2 + offset;
|
||||||
|
|
||||||
|
return Coordinate.fromIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector3f GetCellColor(int x, int y) {
|
||||||
|
float index = (y * BOARD_WIDTH + x) / 2.0f;
|
||||||
|
float r = (int) (index / 3) / 255.0f;
|
||||||
|
float g = (int) ((index + 1) / 3) / 255.0f;
|
||||||
|
float b = (int) ((index + 2) / 3) / 255.0f;
|
||||||
|
if ((x + y) % 2 != 0) {
|
||||||
|
System.out.println(GetCellFromColor(new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f)));
|
||||||
|
return new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f);
|
||||||
|
} else {
|
||||||
|
System.out.println(GetCellFromColor(new Vector3f(r, g, b)));
|
||||||
|
return new Vector3f(r, g, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private float[] GetBoardColors() {
|
private float[] GetBoardColors() {
|
||||||
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
||||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
|
||||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||||
Vector3f color;
|
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||||
if ((i + j) % 2 != 0) {
|
Vector3f color = GetCellColor(i, j);
|
||||||
color = new Vector3f(1.0f, 1.0f, 1.0f);
|
|
||||||
} else {
|
|
||||||
color = new Vector3f(0.0f, 0.0f, 0.0f);
|
|
||||||
}
|
|
||||||
int squareIndex = i * BOARD_WIDTH + j;
|
int squareIndex = i * BOARD_WIDTH + j;
|
||||||
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
||||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
||||||
@@ -109,6 +136,12 @@ public class Renderer {
|
|||||||
this.vao.Unbind();
|
this.vao.Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Coordinate GetSelectedCell() {
|
||||||
|
float pixels[] = new float[3];
|
||||||
|
GL30.glReadPixels(500, 500, 1, 1, GL_RGB, GL_FLOAT, pixels);
|
||||||
|
return GetCellFromColor(new Vector3f(pixels[0], pixels[1], pixels[2]));
|
||||||
|
}
|
||||||
|
|
||||||
public void Render(Camera cam) {
|
public void Render(Camera cam) {
|
||||||
this.shader.Start();
|
this.shader.Start();
|
||||||
this.shader.SetCamMatrix(cam.getMatrix());
|
this.shader.SetCamMatrix(cam.getMatrix());
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ import org.lwjgl.glfw.*;
|
|||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
import org.lwjgl.system.*;
|
import org.lwjgl.system.*;
|
||||||
|
|
||||||
|
import chess.controller.CommandExecutor;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
|
import chess.model.Color;
|
||||||
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
|
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.Callbacks.*;
|
import static org.lwjgl.glfw.Callbacks.*;
|
||||||
@@ -13,17 +19,20 @@ import static org.lwjgl.opengl.GL11.*;
|
|||||||
import static org.lwjgl.system.MemoryStack.*;
|
import static org.lwjgl.system.MemoryStack.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
public class Window {
|
public class Window implements GameListener{
|
||||||
|
|
||||||
// The window handle
|
// The window handle
|
||||||
private long window;
|
private long window;
|
||||||
|
|
||||||
|
private final CommandExecutor commandExecutor;
|
||||||
|
|
||||||
private Renderer renderer;
|
private Renderer renderer;
|
||||||
private Camera cam;
|
private Camera cam;
|
||||||
|
|
||||||
public Window() {
|
public Window(CommandExecutor commandExecutor) {
|
||||||
this.renderer = new Renderer();
|
this.renderer = new Renderer();
|
||||||
this.cam = new Camera();
|
this.cam = new Camera();
|
||||||
|
this.commandExecutor = new CommandExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -114,6 +123,8 @@ public class Window {
|
|||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
|
System.out.println(this.renderer.GetSelectedCell());
|
||||||
|
|
||||||
glfwSwapBuffers(window); // swap the color buffers
|
glfwSwapBuffers(window); // swap the color buffers
|
||||||
|
|
||||||
// Poll for window events. The key callback above will only be
|
// Poll for window events. The key callback above will only be
|
||||||
@@ -132,4 +143,82 @@ public class Window {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBoardUpdate() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onBoardUpdate'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onDraw'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onGameEnd'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameStart() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onGameStart'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onKingInCheck'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onKingInMat'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onMove'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoveNotAllowed(Move move) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onMoveNotAllowed'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPatSituation'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPlayerTurn(Color color) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPlayerTurn'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPromotePawn(Coordinate pieceCoords) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onPromotePawn'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color coward) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onSurrender'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWin(Color winner) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'onWin'");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -213,7 +213,7 @@ public class Window extends JFrame implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(chess.model.Color color, boolean undone) {
|
public void onPlayerTurn(chess.model.Color color) {
|
||||||
this.displayText.setText("Current turn: " + color);
|
this.displayText.setText("Current turn: " + color);
|
||||||
updateButtons();
|
updateButtons();
|
||||||
}
|
}
|
||||||
@@ -318,10 +318,4 @@ public class Window extends JFrame implements GameListener {
|
|||||||
JOptionPane.showMessageDialog(this, "Same position was repeated three times. It's a draw!");
|
JOptionPane.showMessageDialog(this, "Same position was repeated three times. It's a draw!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCastling(boolean bigCastling) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPawnPromoted(PromoteType promotion) {}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,24 @@
|
|||||||
*/
|
*/
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import chess.ai.DumbAI;
|
import chess.ai.DumbAI;
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.controller.commands.UndoCommand;
|
import chess.controller.commands.UndoCommand;
|
||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
import chess.model.Color;
|
import chess.model.*;
|
||||||
import chess.model.Game;
|
import chess.model.pieces.*;
|
||||||
|
import chess.simulator.Simulator;
|
||||||
|
import chess.view.simplerender.Window;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
class AppTest {
|
class AppTest {
|
||||||
@Test void functionalRollback(){
|
@Test void functionalRollback(){
|
||||||
Game game = new Game();
|
Game game = new Game(new ChessBoard());
|
||||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
@@ -34,9 +38,9 @@ class AppTest {
|
|||||||
result = commandExecutor.executeCommand(new UndoCommand());
|
result = commandExecutor.executeCommand(new UndoCommand());
|
||||||
} while (result != Command.CommandResult.NotAllowed);
|
} while (result != Command.CommandResult.NotAllowed);
|
||||||
|
|
||||||
Game initialGame = new Game();
|
Game initialGame = new Game(new ChessBoard());
|
||||||
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
|
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
|
||||||
initialCommandExecutor.executeCommand(new NewGameCommand());
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
assert(game.getBoard().equals(initialGame.getBoard()));
|
assert(game.getBoard().equals(initialGame.getBoard()));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user