Bugfix: can no longer overwrite move with the UI. Added colours to signal end state.

This commit is contained in:
Ticho Hidding
2025-09-20 21:30:37 +02:00
parent 13411ed321
commit f86a99acb6
3 changed files with 51 additions and 14 deletions

View File

@@ -3,11 +3,17 @@ package org.toop.frontend.UI;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import javax.swing.*; import javax.swing.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.frontend.games.LocalTicTacToe; import org.toop.frontend.games.LocalTicTacToe;
import org.toop.game.tictactoe.GameBase;
public class UIGameBoard { public class UIGameBoard {
private static final int TICTACTOE_SIZE = 3; private static final int TICTACTOE_SIZE = 3;
private static final Logger logger = LogManager.getLogger(LocalGameSelector.class);
private JPanel tttPanel; // Root panel for this game private JPanel tttPanel; // Root panel for this game
private JButton backToMainMenuButton; private JButton backToMainMenuButton;
private JButton[] cells; private JButton[] cells;
@@ -57,16 +63,21 @@ public class UIGameBoard {
final int index = i; final int index = i;
cells[i].addActionListener( cells[i].addActionListener(
(ActionEvent _) -> { (ActionEvent _) -> {
int cp = this.localTicTacToe.getCurrentPlayersTurn(); if (cells[index].getText().equals(" ")) {
if (cp == 0) { int cp = this.localTicTacToe.getCurrentPlayersTurn();
this.currentPlayer = "X"; if (cp == 0) {
currentPlayerIndex = 0; this.currentPlayer = "X";
} else if (cp == 1) { currentPlayerIndex = 0;
this.currentPlayer = "O"; } else if (cp == 1) {
currentPlayerIndex = 1; this.currentPlayer = "O";
currentPlayerIndex = 1;
}
this.localTicTacToe.move(index);
cells[index].setText(currentPlayer);
}
else{
logger.info("Player " + currentPlayerIndex + " attempted invalid move at: " + cells[index].getText());
} }
this.localTicTacToe.move(index);
cells[index].setText(currentPlayer);
}); });
} }
@@ -77,6 +88,21 @@ public class UIGameBoard {
System.out.println(cells[index].getText()); System.out.println(cells[index].getText());
cells[index].setText(move); cells[index].setText(move);
} }
public void setState(GameBase.State state, String playerMove) {
Color color;
if (state == GameBase.State.WIN && playerMove.equals(currentPlayer)) {
color = new Color(160,220,160);
}
else if (state == GameBase.State.WIN) {
color = new Color(220,160,160);
}
else{
color = new Color(220,220,160);
}
for (JButton cell : cells) {
cell.setBackground(color);
}
}
public JPanel getTTTPanel() { public JPanel getTTTPanel() {
return tttPanel; return tttPanel;

View File

@@ -142,23 +142,34 @@ public class LocalTicTacToe { // TODO: Implement runnable
this.ticTacToe = new TicTacToe("X", "O"); this.ticTacToe = new TicTacToe("X", "O");
while (running) { while (running) {
try { try {
GameBase.State state;
if (!isAiPlayer[0]) { if (!isAiPlayer[0]) {
this.ticTacToe.play(this.moveQueuePlayerA.take()); state = this.ticTacToe.play(this.moveQueuePlayerA.take());
} else { } else {
int bestMove = aiPlayers[0].findBestMove(this.ticTacToe); int bestMove = aiPlayers[0].findBestMove(this.ticTacToe);
if (this.ticTacToe.play(bestMove) != GameBase.State.INVALID) { state = this.ticTacToe.play(bestMove);
if (state != GameBase.State.INVALID) {
ui.setCell(bestMove, "X"); ui.setCell(bestMove, "X");
} }
} }
if (state == GameBase.State.WIN || state == GameBase.State.DRAW) {
ui.setState(state, "X");
running = false;
}
this.setNextPlayersTurn(); this.setNextPlayersTurn();
if (!isAiPlayer[1]) { if (!isAiPlayer[1]) {
this.ticTacToe.play(this.moveQueuePlayerB.take()); state = this.ticTacToe.play(this.moveQueuePlayerB.take());
} else { } else {
int bestMove = aiPlayers[1].findBestMove(this.ticTacToe); int bestMove = aiPlayers[1].findBestMove(this.ticTacToe);
if (this.ticTacToe.play(bestMove) != GameBase.State.INVALID) { state = this.ticTacToe.play(bestMove);
if (state != GameBase.State.INVALID) {
ui.setCell(bestMove, "O"); ui.setCell(bestMove, "O");
} }
} }
if (state == GameBase.State.WIN || state == GameBase.State.DRAW) {
ui.setState(state, "O");
running = false;
}
this.setNextPlayersTurn(); this.setNextPlayersTurn();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@@ -28,7 +28,7 @@ public class MinMaxTicTacToe {
} }
if (empty && game.validateMove(4)) { if (empty && game.validateMove(4)) {
return 4; return 0;
} }
// simulate all possible moves on the field // simulate all possible moves on the field