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