mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Bugfix: can no longer overwrite move with the UI. Added colours to signal end state.
This commit is contained in:
@@ -3,11 +3,17 @@ package org.toop.frontend.UI;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import javax.swing.*;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.frontend.games.LocalTicTacToe;
|
||||
import org.toop.game.tictactoe.GameBase;
|
||||
|
||||
public class UIGameBoard {
|
||||
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 JButton backToMainMenuButton;
|
||||
private JButton[] cells;
|
||||
@@ -57,16 +63,21 @@ public class UIGameBoard {
|
||||
final int index = i;
|
||||
cells[i].addActionListener(
|
||||
(ActionEvent _) -> {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) {
|
||||
this.currentPlayer = "X";
|
||||
currentPlayerIndex = 0;
|
||||
} else if (cp == 1) {
|
||||
this.currentPlayer = "O";
|
||||
currentPlayerIndex = 1;
|
||||
if (cells[index].getText().equals(" ")) {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) {
|
||||
this.currentPlayer = "X";
|
||||
currentPlayerIndex = 0;
|
||||
} else if (cp == 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());
|
||||
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() {
|
||||
return tttPanel;
|
||||
|
||||
@@ -142,23 +142,34 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
this.ticTacToe = new TicTacToe("X", "O");
|
||||
while (running) {
|
||||
try {
|
||||
GameBase.State state;
|
||||
if (!isAiPlayer[0]) {
|
||||
this.ticTacToe.play(this.moveQueuePlayerA.take());
|
||||
state = this.ticTacToe.play(this.moveQueuePlayerA.take());
|
||||
} else {
|
||||
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");
|
||||
}
|
||||
}
|
||||
if (state == GameBase.State.WIN || state == GameBase.State.DRAW) {
|
||||
ui.setState(state, "X");
|
||||
running = false;
|
||||
}
|
||||
this.setNextPlayersTurn();
|
||||
if (!isAiPlayer[1]) {
|
||||
this.ticTacToe.play(this.moveQueuePlayerB.take());
|
||||
state = this.ticTacToe.play(this.moveQueuePlayerB.take());
|
||||
} else {
|
||||
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");
|
||||
}
|
||||
}
|
||||
if (state == GameBase.State.WIN || state == GameBase.State.DRAW) {
|
||||
ui.setState(state, "O");
|
||||
running = false;
|
||||
}
|
||||
this.setNextPlayersTurn();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -28,7 +28,7 @@ public class MinMaxTicTacToe {
|
||||
}
|
||||
|
||||
if (empty && game.validateMove(4)) {
|
||||
return 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// simulate all possible moves on the field
|
||||
|
||||
Reference in New Issue
Block a user