Fixed race condition on startup of AI vs Player game

# Conflicts:
#	src/main/java/org/toop/frontend/UI/RemoteGameSelector.java
This commit is contained in:
Ticho Hidding
2025-09-22 12:04:34 +02:00
committed by lieght
parent 7a0ab2f7f4
commit 7e23aaa849
3 changed files with 16 additions and 5 deletions

View File

@@ -92,6 +92,7 @@ public class LocalGameSelector extends JFrame {
}
cardLayout.show(cards, "TicTacToe");
}
lttt.startThreads();
}
public void showMainMenu() {

View File

@@ -104,7 +104,7 @@ public class RemoteGameSelector {
// });
GlobalEventBus.post(
new NetworkEvents.SendCommand(
new Events.ServerEvents.SendCommand(
connectionId,
"create_game",
nameTextField.getText(),
@@ -130,8 +130,9 @@ public class RemoteGameSelector {
frame.remove(mainMenu);
localTicTacToe =
LocalTicTacToe.createRemote(
ipTextField.getText(), Integer.parseInt(portTextField.getText())); // TODO: Unsafe parse
UIGameBoard ttt = new UIGameBoard(localTicTacToe, this); // TODO: Fix later
ipTextField.getText(), portTextField.getText());
UIGameBoard ttt = new UIGameBoard(localTicTacToe, this);
localTicTacToe.startThreads();
frame.add(ttt.getTTTPanel()); // TODO: Fix later
frame.revalidate();
frame.repaint();

View File

@@ -16,6 +16,8 @@ import org.toop.game.tictactoe.TicTacToe;
import org.toop.game.tictactoe.ai.MinMaxTicTacToe;
import java.util.function.Supplier;
import static java.lang.Thread.sleep;
/**
* A representation of a local tic-tac-toe game. Calls are made to a server for information about
* current game state. MOST OF THIS CODE IS TRASH, THROW IT OUT OF THE WINDOW AFTER DEMO.
@@ -76,7 +78,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
this.connectionId = this.createConnection(ip, port);
this.createGame("X", "O");
this.isLocal = false;
this.executor.submit(this::remoteGameThread);
//this.executor.submit(this::remoteGameThread);
}
private LocalTicTacToe(boolean[] aiFlags) {
@@ -91,7 +93,14 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
this.isLocal = true;
this.executor.submit(this::localGameThread);
//this.executor.submit(this::localGameThread);
}
public void startThreads(){
if (isLocal) {
this.executor.submit(this::localGameThread);
}else {
this.executor.submit(this::remoteGameThread);
}
}
public static LocalTicTacToe createLocal(boolean[] aiPlayers) {