Add restart functionality and again some smaller ui improvements

This commit is contained in:
Marcel Schwarz 2021-11-03 23:22:16 +01:00
parent 6c4978cf72
commit 9d866c7034
4 changed files with 37 additions and 12 deletions

View File

@ -4,6 +4,8 @@ import de.icaotix.ultimatetictactoe.model.definitions.GameState;
import de.icaotix.ultimatetictactoe.model.definitions.Player; import de.icaotix.ultimatetictactoe.model.definitions.Player;
public interface IUltimateTicTacToe { public interface IUltimateTicTacToe {
void initialize();
ITicTacToeGame getSubGame(int id); ITicTacToeGame getSubGame(int id);
int getActiveField(); int getActiveField();

View File

@ -9,13 +9,18 @@ import java.util.HashSet;
public class UltimateTicTacToe implements IUltimateTicTacToe { public class UltimateTicTacToe implements IUltimateTicTacToe {
private final ITicTacToeGame[] subGames; private ITicTacToeGame[] subGames;
private final GameState[] masterGameStates; private GameState[] masterGameStates;
private Player currentPlayer; private Player currentPlayer;
private int activeField; private int activeField;
private GameState globalGameState; private GameState globalGameState;
public UltimateTicTacToe() { public UltimateTicTacToe() {
this.initialize();
}
@Override
public void initialize() {
this.subGames = new ITicTacToeGame[9]; this.subGames = new ITicTacToeGame[9];
for (int i = 0; i < this.subGames.length; i++) { for (int i = 0; i < this.subGames.length; i++) {
this.subGames[i] = new TicTacToeGame(); this.subGames[i] = new TicTacToeGame();

View File

@ -10,14 +10,21 @@ import java.awt.*;
public class UltimateTicTacToeView extends JFrame { public class UltimateTicTacToeView extends JFrame {
private final UltimateTicTacToePanelViewModel viewModel; private final UltimateTicTacToePanelViewModel viewModel;
private final Label currentPlayerLabel; private JLabel currentPlayerLabel;
public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) { public UltimateTicTacToeView(UltimateTicTacToePanelViewModel viewModel) {
super("Ultimate TicTacToe"); super("Ultimate TicTacToe");
this.viewModel = viewModel; this.viewModel = viewModel;
this.setDefaultCloseOperation(EXIT_ON_CLOSE); 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()); this.getContentPane().setLayout(new BorderLayout());
JPanel mainGamePane = new JPanel(); JPanel mainGamePane = new JPanel();
@ -29,26 +36,33 @@ public class UltimateTicTacToeView extends JFrame {
mainGamePane.add(new TicTacToeView(subViewModel).getView()); 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.getContentPane().add(this.currentPlayerLabel, BorderLayout.NORTH);
this.viewModel.setGameResultCallback(this::setGameResult); this.viewModel.setGameResultCallback(this::setGameResult);
this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer); this.viewModel.setCurrentPlayerCallback(this::setCurrentPlayer);
//Display the window.
this.setSize(new Dimension(500, 520));
this.setVisible(true);
} }
public void setGameResult(String resultText) { public void setGameResult(String resultText) {
this.getContentPane().removeAll(); this.getContentPane().removeAll();
this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().setLayout(new BorderLayout());
JButton winnerLabel = new JButton(resultText); JLabel winnerLabel = new JLabel(resultText);
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20);
winnerLabel.setFont(font); 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().validate();
this.getContentPane().repaint(); this.getContentPane().repaint();
} }

View File

@ -68,4 +68,8 @@ public class UltimateTicTacToePanelViewModel {
public void setCurrentPlayerCallback(Consumer<String> currentPlayerCallback) { public void setCurrentPlayerCallback(Consumer<String> currentPlayerCallback) {
this.currentPlayerCallback = currentPlayerCallback; this.currentPlayerCallback = currentPlayerCallback;
} }
public void startNewGame() {
this.ultimateTicTacToe.initialize();
}
} }