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

This commit is contained in:
2025-12-04 00:49:49 +01:00
parent 34d83ff7a5
commit f4ee992166
10 changed files with 83 additions and 49 deletions

View File

@@ -9,10 +9,11 @@ import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import java.util.function.Consumer;
public abstract class GameCanvas<T extends AbstractGame> implements DrawPlayerMove, DrawPlayerHover {
public abstract class GameCanvas<T extends TurnBasedGame<T>> implements DrawPlayerMove, DrawPlayerHover {
protected record Cell(float x, float y, float width, float height) {
public boolean isInside(double x, double y) {
return x >= this.x && x <= this.x + width &&

View File

@@ -2,11 +2,11 @@ package org.toop.app.canvas;
import javafx.scene.paint.Color;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.game.games.tictactoe.TicTacToeR;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
import java.util.function.Consumer;
public final class TicTacToeCanvas extends GameCanvas<TicTacToeR> {
public final class TicTacToeCanvas extends GameCanvas<BitboardTicTacToe> {
public TicTacToeCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, Color.TRANSPARENT, width, height, 3, 3, 30, false, onCellClicked,null);
}

View File

@@ -3,6 +3,7 @@ package org.toop.app.gameControllers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.gameFramework.controller.UpdatesGameUI;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.app.canvas.GameCanvas;
import org.toop.framework.networking.events.NetworkEvents;
@@ -18,7 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public abstract class AbstractGameController<T extends AbstractGame<T>> implements UpdatesGameUI, ThreadBehaviour<T> {
public abstract class AbstractGameController<T extends TurnBasedGame<T>> implements UpdatesGameUI, ThreadBehaviour<T> {
protected final EventFlow eventFlow = new EventFlow();
protected final List<Consumer<?>> listeners = new ArrayList<>();
@@ -33,18 +34,13 @@ public abstract class AbstractGameController<T extends AbstractGame<T>> implemen
protected final GameCanvas<T> canvas;
private final Player<T>[] players; // List of players, can't be changed.
protected final T game; // Reference to game instance
protected final TurnBasedGame<T> game; // Reference to game instance
private final ThreadBehaviour<T> gameThreadBehaviour;
// TODO: Change gameType to automatically happen with either dependency injection or something else.
// TODO: Make visualisation of moves a behaviour.
protected AbstractGameController(GameCanvas<T> canvas, Player<T>[] players, T game, ThreadBehaviour<T> gameThreadBehaviour, String gameType) {
logger.info("Creating AbstractGameController");
// Make sure player list matches expected size
if (players.length != game.getPlayerCount()){
logger.error("Player count mismatch");
throw new IllegalArgumentException("players and game's players must have same length");
}
this.canvas = canvas;
this.players = players;

View File

@@ -33,7 +33,7 @@ public class ReversiController extends AbstractGameController<ReversiR> {
private void onHoverMove(GUIEvents.PlayerMoveHovered event){
int cellEntered = event.move();
canvas.drawPlayerHover(-1, cellEntered, game);
//canvas.drawPlayerHover(-1, cellEntered, game);
/*// (information.players[game.getCurrentTurn()].isHuman) {
int[] legalMoves = game.getLegalMoves();
boolean isLegalMove = false;
@@ -72,20 +72,20 @@ public class ReversiController extends AbstractGameController<ReversiR> {
}
}
final int[] flipped = game.getMostRecentlyFlippedPieces();
//final int[] flipped = game.getMostRecentlyFlippedPieces();
final SequentialTransition animation = new SequentialTransition();
final Color fromColor = getCurrentPlayerIndex() == 0? Color.WHITE : Color.BLACK;
final Color toColor = getCurrentPlayerIndex() == 0? Color.BLACK : Color.WHITE;
if (animate && flipped != null) {
/*if (animate && flipped != null) {
for (final int flip : flipped) {
canvas.clear(flip);
canvas.drawDot(fromColor, flip);
animation.getChildren().addFirst(canvas.flipDot(fromColor, toColor, flip));
}
}
}*/
animation.setOnFinished(_ -> {

View File

@@ -12,17 +12,17 @@ import org.toop.game.gameThreads.LocalThreadBehaviour;
import org.toop.game.gameThreads.OnlineThreadBehaviour;
import org.toop.game.players.LocalPlayer;
import org.toop.app.widget.WidgetContainer;
import org.toop.game.games.tictactoe.TicTacToeR;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
public class TicTacToeController extends AbstractGameController<TicTacToeR> {
public class TicTacToeController extends AbstractGameController<BitboardTicTacToe> {
public TicTacToeController(Player<TicTacToeR>[] players, boolean local) {
TicTacToeR ticTacToeR = new TicTacToeR(players);
public TicTacToeController(Player<BitboardTicTacToe>[] players, boolean local) {
BitboardTicTacToe BitboardTicTacToe = new BitboardTicTacToe(players);
super(
new TicTacToeCanvas(Color.GRAY, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {new EventFlow().addPostEvent(GUIEvents.PlayerAttemptedMove.class, c).postEvent();}),
players,
ticTacToeR,
local ? new LocalThreadBehaviour(ticTacToeR, players) : new OnlineThreadBehaviour<>(ticTacToeR, players), // TODO: Player order matters here, this won't work atm
BitboardTicTacToe,
local ? new LocalThreadBehaviour(BitboardTicTacToe, players) : new OnlineThreadBehaviour<>(BitboardTicTacToe, players), // TODO: Player order matters here, this won't work atm
"TicTacToe");
initUI();
@@ -31,7 +31,7 @@ public class TicTacToeController extends AbstractGameController<TicTacToeR> {
//new EventFlow().listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer lp){lp.setMove(event.move());}});
}
public TicTacToeController(Player<TicTacToeR>[] players) {
public TicTacToeController(Player<BitboardTicTacToe>[] players) {
this(players, true);
}
@@ -53,7 +53,7 @@ public class TicTacToeController extends AbstractGameController<TicTacToeR> {
int[] board = game.getBoard();
// Draw each square
for (int i = 0; i < board.length; i++){
for (int i = 0; i < 9; i++){
// If square isn't empty, draw player move
if (board[i] != AbstractGame.EMPTY){
canvas.drawPlayerMove(board[i], i);