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