small fix

This commit is contained in:
Ticho Hidding
2025-10-06 02:34:48 +02:00
parent 20a7fd7a3e
commit 9144cb453e
2 changed files with 24 additions and 30 deletions

View File

@@ -177,14 +177,15 @@ public final class TicTacToeLayer extends Layer {
final OnlineGameState onlineGameState = new OnlineGameState(); final OnlineGameState onlineGameState = new OnlineGameState();
onlineGameState.clientId = clientId; onlineGameState.clientId = clientId;
new EventFlow() //new EventFlow()
.listen(NetworkEvents.GameMoveResponse.class,respEvent -> onMoveResponse(onlineGameState, respEvent)); // .listen(NetworkEvents.GameMoveResponse.class,respEvent -> onMoveResponse(onlineGameState, respEvent));
new EventFlow() new EventFlow()
.listen(this::yourTurnResponse) .listen(this::yourTurnResponse)
.listen(this::handleChallengeResponse) .listen(this::handleChallengeResponse)
.listen(this::handleServerGameStart) .listen(this::handleServerGameStart)
.listen(this::handleReceivedMessage); .listen(this::handleReceivedMessage)
.listen(this::onMoveResponse);
new EventFlow().addPostEvent(new NetworkEvents.SendLogin(clientId, information.playerName()[0])) new EventFlow().addPostEvent(new NetworkEvents.SendLogin(clientId, information.playerName()[0]))
.postEvent(); .postEvent();
@@ -192,10 +193,10 @@ public final class TicTacToeLayer extends Layer {
new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, "tic-tac-toe")) new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, "tic-tac-toe"))
.postEvent(); .postEvent();
new EventFlow().addPostEvent(new NetworkEvents.SendCommand(clientId, "message hello-world"))
.postEvent();
while (running) { while (running) {
try {
Thread.sleep(250);
}catch (InterruptedException exception) {}
boolean hasStarted = gameHasStarted.get(); boolean hasStarted = gameHasStarted.get();
if (hasStarted) { if (hasStarted) {
onlineGameState.firstPlayerIsMe = firstPlayerIsMe.get(); onlineGameState.firstPlayerIsMe = firstPlayerIsMe.get();
@@ -206,38 +207,26 @@ public final class TicTacToeLayer extends Layer {
currentPlayerMove = 'O'; currentPlayerMove = 'O';
} }
if(!information.isPlayerHuman()[0]){ if(!information.isPlayerHuman()[0]){
if (onlineGameState.firstPlayerIsMe && ticTacToe.getCurrentTurn()%2 == 0 || !onlineGameState.firstPlayerIsMe && ticTacToe.getCurrentTurn()%2 == 1) { boolean myTurn = (onlineGameState.firstPlayerIsMe && ticTacToe.getCurrentTurn() % 2 == 0)
|| (!onlineGameState.firstPlayerIsMe && ticTacToe.getCurrentTurn() % 2 == 1);
if (myTurn) {
Game.Move move; Game.Move move;
move = ticTacToeAI.findBestMove(ticTacToe, compurterDifficultyToDepth(10, 10)); move = ticTacToeAI.findBestMove(ticTacToe, compurterDifficultyToDepth(10, 10));
new EventFlow().addPostEvent(new NetworkEvents.SendMove(clientId, (short) move.position())) new EventFlow().addPostEvent(new NetworkEvents.SendMove(clientId, (short) move.position()))
.postEvent(); .postEvent();
final Game.State state = ticTacToe.play(move);
drawSymbol(move);
if (Game.State.NORMAL != state) {
System.out.println("Win Or Draw");//todo
running = false;
}
} }
} }
else { else {
try { try {
final Game.Move wants = playerMoveQueue.take(); final Game.Move wants = playerMoveQueue.take();
final Game.Move[] legalMoves = ticTacToe.getLegalMoves(); final Game.Move[] legalMoves = ticTacToe.getLegalMoves();
Game.State state = Game.State.NORMAL;
for (final Game.Move legalMove : legalMoves) { for (final Game.Move legalMove : legalMoves) {
if (legalMove.position() == wants.position() && legalMove.value() == wants.value()) { if (legalMove.position() == wants.position() && legalMove.value() == wants.value()) {
state = ticTacToe.play(wants);
new EventFlow().addPostEvent(new NetworkEvents.SendMove(clientId, (short) wants.position())) new EventFlow().addPostEvent(new NetworkEvents.SendMove(clientId, (short) wants.position()))
.postEvent(); .postEvent();
drawSymbol(wants);
break; break;
} }
} }
if(Game.State.NORMAL != state){
System.out.println("Win Or Draw");
running = false;
}
} catch (InterruptedException exception) { } catch (InterruptedException exception) {
return; return;
} }
@@ -263,16 +252,21 @@ public final class TicTacToeLayer extends Layer {
gameHasStarted.set(true); gameHasStarted.set(true);
} }
private void onMoveResponse(OnlineGameState ogs, NetworkEvents.GameMoveResponse resp) { private void onMoveResponse(NetworkEvents.GameMoveResponse resp) {
ogs.receivedMove = Long.parseLong(resp.move()); char playerChar;
char opponentChar; if (resp.player().equals(information.playerName()[0]) && firstPlayerIsMe.get()
if (firstPlayerIsMe.get()) { || !resp.player().equals(information.playerName()[0]) && !firstPlayerIsMe.get()) {
opponentChar = 'O'; playerChar = 'X';
} }
else { else{
opponentChar = 'X'; playerChar = 'O';
} }
ticTacToe.play(new Game.Move(Integer.parseInt(resp.move()),opponentChar)); Game.Move move =new Game.Move(Integer.parseInt(resp.move()),playerChar);
Game.State state = ticTacToe.play(move);
if (state != Game.State.NORMAL) { //todo differentiate between future draw guaranteed and is currently a draw
gameHasStarted.set(false);
}
drawSymbol(move);
} }
private void handleChallengeResponse(NetworkEvents.ChallengeResponse resp) { private void handleChallengeResponse(NetworkEvents.ChallengeResponse resp) {

View File

@@ -62,7 +62,7 @@ public class NetworkEvents extends EventsBase {
implements EventWithoutSnowflake {} implements EventWithoutSnowflake {}
/** Response indicating a game move occurred. */ /** Response indicating a game move occurred. */
public record GameMoveResponse(long clientId, String player, String details, String move) public record GameMoveResponse(long clientId, String player, String move, String details)
implements EventWithoutSnowflake {} implements EventWithoutSnowflake {}
/** Response indicating it is the player's turn. */ /** Response indicating it is the player's turn. */