Beautify ui a bit, adjust some names
This commit is contained in:
parent
8de7f2ac50
commit
6c4978cf72
@ -10,7 +10,7 @@ public interface IUltimateTicTacToe {
|
|||||||
|
|
||||||
Player getCurrentPlayer();
|
Player getCurrentPlayer();
|
||||||
|
|
||||||
GameState getGameState();
|
GameState getGlobalGameState();
|
||||||
|
|
||||||
void doPlayerMove(int cell);
|
void doPlayerMove(int cell);
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ public class UltimateTicTacToe implements IUltimateTicTacToe {
|
|||||||
private final GameState[] masterGameStates;
|
private final GameState[] masterGameStates;
|
||||||
private Player currentPlayer;
|
private Player currentPlayer;
|
||||||
private int activeField;
|
private int activeField;
|
||||||
private GameState gameState;
|
private GameState globalGameState;
|
||||||
|
|
||||||
public UltimateTicTacToe() {
|
public UltimateTicTacToe() {
|
||||||
this.subGames = new ITicTacToeGame[9];
|
this.subGames = new ITicTacToeGame[9];
|
||||||
@ -26,7 +26,7 @@ public class UltimateTicTacToe implements IUltimateTicTacToe {
|
|||||||
// X always starts in the center field
|
// X always starts in the center field
|
||||||
this.currentPlayer = Player.X;
|
this.currentPlayer = Player.X;
|
||||||
this.activeField = 4;
|
this.activeField = 4;
|
||||||
this.gameState = GameState.RUNNING;
|
this.globalGameState = GameState.RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -45,8 +45,8 @@ public class UltimateTicTacToe implements IUltimateTicTacToe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GameState getGameState() {
|
public GameState getGlobalGameState() {
|
||||||
return gameState;
|
return globalGameState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,7 +60,7 @@ public class UltimateTicTacToe implements IUltimateTicTacToe {
|
|||||||
|
|
||||||
updateGameState();
|
updateGameState();
|
||||||
|
|
||||||
if (this.gameState == GameState.RUNNING) {
|
if (this.globalGameState == GameState.RUNNING) {
|
||||||
this.activeField = findNextAvailableSubGame(cell);
|
this.activeField = findNextAvailableSubGame(cell);
|
||||||
this.currentPlayer = this.currentPlayer == Player.X ? Player.O : Player.X;
|
this.currentPlayer = this.currentPlayer == Player.X ? Player.O : Player.X;
|
||||||
}
|
}
|
||||||
@ -75,15 +75,15 @@ public class UltimateTicTacToe implements IUltimateTicTacToe {
|
|||||||
interestingStates.add(this.masterGameStates[winningCombination[2]]);
|
interestingStates.add(this.masterGameStates[winningCombination[2]]);
|
||||||
if (interestingStates.size() == 1 && !interestingStates.contains(GameState.RUNNING)) {
|
if (interestingStates.size() == 1 && !interestingStates.contains(GameState.RUNNING)) {
|
||||||
switch (this.masterGameStates[winningCombination[0]]) {
|
switch (this.masterGameStates[winningCombination[0]]) {
|
||||||
case O_WON -> this.gameState = GameState.O_WON;
|
case O_WON -> this.globalGameState = GameState.O_WON;
|
||||||
case X_WON -> this.gameState = GameState.X_WON;
|
case X_WON -> this.globalGameState = GameState.X_WON;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Arrays.stream(this.masterGameStates).allMatch(gameState -> gameState != GameState.RUNNING)) {
|
if (Arrays.stream(this.masterGameStates).allMatch(gameState -> gameState != GameState.RUNNING)) {
|
||||||
this.gameState = GameState.DRAW;
|
this.globalGameState = GameState.DRAW;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package de.icaotix.ultimatetictactoe.view;
|
|||||||
import de.icaotix.ultimatetictactoe.viewmodel.TicTacToePanelViewModel;
|
import de.icaotix.ultimatetictactoe.viewmodel.TicTacToePanelViewModel;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.plaf.metal.MetalButtonUI;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -10,25 +11,31 @@ public class TicTacToeView {
|
|||||||
|
|
||||||
private final TicTacToePanelViewModel viewModel;
|
private final TicTacToePanelViewModel viewModel;
|
||||||
private final JPanel view;
|
private final JPanel view;
|
||||||
private final Button[] buttons;
|
private final JButton[] buttons;
|
||||||
|
|
||||||
public TicTacToeView(TicTacToePanelViewModel viewModel) {
|
public TicTacToeView(TicTacToePanelViewModel viewModel) {
|
||||||
this.viewModel = viewModel;
|
this.viewModel = viewModel;
|
||||||
this.buttons = new Button[9];
|
this.buttons = new JButton[9];
|
||||||
this.view = new JPanel(new GridLayout(3, 3, 0, 0));
|
this.view = new JPanel(new GridLayout(3, 3, 0, 0));
|
||||||
view.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
view.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
var btn = new Button("");
|
var btn = new JButton("");
|
||||||
btn.setEnabled(false);
|
|
||||||
btn.setBackground(Color.GRAY);
|
|
||||||
btn.setActionCommand(Integer.toString(i));
|
btn.setActionCommand(Integer.toString(i));
|
||||||
btn.addActionListener(e -> {
|
btn.addActionListener(e -> {
|
||||||
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
|
viewModel.onButtonClicked(Integer.parseInt(e.getActionCommand()));
|
||||||
});
|
});
|
||||||
|
btn.setUI(new MetalButtonUI() {
|
||||||
|
@Override
|
||||||
|
protected Color getDisabledTextColor() {
|
||||||
|
return Color.BLACK;
|
||||||
|
}
|
||||||
|
});
|
||||||
buttons[i] = btn;
|
buttons[i] = btn;
|
||||||
view.add(btn);
|
view.add(btn);
|
||||||
}
|
}
|
||||||
|
this.deactivate();
|
||||||
|
|
||||||
this.viewModel.setActivateCallback(this::activate);
|
this.viewModel.setActivateCallback(this::activate);
|
||||||
this.viewModel.setDeactivateCallback(this::deactivate);
|
this.viewModel.setDeactivateCallback(this::deactivate);
|
||||||
this.viewModel.setFinishPanelCallback(this::setFinishPanel);
|
this.viewModel.setFinishPanelCallback(this::setFinishPanel);
|
||||||
@ -40,7 +47,7 @@ public class TicTacToeView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setButtonText(int btnId, String text) {
|
public void setButtonText(int btnId, String text) {
|
||||||
this.buttons[btnId].setLabel(text);
|
this.buttons[btnId].setText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void activate(List<Integer> btnIds) {
|
public void activate(List<Integer> btnIds) {
|
||||||
@ -51,19 +58,18 @@ public class TicTacToeView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deactivate() {
|
public void deactivate() {
|
||||||
for (Button button : buttons) {
|
for (JButton button : buttons) {
|
||||||
button.setEnabled(false);
|
button.setEnabled(false);
|
||||||
button.setBackground(Color.GRAY);
|
button.setBackground(Color.LIGHT_GRAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFinishPanel(String finishString) {
|
public void setFinishPanel(String finishString) {
|
||||||
this.view.removeAll();
|
this.view.removeAll();
|
||||||
this.view.setLayout(new BorderLayout());
|
this.view.setLayout(new BorderLayout());
|
||||||
Button winnerLabel = new Button(finishString);
|
JButton winnerLabel = new JButton(finishString);
|
||||||
winnerLabel.setEnabled(false);
|
winnerLabel.setEnabled(false);
|
||||||
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
winnerLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
|
||||||
winnerLabel.setFont(font);
|
|
||||||
this.view.add(winnerLabel, BorderLayout.CENTER);
|
this.view.add(winnerLabel, BorderLayout.CENTER);
|
||||||
this.view.validate();
|
this.view.validate();
|
||||||
this.view.repaint();
|
this.view.repaint();
|
||||||
|
@ -22,6 +22,7 @@ public class UltimateTicTacToeView extends JFrame {
|
|||||||
|
|
||||||
JPanel mainGamePane = new JPanel();
|
JPanel mainGamePane = new JPanel();
|
||||||
this.getContentPane().add(mainGamePane, BorderLayout.CENTER);
|
this.getContentPane().add(mainGamePane, BorderLayout.CENTER);
|
||||||
|
mainGamePane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
mainGamePane.setLayout(new GridLayout(3, 3, 2, 2));
|
mainGamePane.setLayout(new GridLayout(3, 3, 2, 2));
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
TicTacToePanelViewModel subViewModel = viewModel.getSubGameViewModel(i);
|
TicTacToePanelViewModel subViewModel = viewModel.getSubGameViewModel(i);
|
||||||
@ -43,7 +44,7 @@ public class UltimateTicTacToeView extends JFrame {
|
|||||||
this.getContentPane().removeAll();
|
this.getContentPane().removeAll();
|
||||||
|
|
||||||
this.getContentPane().setLayout(new BorderLayout());
|
this.getContentPane().setLayout(new BorderLayout());
|
||||||
Button winnerLabel = new Button(resultText);
|
JButton winnerLabel = new JButton(resultText);
|
||||||
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
||||||
winnerLabel.setFont(font);
|
winnerLabel.setFont(font);
|
||||||
|
|
||||||
|
@ -23,9 +23,9 @@ public class UltimateTicTacToePanelViewModel {
|
|||||||
|
|
||||||
public void prepareNextMove() {
|
public void prepareNextMove() {
|
||||||
// Check if game already finished
|
// Check if game already finished
|
||||||
if (this.ultimateTicTacToe.getGameState() != GameState.RUNNING) {
|
if (this.ultimateTicTacToe.getGlobalGameState() != GameState.RUNNING) {
|
||||||
if (this.gameResultCallback != null) {
|
if (this.gameResultCallback != null) {
|
||||||
final String result = this.ultimateTicTacToe.getGameState().displayText;
|
final String result = this.ultimateTicTacToe.getGlobalGameState().displayText;
|
||||||
this.gameResultCallback.accept(result);
|
this.gameResultCallback.accept(result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user