fast server connection

This commit is contained in:
ramollia
2025-10-07 12:40:12 +02:00
parent 3d9914c927
commit e12e48b4fb
2 changed files with 29 additions and 5 deletions

View File

@@ -146,7 +146,7 @@ public final class ConnectedLayer extends Layer {
.listen(NetworkEvents.GameMatchResponse.class, e -> {
if (e.clientId() == this.clientId) {
pollTimer.cancel();
App.activate(new TicTacToeLayer(information, this.clientId));
App.activate(new TicTacToeLayer(information, this.clientId, e));
}
}, false).postEvent();
// ^

View File

@@ -39,7 +39,6 @@ public final class TicTacToeLayer extends Layer {
private final BlockingQueue<Game.Move> playerMoveQueue = new LinkedBlockingQueue<>();
// Todo: set these from the server
private char currentPlayerMove = Game.EMPTY;
private String player2Name = "";
final AtomicBoolean firstPlayerIsMe = new AtomicBoolean(true);
@@ -56,8 +55,16 @@ public final class TicTacToeLayer extends Layer {
playerMoveQueue.put(new Game.Move(cell, 'O'));
}
} else {
if (information.isPlayerHuman()[0] && currentPlayerMove != Game.EMPTY) {
playerMoveQueue.put(new Game.Move(cell, firstPlayerIsMe.get()? 'X' : 'O'));
if (information.isPlayerHuman()[0]) {
if (ticTacToe.get().getCurrentTurn() == 0) {
if (firstPlayerIsMe.get()) {
playerMoveQueue.put(new Game.Move(cell, 'X'));
}
} else {
if (!firstPlayerIsMe.get()) {
playerMoveQueue.put(new Game.Move(cell, 'O'));
}
}
}
}
} catch (InterruptedException _) {}
@@ -88,6 +95,18 @@ public final class TicTacToeLayer extends Layer {
reload();
}
public TicTacToeLayer(GameInformation information, long clientID, NetworkEvents.GameMatchResponse resp) {
this(information);
Thread a = new Thread(this::serverGameThread);
a.setDaemon(false);
a.start();
handleServerGameStart(resp);
reload();
}
@Override
public void reload() {
popAll();
@@ -256,7 +275,11 @@ public final class TicTacToeLayer extends Layer {
if (state != Game.State.NORMAL) { //todo differentiate between future draw guaranteed and is currently a draw
if (state == Game.State.WIN) {
App.push(new GameFinishedPopup(false, information.playerName()[ticTacToe.get().getCurrentTurn()]));
if (!resp.player().equalsIgnoreCase(player2Name)) {
App.push(new GameFinishedPopup(false, information.playerName()[0]));
} else {
App.push(new GameFinishedPopup(false, player2Name));
}
} else if (state == Game.State.DRAW) {
App.push(new GameFinishedPopup(true, ""));
}
@@ -278,6 +301,7 @@ public final class TicTacToeLayer extends Layer {
if (information.isPlayerHuman()[0]) {
try {
position = playerMoveQueue.take().position();
System.out.println("TEST" + position);
} catch (InterruptedException _) {}
} else {
final Game.Move move = ticTacToeAI.findBestMove(ticTacToe.get(), compurterDifficultyToDepth(10,