Moved game and board creation to Game
This commit is contained in:
parent
a2d479bba0
commit
f6a578531f
@ -1,11 +1,13 @@
|
||||
package dev.kske.chess.game;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import dev.kske.chess.board.Board;
|
||||
import dev.kske.chess.board.GameState;
|
||||
import dev.kske.chess.board.Move;
|
||||
import dev.kske.chess.board.Piece.Color;
|
||||
import dev.kske.chess.game.ai.AIPlayer;
|
||||
import dev.kske.chess.ui.BoardComponent;
|
||||
import dev.kske.chess.ui.BoardPane;
|
||||
import dev.kske.chess.ui.OverlayComponent;
|
||||
@ -27,12 +29,39 @@ public class Game {
|
||||
this.players = players;
|
||||
this.overlayComponent = boardPane.getOverlayComponent();
|
||||
this.boardComponent = boardPane.getBoardComponent();
|
||||
this.board = boardComponent.getBoard();
|
||||
this.board = new Board();
|
||||
boardComponent.setBoard(board);
|
||||
|
||||
// Initialize the game variable in each player
|
||||
players.values().forEach(player -> player.setGame(this));
|
||||
}
|
||||
|
||||
public static Game createNatural(BoardPane boardPane) {
|
||||
Map<Color, Player> players = new HashMap<>();
|
||||
OverlayComponent overlay = boardPane.getOverlayComponent();
|
||||
|
||||
players.put(Color.WHITE, new NaturalPlayer(Color.WHITE, overlay));
|
||||
players.put(Color.BLACK, new NaturalPlayer(Color.BLACK, overlay));
|
||||
return new Game(players, boardPane);
|
||||
}
|
||||
|
||||
public static Game createNaturalVsAI(BoardPane boardPane, int maxDepth, int alphaBeta) {
|
||||
Map<Color, Player> players = new HashMap<>();
|
||||
OverlayComponent overlay = boardPane.getOverlayComponent();
|
||||
|
||||
players.put(Color.WHITE, new NaturalPlayer(Color.WHITE, overlay));
|
||||
players.put(Color.BLACK, new AIPlayer(Color.BLACK, maxDepth, alphaBeta));
|
||||
return new Game(players, boardPane);
|
||||
}
|
||||
|
||||
public static Game createAIVsAI(BoardPane boardPane, int maxDepthW, int maxDepthB, int alphaBetaW, int alphaBetaB) {
|
||||
Map<Color, Player> players = new HashMap<>();
|
||||
|
||||
players.put(Color.WHITE, new AIPlayer(Color.WHITE, maxDepthW, alphaBetaW));
|
||||
players.put(Color.BLACK, new AIPlayer(Color.BLACK, maxDepthB, alphaBetaB));
|
||||
return new Game(players, boardPane);
|
||||
}
|
||||
|
||||
public void onMove(Player player, Move move) {
|
||||
if (board.getPos(move).getColor() == player.color && board.attemptMove(move)) {
|
||||
System.out.printf("%s: %s%n", player.color, move);
|
||||
@ -63,4 +92,6 @@ public class Game {
|
||||
overlayComponent.clearDots();
|
||||
overlayComponent.clearArrow();
|
||||
}
|
||||
|
||||
public Board getBoard() { return board; }
|
||||
}
|
||||
|
@ -22,8 +22,8 @@ public class NaturalPlayer extends Player {
|
||||
|
||||
private boolean moveRequested;
|
||||
|
||||
public NaturalPlayer(Board board, Color color, OverlayComponent overlayComponent) {
|
||||
super(board, color);
|
||||
public NaturalPlayer(Color color, OverlayComponent overlayComponent) {
|
||||
super(color);
|
||||
moveRequested = false;
|
||||
overlayComponent.addMouseListener(new MouseAdapter() {
|
||||
|
||||
|
@ -15,8 +15,7 @@ public abstract class Player {
|
||||
protected Board board;
|
||||
protected Color color;
|
||||
|
||||
public Player(Board board, Color color) {
|
||||
this.board = board;
|
||||
public Player(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@ -26,7 +25,10 @@ public abstract class Player {
|
||||
|
||||
public Game getGame() { return game; }
|
||||
|
||||
public void setGame(Game game) { this.game = game; }
|
||||
public void setGame(Game game) {
|
||||
this.game = game;
|
||||
board = game.getBoard();
|
||||
}
|
||||
|
||||
public Board getBoard() { return board; }
|
||||
|
||||
|
@ -30,8 +30,8 @@ public class AIPlayer extends Player {
|
||||
private volatile boolean exitRequested;
|
||||
private volatile ExecutorService executor;
|
||||
|
||||
public AIPlayer(Board board, Color color, int maxDepth, int alphaBetaThreshold) {
|
||||
super(board, color);
|
||||
public AIPlayer(Color color, int maxDepth, int alphaBetaThreshold) {
|
||||
super(color);
|
||||
availableProcessors = Runtime.getRuntime().availableProcessors();
|
||||
this.maxDepth = maxDepth;
|
||||
this.alphaBetaThreshold = alphaBetaThreshold;
|
||||
|
@ -7,7 +7,6 @@ import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import dev.kske.chess.board.Board;
|
||||
import dev.kske.chess.game.Game;
|
||||
|
||||
/**
|
||||
@ -56,7 +55,6 @@ public class MainWindow {
|
||||
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
boardPane = new BoardPane();
|
||||
boardPane.getBoardComponent().setBoard(new Board());
|
||||
mframe.getContentPane().add(boardPane, BorderLayout.CENTER);
|
||||
|
||||
mframe.setJMenuBar(new MenuBar(this));
|
||||
|
@ -1,18 +1,10 @@
|
||||
package dev.kske.chess.ui;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
import dev.kske.chess.board.Board;
|
||||
import dev.kske.chess.board.Piece.Color;
|
||||
import dev.kske.chess.game.Game;
|
||||
import dev.kske.chess.game.NaturalPlayer;
|
||||
import dev.kske.chess.game.Player;
|
||||
import dev.kske.chess.game.ai.AIPlayer;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
@ -26,18 +18,10 @@ public class MenuBar extends JMenuBar {
|
||||
|
||||
private final MainWindow mainWindow;
|
||||
private final BoardPane boardPane;
|
||||
private final OverlayComponent overlayComponent;
|
||||
private final BoardComponent boardComponent;
|
||||
private final Board board;
|
||||
private final Map<Color, Player> players;
|
||||
|
||||
public MenuBar(MainWindow mainWindow) {
|
||||
this.mainWindow = mainWindow;
|
||||
boardPane = mainWindow.getBoardPane();
|
||||
overlayComponent = boardPane.getOverlayComponent();
|
||||
boardComponent = boardPane.getBoardComponent();
|
||||
board = boardComponent.getBoard();
|
||||
players = new HashMap<>();
|
||||
|
||||
initGameMenu();
|
||||
}
|
||||
@ -49,38 +33,28 @@ public class MenuBar extends JMenuBar {
|
||||
JMenuItem aiMenuItem = new JMenuItem("Game against artificial opponent");
|
||||
JMenuItem aiVsAiMenuItem = new JMenuItem("Watch AI vs. AI");
|
||||
|
||||
naturalMenuItem.addActionListener((evt) -> {
|
||||
players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent));
|
||||
players.put(Color.BLACK, new NaturalPlayer(board, Color.BLACK, overlayComponent));
|
||||
startGame();
|
||||
});
|
||||
naturalMenuItem.addActionListener((evt) -> startGame(Game.createNatural(boardPane)));
|
||||
|
||||
aiMenuItem.addActionListener((evt) -> {
|
||||
AIConfigDialog dialog = new AIConfigDialog();
|
||||
dialog.setVisible(true);
|
||||
if (dialog.isStartGame()) {
|
||||
players.put(Color.WHITE, new NaturalPlayer(board, Color.WHITE, overlayComponent));
|
||||
players.put(Color.BLACK,
|
||||
new AIPlayer(board, Color.BLACK, dialog.getMaxDepth(), dialog.getAlphaBetaThreshold()));
|
||||
startGame();
|
||||
}
|
||||
if (dialog.isStartGame())
|
||||
startGame(Game.createNaturalVsAI(boardPane, dialog.getMaxDepth(), dialog.getAlphaBetaThreshold()));
|
||||
});
|
||||
|
||||
aiVsAiMenuItem.addActionListener((evt) -> {
|
||||
players.put(Color.WHITE, new AIPlayer(board, Color.WHITE, 4, -10));
|
||||
players.put(Color.BLACK, new AIPlayer(board, Color.BLACK, 3, 0));
|
||||
startGame();
|
||||
});
|
||||
aiVsAiMenuItem.addActionListener((evt) -> startGame(Game.createAIVsAI(boardPane, 4, 3, -10, -10)));
|
||||
|
||||
gameMenu.add(naturalMenuItem);
|
||||
gameMenu.add(aiMenuItem);
|
||||
gameMenu.add(aiVsAiMenuItem);
|
||||
|
||||
add(gameMenu);
|
||||
|
||||
// Start a game
|
||||
naturalMenuItem.doClick();
|
||||
}
|
||||
|
||||
private void startGame() {
|
||||
Game game = new Game(players, boardPane);
|
||||
private void startGame(Game game) {
|
||||
mainWindow.setGame(game);
|
||||
|
||||
// Update board and board component
|
||||
|
Reference in New Issue
Block a user