mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Player vs AI working in the UI
This commit is contained in:
@@ -24,6 +24,7 @@ public class UIGameBoard {
|
|||||||
public UIGameBoard(LocalTicTacToe lttt, LocalGameSelector parent) {
|
public UIGameBoard(LocalTicTacToe lttt, LocalGameSelector parent) {
|
||||||
this.parentSelector = parent;
|
this.parentSelector = parent;
|
||||||
this.localTicTacToe = lttt;
|
this.localTicTacToe = lttt;
|
||||||
|
lttt.setUIReference(this);
|
||||||
|
|
||||||
// Root panel
|
// Root panel
|
||||||
tttPanel = new JPanel(new BorderLayout());
|
tttPanel = new JPanel(new BorderLayout());
|
||||||
@@ -47,14 +48,14 @@ public class UIGameBoard {
|
|||||||
|
|
||||||
for (int i = 0; i < sizeX * sizeY; i++) {
|
for (int i = 0; i < sizeX * sizeY; i++) {
|
||||||
cells[i] = new JButton(" ");
|
cells[i] = new JButton(" ");
|
||||||
cells[i].setFont(new Font("Arial", Font.BOLD, 100 / sizeX));
|
cells[i].setFont(new Font("Arial", Font.BOLD, 400 / sizeX));
|
||||||
panel.add(cells[i]);
|
panel.add(cells[i]);
|
||||||
|
|
||||||
final int index = i;
|
final int index = i;
|
||||||
cells[i].addActionListener((ActionEvent e) -> {
|
cells[i].addActionListener((ActionEvent e) -> {
|
||||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||||
if (cp == 1) { this.currentPlayer = "X"; currentPlayerIndex = 0; }
|
if (cp == 0) { this.currentPlayer = "X"; currentPlayerIndex = 0; }
|
||||||
else if (cp == 2) { this.currentPlayer = "O"; currentPlayerIndex = 1; }
|
else if (cp == 1) { this.currentPlayer = "O"; currentPlayerIndex = 1; }
|
||||||
this.localTicTacToe.move(index);
|
this.localTicTacToe.move(index);
|
||||||
cells[index].setText(currentPlayer);
|
cells[index].setText(currentPlayer);
|
||||||
});
|
});
|
||||||
@@ -62,6 +63,10 @@ public class UIGameBoard {
|
|||||||
|
|
||||||
return panel;
|
return panel;
|
||||||
}
|
}
|
||||||
|
public void setCell(int index, String move){
|
||||||
|
System.out.println(cells[index].getText());
|
||||||
|
cells[index].setText(move);
|
||||||
|
}
|
||||||
|
|
||||||
public JPanel getTTTPanel() {
|
public JPanel getTTTPanel() {
|
||||||
return tttPanel;
|
return tttPanel;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.eventbus.Events;
|
import org.toop.eventbus.Events;
|
||||||
import org.toop.eventbus.GlobalEventBus;
|
import org.toop.eventbus.GlobalEventBus;
|
||||||
|
import org.toop.frontend.UI.UIGameBoard;
|
||||||
|
import org.toop.game.GameBase;
|
||||||
import org.toop.game.tictactoe.MinMaxTicTacToe;
|
import org.toop.game.tictactoe.MinMaxTicTacToe;
|
||||||
import org.toop.game.tictactoe.TicTacToe;
|
import org.toop.game.tictactoe.TicTacToe;
|
||||||
|
|
||||||
@@ -32,6 +34,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
|||||||
private boolean isAiPlayer[] = new boolean[2];
|
private boolean isAiPlayer[] = new boolean[2];
|
||||||
private MinMaxTicTacToe[] aiPlayers = new MinMaxTicTacToe[2];
|
private MinMaxTicTacToe[] aiPlayers = new MinMaxTicTacToe[2];
|
||||||
private TicTacToe ticTacToe;
|
private TicTacToe ticTacToe;
|
||||||
|
private UIGameBoard ui;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,13 +139,19 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
|||||||
if (!isAiPlayer[0]) {
|
if (!isAiPlayer[0]) {
|
||||||
this.ticTacToe.play(this.moveQueuePlayerA.take());
|
this.ticTacToe.play(this.moveQueuePlayerA.take());
|
||||||
} else {
|
} else {
|
||||||
this.ticTacToe.play(aiPlayers[0].findBestMove(this.ticTacToe));
|
int bestMove = aiPlayers[0].findBestMove(this.ticTacToe);
|
||||||
|
if (this.ticTacToe.play(bestMove) != GameBase.State.INVALID) {
|
||||||
|
ui.setCell(bestMove, "X");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.setNextPlayersTurn();
|
this.setNextPlayersTurn();
|
||||||
if (!isAiPlayer[1]) {
|
if (!isAiPlayer[1]) {
|
||||||
this.ticTacToe.play(this.moveQueuePlayerB.take());
|
this.ticTacToe.play(this.moveQueuePlayerB.take());
|
||||||
} else {
|
} else {
|
||||||
this.ticTacToe.play(aiPlayers[1].findBestMove(this.ticTacToe));
|
int bestMove = aiPlayers[1].findBestMove(this.ticTacToe);
|
||||||
|
if (this.ticTacToe.play(bestMove) != GameBase.State.INVALID) {
|
||||||
|
ui.setCell(bestMove, "O");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.setNextPlayersTurn();
|
this.setNextPlayersTurn();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -214,4 +223,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
|||||||
GlobalEventBus.unregister(this.receivedMessageListener);
|
GlobalEventBus.unregister(this.receivedMessageListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUIReference(UIGameBoard uiGameBoard) {
|
||||||
|
this.ui = uiGameBoard;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user