Everything is broken

This commit is contained in:
2025-12-04 19:43:03 +01:00
parent 7ea45586ae
commit 0993abeec7
13 changed files with 105 additions and 52 deletions

View File

@@ -4,6 +4,7 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBasedGame<T> {
private final int columnSize;
@@ -13,14 +14,13 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
// long is 64 bits. Every game has a limit of 64 cells maximum.
private final long[] playerBitboard;
private int currentTurn;
private AtomicInteger currentTurn = new AtomicInteger(0);
public BitboardGame(int columnSize, int rowSize, int playerCount, Player<T>[] players) {
this.columnSize = columnSize;
this.rowSize = rowSize;
this.players = players;
this.playerBitboard = new long[playerCount];
this.currentTurn = 0;
Arrays.fill(playerBitboard, 0L);
}
@@ -91,11 +91,12 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
public Player<T> getPlayer(int index) {return players[index];}
public int getCurrentPlayerIndex() {
return currentTurn % playerBitboard.length;
System.out.println(currentTurn.get() % playerBitboard.length);
return currentTurn.get() % playerBitboard.length;
}
public int getNextPlayer() {
return (currentTurn + 1) % playerBitboard.length;
return (currentTurn.get() + 1) % playerBitboard.length;
}
public Player<T> getCurrentPlayer(){
@@ -103,6 +104,7 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
}
public void nextTurn() {
currentTurn++;
System.out.println("Incrementing turn");
currentTurn.incrementAndGet();
}
}

View File

@@ -17,19 +17,12 @@ import org.toop.game.players.OnlinePlayer;
* for the local player while receiving moves from other players.
*/
public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements SupportsOnlinePlay {
/** The local player controlled by this client. */
private final Player<T> mainPlayer;
private final int playerTurn;
/**
* Creates behaviour and sets the first local player
* (non-online player) from the given array.
*/
public OnlineThreadBehaviour(T game, Player<T>[] players) {
public OnlineThreadBehaviour(T game) {
super(game);
this.playerTurn = getFirstNotOnlinePlayer(players);
this.mainPlayer = players[this.playerTurn];
}
/** Finds the first non-online player in the array. */
@@ -61,7 +54,7 @@ public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractT
@Override
public void onYourTurn(NetworkEvents.YourTurnResponse event) {
if (!isRunning.get()) return;
int move = mainPlayer.getMove(game.deepCopy());
int move = game.getPlayer(game.getCurrentTurn()).getMove(game.deepCopy());
new EventFlow().addPostEvent(NetworkEvents.SendMove.class, event.clientId(), (short) move).postEvent();
}
@@ -83,9 +76,9 @@ public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractT
@Override
public void gameFinished(NetworkEvents.GameResultResponse event) {
switch(event.condition().toUpperCase()){
case "WIN" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, playerTurn).postEvent();
case "WIN" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, game.getCurrentTurn()).postEvent();
case "DRAW" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, false, AbstractGame.EMPTY).postEvent();
case "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, (playerTurn + 1)%2).postEvent();
case "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, (game.getCurrentTurn() + 1)%2).postEvent();
default -> {
logger.error("Invalid condition");
throw new RuntimeException("Unknown condition");

View File

@@ -1,6 +1,7 @@
package org.toop.game.gameThreads;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
@@ -11,16 +12,15 @@ import org.toop.framework.gameFramework.model.player.AbstractPlayer;
* This is identical to {@link OnlineThreadBehaviour}, but inserts a
* short sleep before delegating to the base implementation.
*/
public class OnlineWithSleepThreadBehaviour extends OnlineThreadBehaviour {
public class OnlineWithSleepThreadBehaviour<T extends TurnBasedGame<T>> extends OnlineThreadBehaviour<T> {
/**
* Creates the behaviour and forwards the players to the base class.
*
* @param game the online-capable turn-based game
* @param players the list of local and remote players
*/
public OnlineWithSleepThreadBehaviour(AbstractGame game, AbstractPlayer[] players) {
super(game, players);
public OnlineWithSleepThreadBehaviour(T game) {
super(game);
}
/**
@@ -32,7 +32,7 @@ public class OnlineWithSleepThreadBehaviour extends OnlineThreadBehaviour {
public void onYourTurn(NetworkEvents.YourTurnResponse event) {
try {
Thread.sleep(1000);
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}

View File

@@ -65,6 +65,7 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
@Override
public int[] getLegalMoves(){
System.out.println(Arrays.toString(translateLegalMoves(getLegalMoves2())));
System.out.println(Long.toBinaryString(getLegalMoves2()));
return translateLegalMoves(getLegalMoves2());
}

View File

@@ -4,8 +4,8 @@ import org.toop.framework.gameFramework.model.player.AbstractAI;
import java.util.Random;
public final class ReversiAIR extends AbstractAI<ReversiR> {
public int getMove(ReversiR game) {
public final class ReversiAIR extends AbstractAI<BitboardReversi> {
public int getMove(BitboardReversi game) {
int[] moves = game.getLegalMoves();
if (moves.length == 0) return -1;

View File

@@ -13,7 +13,7 @@ import org.toop.framework.gameFramework.GameState;
* opening or when no clear best move is found.
* </p>
*/
public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
public final class TicTacToeAIR extends AbstractAI<BitboardTicTacToe> {
/**
* Determines the best move for the given Tic-Tac-Toe game state.
@@ -27,7 +27,7 @@ public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
* @param depth the depth of lookahead for evaluating moves (non-negative)
* @return the index of the best move, or -1 if no moves are available
*/
public int getMove(TicTacToeR game) {
public int getMove(BitboardTicTacToe game) {
int depth = 9;
assert game != null;
final int[] legalMoves = game.getLegalMoves();
@@ -71,8 +71,8 @@ public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
* @param maximizing true if the AI is to maximize score, false if minimizing
* @return the score of the move
*/
private int getMoveScore(TicTacToeR game, int depth, int move, boolean maximizing) {
final TicTacToeR copy = game.deepCopy();
private int getMoveScore(BitboardTicTacToe game, int depth, int move, boolean maximizing) {
final BitboardTicTacToe copy = game.deepCopy();
final PlayResult result = copy.play(move);
GameState state = result.state();