From 9d866c70346b9e1a5150896f209208938e4c04dd Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 3 Nov 2021 23:22:16 +0100 Subject: [PATCH] Add restart functionality and again some smaller ui improvements --- .../model/IUltimateTicTacToe.java | 2 ++ .../model/UltimateTicTacToe.java | 9 +++-- .../view/UltimateTicTacToeView.java | 34 +++++++++++++------ .../UltimateTicTacToePanelViewModel.java | 4 +++ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/icaotix/ultimatetictactoe/model/IUltimateTicTacToe.java b/src/main/java/de/icaotix/ultimatetictactoe/model/IUltimateTicTacToe.java index 68ab212..b0ca2a0 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/model/IUltimateTicTacToe.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/model/IUltimateTicTacToe.java @@ -4,6 +4,8 @@ import de.icaotix.ultimatetictactoe.model.definitions.GameState; import de.icaotix.ultimatetictactoe.model.definitions.Player; public interface IUltimateTicTacToe { + void initialize(); + ITicTacToeGame getSubGame(int id); int getActiveField(); diff --git a/src/main/java/de/icaotix/ultimatetictactoe/model/UltimateTicTacToe.java b/src/main/java/de/icaotix/ultimatetictactoe/model/UltimateTicTacToe.java index 4dc6d28..d2c1269 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/model/UltimateTicTacToe.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/model/UltimateTicTacToe.java @@ -9,13 +9,18 @@ import java.util.HashSet; public class UltimateTicTacToe implements IUltimateTicTacToe { - private final ITicTacToeGame[] subGames; - private final GameState[] masterGameStates; + private ITicTacToeGame[] subGames; + private GameState[] masterGameStates; private Player currentPlayer; private int activeField; private GameState globalGameState; public UltimateTicTacToe() { + this.initialize(); + } + + @Override + public void initialize() { this.subGames = new ITicTacToeGame[9]; for (int i = 0; i < this.subGames.length; i++) { this.subGames[i] = new TicTacToeGame(); diff --git a/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java b/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java index 261d155..0a13738 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/view/UltimateTicTacToeView.java @@ -10,14 +10,21 @@ import java.awt.*; public class UltimateTicTacToeView extends JFrame { private final UltimateTicTacToePanelViewModel viewModel; - private final Label currentPlayerLabel; + private JLabel currentPlayerLabel; public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) { super("Ultimate TicTacToe"); this.viewModel = viewModel; this.setDefaultCloseOperation(EXIT_ON_CLOSE); - //Create and set up the window. + this.setContentToGamePane(); + + this.setSize(new Dimension(500, 520)); + this.setVisible(true); + } + + public void setContentToGamePane() { + this.getContentPane().removeAll(); this.getContentPane().setLayout(new BorderLayout()); JPanel mainGamePane = new JPanel(); @@ -29,26 +36,33 @@ public class UltimateTicTacToeView extends JFrame { mainGamePane.add(new TicTacToeView(subViewModel).getView()); } - this.currentPlayerLabel = new Label(); + this.currentPlayerLabel = new JLabel(); + this.currentPlayerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); + this.currentPlayerLabel.setHorizontalAlignment(SwingConstants.CENTER); this.getContentPane().add(this.currentPlayerLabel, BorderLayout.NORTH); this.viewModel.setGameResultCallback(this::setGameResult); this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer); - - //Display the window. - this.setSize(new Dimension(500, 520)); - this.setVisible(true); } public void setGameResult(String resultText) { this.getContentPane().removeAll(); this.getContentPane().setLayout(new BorderLayout()); - JButton winnerLabel = new JButton(resultText); - Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15); + JLabel winnerLabel = new JLabel(resultText); + Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20); winnerLabel.setFont(font); + winnerLabel.setHorizontalAlignment(SwingConstants.CENTER); - this.getContentPane().add(winnerLabel); + JButton restartBtn = new JButton("RESTART"); + restartBtn.addActionListener(e -> { + this.viewModel.startNewGame(); + this.setContentToGamePane(); + this.viewModel.prepareNextMove(); + }); + + this.getContentPane().add(winnerLabel, BorderLayout.CENTER); + this.getContentPane().add(restartBtn, BorderLayout.SOUTH); this.getContentPane().validate(); this.getContentPane().repaint(); } diff --git a/src/main/java/de/icaotix/ultimatetictactoe/viewmodel/UltimateTicTacToePanelViewModel.java b/src/main/java/de/icaotix/ultimatetictactoe/viewmodel/UltimateTicTacToePanelViewModel.java index b5505c4..61aaf18 100644 --- a/src/main/java/de/icaotix/ultimatetictactoe/viewmodel/UltimateTicTacToePanelViewModel.java +++ b/src/main/java/de/icaotix/ultimatetictactoe/viewmodel/UltimateTicTacToePanelViewModel.java @@ -68,4 +68,8 @@ public class UltimateTicTacToePanelViewModel { public void setCurrentPlayerCallback(Consumer currentPlayerCallback) { this.currentPlayerCallback = currentPlayerCallback; } + + public void startNewGame() { + this.ultimateTicTacToe.initialize(); + } }