mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +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.*;
|
||||||
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;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user