From f7812a5f8105c6703190c664883f80bb15210249 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 17 Jul 2019 08:26:51 +0200 Subject: [PATCH] Moved game and board creation to Game --- src/dev/kske/chess/game/Game.java | 33 ++++++++++++++++- src/dev/kske/chess/game/NaturalPlayer.java | 4 +-- src/dev/kske/chess/game/Player.java | 10 +++--- src/dev/kske/chess/game/ai/AIPlayer.java | 4 +-- src/dev/kske/chess/ui/MainWindow.java | 2 -- src/dev/kske/chess/ui/MenuBar.java | 42 +++++----------------- 6 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index 6a30f6a..a7cd531 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -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 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 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 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; } } diff --git a/src/dev/kske/chess/game/NaturalPlayer.java b/src/dev/kske/chess/game/NaturalPlayer.java index 2c97782..f105862 100644 --- a/src/dev/kske/chess/game/NaturalPlayer.java +++ b/src/dev/kske/chess/game/NaturalPlayer.java @@ -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() { diff --git a/src/dev/kske/chess/game/Player.java b/src/dev/kske/chess/game/Player.java index a67d7ac..07d578a 100644 --- a/src/dev/kske/chess/game/Player.java +++ b/src/dev/kske/chess/game/Player.java @@ -15,9 +15,8 @@ public abstract class Player { protected Board board; protected Color color; - public Player(Board board, Color color) { - this.board = board; - this.color = color; + public Player(Color color) { + this.color = color; } public abstract void requestMove(); @@ -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; } diff --git a/src/dev/kske/chess/game/ai/AIPlayer.java b/src/dev/kske/chess/game/ai/AIPlayer.java index a17b6ef..6adf76e 100644 --- a/src/dev/kske/chess/game/ai/AIPlayer.java +++ b/src/dev/kske/chess/game/ai/AIPlayer.java @@ -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; diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index 54be5ef..e697839 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -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)); diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index 25386bb..471166f 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -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: Chess
@@ -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 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