Merge bitboards into development (#285)

* added new classes for the games that use bitboards instead. also combined game with turnbasedgame

* (DOES NOT COMPILE) In-between commit

* turn updates

* smalle fixes aan turn updates

* Bitboard implemented with scuffed TicTacToe translation done by game. This should be done by the view.

* Almost done with implementing bitboards. Reversi is broken and artifical players don't work yet.

* better human/ai selector with bot selection and depth on TicTacToeAIR

* fixed getLegalMoves

* depth + thinktime back to AIs, along with a a specific TicTacToeAIRSleep

* fixed overlapping back and disconnect buttons

* Changed to debug instead of info

* changed the transitionNextCustom to be easier to use

* added getAllWidgets to WidgetContainer

* Correct back view

* added replacePrevious in ViewWidget

* added removeIndexFromPreviousChain

* fixed incorrect index counting

* Fixt wrong view order

* fixed? getLegalMoves

* Everything is broken

* Removed todo

* fixed getLegalMoves & getFlips

* Challenge popups "Fixed"

* Fixed local and online play for both games

* Popups now remove themselves

* Removed souts for debugging

* localize the ChallengePopup text

* made the game text a header instead

* made more classes deepClonable.

* fixed getAllWidgets

* Added comment

* Escape popup

* fixed redundant container

* Made all network events async again

* Escape remove popup

* Working escape menu

* Removed old AI and old files. Added a new generic random AI. game no longer deals with translation.

* Drawing of board on canvas is now done from bitboards rather than translating.

* Added a method getWinner() to game interface.Controller now tells gameThreads how to deal with drawing UI and sending a move to server.

* Added find functionality

* Added a ChatGPT generated MiniMaxAI based on the old MiniMaxAI but with alpha-beta pruning and heuristics for Reversi

* Removed System-Outs to clean up console

* Update BitGameCanvas.java

* Merge fixes

* Removed unused imports

---------

Co-authored-by: ramollia <>
Co-authored-by: michiel301b <m.brands.3@st.hanze.nl>
Co-authored-by: lieght <49651652+BAFGdeJong@users.noreply.github.com>
This commit is contained in:
Stef
2025-12-08 18:23:06 +01:00
committed by GitHub
parent adc7b1a8f3
commit 912d25c01f
52 changed files with 1330 additions and 1602 deletions

View File

@@ -0,0 +1,6 @@
package org.toop.framework.gameFramework;
@FunctionalInterface
public interface LongPairConsumer {
void accept(long a, long b);
}

View File

@@ -0,0 +1,18 @@
package org.toop.framework.gameFramework.controller;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
import org.toop.framework.networking.events.NetworkEvents;
public interface GameController extends Controllable, UpdatesGameUI {
/** Called when it is this player's turn to make a move. */
void onYourTurn(NetworkEvents.YourTurnResponse event);
/** Called when a move from another player is received. */
void onMoveReceived(NetworkEvents.GameMoveResponse event);
/** Called when the game has finished, with the final result. */
void gameFinished(NetworkEvents.GameResultResponse event);
void sendMove(long clientId, long move);
}

View File

@@ -1,108 +0,0 @@
package org.toop.framework.gameFramework.model.game;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.Arrays;
public abstract class AbstractGame<T extends TurnBasedGame<T>> implements TurnBasedGame<T> {
private final int playerCount; // How many players are playing
private final Player<T>[] players;
private int turn = 0; // What turn it is in the game
/** Constant representing an empty position on the board. */
public static final int EMPTY = -1;
/** Number of rows in the game board. */
private final int rowSize;
/** Number of columns in the game board. */
private final int columnSize;
/** The game board stored as a one-dimensional array. */
private final int[] board;
protected AbstractGame(int rowSize, int columnSize, int playerCount, Player<T>[] players) {
assert rowSize > 0 && columnSize > 0;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.players = players;
board = new int[rowSize * columnSize];
Arrays.fill(board, EMPTY);
this.playerCount = playerCount;
}
protected AbstractGame(AbstractGame<T> other){
this.rowSize = other.rowSize;
this.columnSize = other.columnSize;
this.board = other.board.clone();
this.playerCount = other.playerCount;
this.turn = other.turn;
// TODO: Make this a deep copy, add deep copy interface to Player
this.players = other.players;
}
public static boolean contains(int[] array, int value) {
// O(n)
for (int element : array){
if (element == value) return true;
}
return false;
}
public Player<T> getPlayer(int index) {
return players[index];
}
public int getPlayerCount(){return this.playerCount;}
protected void nextTurn() {
turn += 1;
}
public int getCurrentTurn() {
return turn % playerCount;
}
protected void setBoard(int position) {
setBoard(position, getCurrentTurn());
}
protected void setBoard(int position, int player) {
this.board[position] = player;
}
/**
* Returns the number of rows in the board.
*
* @return number of rows
*/
public int getRowSize() {
return this.rowSize;
}
/**
* Returns the number of columns in the board.
*
* @return number of columns
*/
public int getColumnSize() {
return this.columnSize;
}
/**
* Returns a copy of the current board state.
*
* @return a cloned array representing the board
*/
public int[] getBoard() {
return this.board.clone();
}
}

View File

@@ -0,0 +1,5 @@
package org.toop.framework.gameFramework.model.game;
public interface BoardProvider {
long[] getBoard();
}

View File

@@ -1,5 +1,5 @@
package org.toop.framework.gameFramework.model.game;
public interface DeepCopyable<T extends TurnBasedGame<T>> {
public interface DeepCopyable<T> {
T deepCopy();
}

View File

@@ -12,7 +12,7 @@ public interface Playable {
*
* @return an array of integers representing legal moves
*/
int[] getLegalMoves();
long getLegalMoves();
/**
* Plays the given move and returns the resulting game state.
@@ -20,5 +20,5 @@ public interface Playable {
* @param move the move to apply
* @return the {@link GameState} and additional info after the move
*/
PlayResult play(int move);
PlayResult play(long move);
}

View File

@@ -10,11 +10,11 @@ import org.toop.framework.networking.events.NetworkEvents;
public interface SupportsOnlinePlay {
/** Called when it is this player's turn to make a move. */
void onYourTurn(NetworkEvents.YourTurnResponse event);
void onYourTurn(long clientId);
/** Called when a move from another player is received. */
void onMoveReceived(NetworkEvents.GameMoveResponse event);
void onMoveReceived(long move);
/** Called when the game has finished, with the final result. */
void gameFinished(NetworkEvents.GameResultResponse event);
void gameFinished(String condition);
}

View File

@@ -1,5 +1,7 @@
package org.toop.framework.gameFramework.model.game;
public interface TurnBasedGame<T extends TurnBasedGame<T>> extends Playable, DeepCopyable<T>, PlayerProvider<T> {
public interface TurnBasedGame<T extends TurnBasedGame<T>> extends Playable, DeepCopyable<T>, PlayerProvider<T>, BoardProvider {
int getCurrentTurn();
int getPlayerCount();
int getWinner();
}

View File

@@ -2,9 +2,12 @@ package org.toop.framework.gameFramework.model.game.threadBehaviour;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.gameFramework.LongPairConsumer;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
* Base class for thread-based game behaviours.
@@ -13,8 +16,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
* a running flag, a game reference, and a logger.
* Subclasses implement the actual game-loop logic.
*/
public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implements ThreadBehaviour<T> {
public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implements ThreadBehaviour {
private LongPairConsumer onSendMove;
private Runnable onUpdateUI;
/** Indicates whether the game loop or event processing is active. */
protected final AtomicBoolean isRunning = new AtomicBoolean();
@@ -32,4 +36,26 @@ public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implem
public AbstractThreadBehaviour(T game) {
this.game = game;
}
protected void updateUI(){
if (onUpdateUI != null) {
onUpdateUI.run();
}
}
protected void sendMove(long clientId, long move){
if (onSendMove != null) {
onSendMove.accept(clientId, move);
}
}
@Override
public void setOnUpdateUI(Runnable onUpdateUI) {
this.onUpdateUI = onUpdateUI;
}
@Override
public void setOnSendMove(LongPairConsumer onSendMove) {
this.onSendMove = onSendMove;
}
}

View File

@@ -1,11 +1,18 @@
package org.toop.framework.gameFramework.model.game.threadBehaviour;
import org.toop.framework.gameFramework.LongPairConsumer;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import java.util.function.Consumer;
/**
* Strategy interface for controlling game thread behavior.
* <p>
* Defines how a game's execution is started, stopped, and which player is active.
*/
public interface ThreadBehaviour<T extends TurnBasedGame<T>> extends Controllable {
public interface ThreadBehaviour extends Controllable {
void setOnUpdateUI(Runnable onUpdateUI);
void setOnSendMove(LongPairConsumer onSendMove);
}

View File

@@ -0,0 +1,7 @@
package org.toop.framework.gameFramework.model.player;
import org.toop.framework.gameFramework.model.game.DeepCopyable;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface AI<T extends TurnBasedGame<T>> extends MoveProvider<T>, DeepCopyable<AI<T>> {
}

View File

@@ -12,6 +12,6 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
*
* @param <T> the specific type of game this AI can play, extending {@link GameR}
*/
public abstract class AbstractAI<T extends TurnBasedGame> implements MoveProvider<T> {
public abstract class AbstractAI<T extends TurnBasedGame<T>> implements AI<T> {
// Concrete AI implementations should override findBestMove(T game, int depth)
}

View File

@@ -16,15 +16,17 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
* </p>
*/
public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Player<T> {
private int playerIndex = -1;
private Logger logger = LogManager.getLogger(this.getClass());
private final Logger logger = LogManager.getLogger(this.getClass());
private final String name;
protected AbstractPlayer(String name) {
this.name = name;
}
protected AbstractPlayer(AbstractPlayer<T> other) {
this.name = other.name;
}
/**
* Determines the next move based on the provided game state.
* <p>
@@ -37,7 +39,7 @@ public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Play
* @return an integer representing the chosen move
* @throws UnsupportedOperationException if the method is not overridden
*/
public int getMove(T gameCopy) {
public long getMove(T gameCopy) {
logger.error("Method getMove not implemented.");
throw new UnsupportedOperationException("Not supported yet.");
}

View File

@@ -2,6 +2,6 @@ package org.toop.framework.gameFramework.model.player;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface MoveProvider<T extends TurnBasedGame> {
int getMove(T game);
public interface MoveProvider<T extends TurnBasedGame<T>> {
long getMove(T game);
}

View File

@@ -1,6 +1,7 @@
package org.toop.framework.gameFramework.model.player;
import org.toop.framework.gameFramework.model.game.DeepCopyable;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface Player<T extends TurnBasedGame<T>> extends NameProvider, MoveProvider<T> {
public interface Player<T extends TurnBasedGame<T>> extends NameProvider, MoveProvider<T>, DeepCopyable<Player<T>> {
}

View File

@@ -10,10 +10,6 @@ import org.toop.framework.eventbus.events.GenericEvent;
* player actions, and game completion.
*/
public class GUIEvents extends EventsBase {
/** Event to refresh or redraw the game canvas. */
public record RefreshGameCanvas() implements GenericEvent {}
/**
* Event indicating the game has ended.
*
@@ -23,8 +19,8 @@ public class GUIEvents extends EventsBase {
public record GameEnded(boolean winOrTie, int winner) implements GenericEvent {}
/** Event indicating a player has attempted a move. */
public record PlayerAttemptedMove(int move) implements GenericEvent {}
public record PlayerAttemptedMove(long move) implements GenericEvent {}
/** Event indicating a player is hovering over a move (for UI feedback). */
public record PlayerMoveHovered(int move) implements GenericEvent {}
public record PlayerMoveHovered(long move) implements GenericEvent {}
}