Fixed local and online play for both games

This commit is contained in:
2025-12-04 20:50:58 +01:00
parent be57e25a48
commit 86e740a34a
6 changed files with 42 additions and 42 deletions

View File

@@ -14,7 +14,7 @@ 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 AtomicInteger currentTurn = new AtomicInteger(0);
private int currentTurn = 0;
public BitboardGame(int columnSize, int rowSize, int playerCount, Player<T>[] players) {
this.columnSize = columnSize;
@@ -91,12 +91,12 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
public Player<T> getPlayer(int index) {return players[index];}
public int getCurrentPlayerIndex() {
System.out.println(currentTurn.get() % playerBitboard.length);
return currentTurn.get() % playerBitboard.length;
System.out.println(currentTurn % playerBitboard.length);
return currentTurn % playerBitboard.length;
}
public int getNextPlayer() {
return (currentTurn.get() + 1) % playerBitboard.length;
return (currentTurn + 1) % playerBitboard.length;
}
public Player<T> getCurrentPlayer(){
@@ -105,6 +105,6 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
public void nextTurn() {
System.out.println("Incrementing turn");
currentTurn.incrementAndGet();
currentTurn++;
}
}

View File

@@ -16,18 +16,14 @@ import org.toop.framework.gameFramework.model.player.Player;
*/
public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
/** All players participating in the game. */
private final Player<T>[] players;
/**
* Creates a fixed-rate behaviour for a local turn-based game.
*
* @param game the game instance
* @param players the list of players in turn order
*/
public LocalFixedRateThreadBehaviour(T game, Player<T>[] players) {
public LocalFixedRateThreadBehaviour(T game) {
super(game);
this.players = players;
}
/** Starts the game loop thread if not already running. */
@@ -52,7 +48,7 @@ public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends A
*/
@Override
public void run() {
final int UPS = 60;
final int UPS = 1;
final long UPDATE_INTERVAL = 1_000_000_000L / UPS;
long nextUpdate = System.nanoTime();

View File

@@ -20,6 +20,9 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
public BitboardTicTacToe(Player<BitboardTicTacToe>[] players) {
super(3, 3, 2, players);
}
public BitboardTicTacToe(BitboardTicTacToe other) {
super(other);
}
@Override
public int[] getLegalMoves(){
@@ -28,7 +31,7 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
@Override
public PlayResult play(int move) {
return new PlayResult(play2(translateMove(move)), getCurrentPlayerIndex());
return play2(translateMove(move));
}
public long getLegalMoves2() {
@@ -39,24 +42,34 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
return (~taken) & 0x1ffL;
}
public GameState play2(long move) {
public PlayResult play2(long move) {
// Player loses if move is invalid
if ((move & getLegalMoves2()) == 0 || Long.bitCount(move) != 1){
return new PlayResult(GameState.WIN, getNextPlayer());
}
// Move is legal, make move
long playerBitboard = getPlayerBitboard(getCurrentPlayerIndex());
playerBitboard |= move;
setPlayerBitboard(getCurrentPlayerIndex(), playerBitboard);
// Check if current player won
if (checkWin(playerBitboard)) {
return new PlayResult(GameState.WIN, getCurrentPlayerIndex());
}
// Proceed to next turn
nextTurn();
if (checkWin(playerBitboard)) {
return GameState.WIN;
}
// Check for early draw
if (getLegalMoves2() == 0L || checkEarlyDraw()) {
return GameState.DRAW;
return new PlayResult(GameState.DRAW, -1);
}
return GameState.NORMAL;
// Nothing weird happened, continue on as normal
return new PlayResult(GameState.NORMAL, -1);
}
private boolean checkWin(long board) {
@@ -94,9 +107,8 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
return translateBoard();
}
// TODO: Implement
@Override
public BitboardTicTacToe deepCopy() {
return this;
return new BitboardTicTacToe(this);
}
}