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

This commit is contained in:
2025-12-04 14:19:48 +01:00
parent f4ee992166
commit 21b489bbe7
21 changed files with 804 additions and 286 deletions

View File

@@ -0,0 +1,237 @@
package org.toop.app.canvas;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import java.util.function.Consumer;
@Deprecated
public abstract class BaseGameCanvas<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 &&
y >= this.y && y <= this.y + height;
}
}
protected final Canvas canvas;
protected final GraphicsContext graphics;
protected final Color color;
protected final Color backgroundColor;
protected final int width;
protected final int height;
protected final int rowSize;
protected final int columnSize;
protected final int gapSize;
protected final boolean edges;
protected final Cell[] cells;
private Consumer<Integer> onCellCLicked;
public void setOnCellClicked(Consumer<Integer> onClick) {
this.onCellCLicked = onClick;
}
protected BaseGameCanvas(Color color, Color backgroundColor, int width, int height, int rowSize, int columnSize, int gapSize, boolean edges, Consumer<Integer> onCellClicked, Consumer<Integer> newCellEntered) {
canvas = new Canvas(width, height);
graphics = canvas.getGraphicsContext2D();
this.onCellCLicked = onCellClicked;
this.color = color;
this.backgroundColor = backgroundColor;
this.width = width;
this.height = height;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.gapSize = gapSize;
this.edges = edges;
cells = new Cell[rowSize * columnSize];
final float cellWidth = ((float) width - gapSize * rowSize - gapSize) / rowSize;
final float cellHeight = ((float) height - gapSize * columnSize - gapSize) / columnSize;
for (int y = 0; y < columnSize; y++) {
final float startY = y * cellHeight + y * gapSize + gapSize;
for (int x = 0; x < rowSize; x++) {
final float startX = x * cellWidth + x * gapSize + gapSize;
cells[x + y * rowSize] = new Cell(startX, startY, cellWidth, cellHeight);
}
}
canvas.setOnMouseClicked(event -> {
if (event.getButton() != MouseButton.PRIMARY) {
return;
}
final int column = (int) ((event.getX() / this.width) * rowSize);
final int row = (int) ((event.getY() / this.height) * columnSize);
final Cell cell = cells[column + row * rowSize];
if (cell.isInside(event.getX(), event.getY())) {
event.consume();
this.onCellCLicked.accept(column + row * rowSize);
}
});
render();
}
private void render() {
graphics.setFill(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setFill(color);
for (int x = 0; x < rowSize - 1; x++) {
final float start = cells[x].x + cells[x].width;
graphics.fillRect(start, gapSize, gapSize, height - gapSize * 2);
}
for (int y = 0; y < columnSize - 1; y++) {
final float start = cells[y * rowSize].y + cells[y * rowSize].height;
graphics.fillRect(gapSize, start, width - gapSize * 2, gapSize);
}
if (edges) {
graphics.fillRect(0, 0, width, gapSize);
graphics.fillRect(0, 0, gapSize, height);
graphics.fillRect(width - gapSize, 0, gapSize, height);
graphics.fillRect(0, height - gapSize, width, gapSize);
}
}
public void fill(Color color, int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.setFill(color);
graphics.fillRect(x, y, width, height);
}
public void clear(int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.clearRect(x, y, width, height);
graphics.setFill(backgroundColor);
graphics.fillRect(x, y, width, height);
}
public void clearAll() {
for (int i = 0; i < cells.length; i++) {
clear(i);
}
}
@Override
public void drawPlayerMove(int player, int move) {
final float x = cells[move].x() + gapSize;
final float y = cells[move].y() + gapSize;
final float width = cells[move].width() - gapSize * 2;
final float height = cells[move].height() - gapSize * 2;
graphics.setFill(color);
graphics.setFont(Font.font("Arial", 40)); // TODO different font and size
graphics.fillText(String.valueOf(player), x + width, y + height);
}
public void drawDot(Color color, int cell) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
final float width = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
graphics.setFill(color);
graphics.fillOval(x, y, width, height);
}
public void drawInnerDot(Color color, int cell, boolean slightlyBigger) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
float multiplier = slightlyBigger?1.4f:1.5f;
final float width = (cells[cell].width() - gapSize * 2)/multiplier;
final float height = (cells[cell].height() - gapSize * 2)/multiplier;
float offset = slightlyBigger?5f:4f;
graphics.setFill(color);
graphics.fillOval(x + width/offset, y + height/offset, width, height);
}
private void drawDotScaled(Color color, int cell, double scale) {
final float cx = cells[cell].x() + gapSize;
final float cy = cells[cell].y() + gapSize;
final float fullWidth = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
final float scaledWidth = (float)(fullWidth * scale);
final float offsetX = (fullWidth - scaledWidth) / 2;
graphics.setFill(color);
graphics.fillOval(cx + offsetX, cy, scaledWidth, height);
}
public Timeline flipDot(Color fromColor, Color toColor, int cell) {
final int steps = 60;
final long duration = 250;
final double interval = duration / (double) steps;
final Timeline timeline = new Timeline();
for (int i = 0; i <= steps; i++) {
final double t = i / (double) steps;
final KeyFrame keyFrame = new KeyFrame(Duration.millis(i * interval),
_ -> {
clear(cell);
final double scale = t <= 0.5 ? 1 - 2 * t : 2 * t - 1;
final Color currentColor = t < 0.5 ? fromColor : toColor;
drawDotScaled(currentColor, cell, scale);
}
);
timeline.getKeyFrames().add(keyFrame);
}
return timeline;
}
public Canvas getCanvas() {
return canvas;
}
}

View File

@@ -0,0 +1,239 @@
package org.toop.app.canvas;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.game.BitboardGame;
import java.util.function.Consumer;
public abstract class BitGameCanvas<T extends BitboardGame<T>> implements GameCanvas<T> {
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 &&
y >= this.y && y <= this.y + height;
}
}
protected final Canvas canvas;
protected final GraphicsContext graphics;
protected final Color color;
protected final Color backgroundColor;
protected final int width;
protected final int height;
protected final int rowSize;
protected final int columnSize;
protected final int gapSize;
protected final boolean edges;
protected final Cell[] cells;
private Consumer<Integer> onCellCLicked;
public void setOnCellClicked(Consumer<Integer> onClick) {
this.onCellCLicked = onClick;
}
protected BitGameCanvas(Color color, Color backgroundColor, int width, int height, int rowSize, int columnSize, int gapSize, boolean edges) {
canvas = new Canvas(width, height);
graphics = canvas.getGraphicsContext2D();
this.onCellCLicked = (c) -> new EventFlow().addPostEvent(GUIEvents.PlayerAttemptedMove.class, c).postEvent();
this.color = color;
this.backgroundColor = backgroundColor;
this.width = width;
this.height = height;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.gapSize = gapSize;
this.edges = edges;
cells = new Cell[rowSize * columnSize];
final float cellWidth = ((float) width - gapSize * rowSize - gapSize) / rowSize;
final float cellHeight = ((float) height - gapSize * columnSize - gapSize) / columnSize;
for (int y = 0; y < columnSize; y++) {
final float startY = y * cellHeight + y * gapSize + gapSize;
for (int x = 0; x < rowSize; x++) {
final float startX = x * cellWidth + x * gapSize + gapSize;
cells[x + y * rowSize] = new Cell(startX, startY, cellWidth, cellHeight);
}
}
canvas.setOnMouseClicked(event -> {
if (event.getButton() != MouseButton.PRIMARY) {
return;
}
final int column = (int) ((event.getX() / this.width) * rowSize);
final int row = (int) ((event.getY() / this.height) * columnSize);
final Cell cell = cells[column + row * rowSize];
if (cell.isInside(event.getX(), event.getY())) {
event.consume();
this.onCellCLicked.accept(column + row * rowSize);
}
});
render();
}
private void render() {
graphics.setFill(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setFill(color);
for (int x = 0; x < rowSize - 1; x++) {
final float start = cells[x].x + cells[x].width;
graphics.fillRect(start, gapSize, gapSize, height - gapSize * 2);
}
for (int y = 0; y < columnSize - 1; y++) {
final float start = cells[y * rowSize].y + cells[y * rowSize].height;
graphics.fillRect(gapSize, start, width - gapSize * 2, gapSize);
}
if (edges) {
graphics.fillRect(0, 0, width, gapSize);
graphics.fillRect(0, 0, gapSize, height);
graphics.fillRect(width - gapSize, 0, gapSize, height);
graphics.fillRect(0, height - gapSize, width, gapSize);
}
}
public void fill(Color color, int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.setFill(color);
graphics.fillRect(x, y, width, height);
}
public void clear(int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.clearRect(x, y, width, height);
graphics.setFill(backgroundColor);
graphics.fillRect(x, y, width, height);
}
public void clearAll() {
for (int i = 0; i < cells.length; i++) {
clear(i);
}
}
public void drawPlayerMove(int player, int move) {
final float x = cells[move].x() + gapSize;
final float y = cells[move].y() + gapSize;
final float width = cells[move].width() - gapSize * 2;
final float height = cells[move].height() - gapSize * 2;
graphics.setFill(color);
graphics.setFont(Font.font("Arial", 40)); // TODO different font and size
graphics.fillText(String.valueOf(player), x + width, y + height);
}
public void drawDot(Color color, int cell) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
final float width = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
graphics.setFill(color);
graphics.fillOval(x, y, width, height);
}
public void drawInnerDot(Color color, int cell, boolean slightlyBigger) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
float multiplier = slightlyBigger?1.4f:1.5f;
final float width = (cells[cell].width() - gapSize * 2)/multiplier;
final float height = (cells[cell].height() - gapSize * 2)/multiplier;
float offset = slightlyBigger?5f:4f;
graphics.setFill(color);
graphics.fillOval(x + width/offset, y + height/offset, width, height);
}
private void drawDotScaled(Color color, int cell, double scale) {
final float cx = cells[cell].x() + gapSize;
final float cy = cells[cell].y() + gapSize;
final float fullWidth = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
final float scaledWidth = (float)(fullWidth * scale);
final float offsetX = (fullWidth - scaledWidth) / 2;
graphics.setFill(color);
graphics.fillOval(cx + offsetX, cy, scaledWidth, height);
}
public Timeline flipDot(Color fromColor, Color toColor, int cell) {
final int steps = 60;
final long duration = 250;
final double interval = duration / (double) steps;
final Timeline timeline = new Timeline();
for (int i = 0; i <= steps; i++) {
final double t = i / (double) steps;
final KeyFrame keyFrame = new KeyFrame(Duration.millis(i * interval),
_ -> {
clear(cell);
final double scale = t <= 0.5 ? 1 - 2 * t : 2 * t - 1;
final Color currentColor = t < 0.5 ? fromColor : toColor;
drawDotScaled(currentColor, cell, scale);
}
);
timeline.getKeyFrames().add(keyFrame);
}
return timeline;
}
public Canvas getCanvas() {
return canvas;
}
}

View File

@@ -1,17 +0,0 @@
package org.toop.app.canvas;
import javafx.scene.paint.Color;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import java.util.function.Consumer;
public class Connect4Canvas extends GameCanvas {
public Connect4Canvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, Color.TRANSPARENT, width, height, 7, 6, 10, true, onCellClicked,null);
}
@Override
public void drawPlayerHover(int player, int move, AbstractGame game) {
}
}

View File

@@ -1,238 +1,8 @@
package org.toop.app.canvas;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
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 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 &&
y >= this.y && y <= this.y + height;
}
}
protected final Canvas canvas;
protected final GraphicsContext graphics;
protected final Color color;
protected final Color backgroundColor;
protected final int width;
protected final int height;
protected final int rowSize;
protected final int columnSize;
protected final int gapSize;
protected final boolean edges;
protected final Cell[] cells;
private Consumer<Integer> onCellCLicked;
public void setOnCellClicked(Consumer<Integer> onClick) {
this.onCellCLicked = onClick;
}
protected GameCanvas(Color color, Color backgroundColor, int width, int height, int rowSize, int columnSize, int gapSize, boolean edges, Consumer<Integer> onCellClicked, Consumer<Integer> newCellEntered) {
canvas = new Canvas(width, height);
graphics = canvas.getGraphicsContext2D();
this.onCellCLicked = onCellClicked;
this.color = color;
this.backgroundColor = backgroundColor;
this.width = width;
this.height = height;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.gapSize = gapSize;
this.edges = edges;
cells = new Cell[rowSize * columnSize];
final float cellWidth = ((float) width - gapSize * rowSize - gapSize) / rowSize;
final float cellHeight = ((float) height - gapSize * columnSize - gapSize) / columnSize;
for (int y = 0; y < columnSize; y++) {
final float startY = y * cellHeight + y * gapSize + gapSize;
for (int x = 0; x < rowSize; x++) {
final float startX = x * cellWidth + x * gapSize + gapSize;
cells[x + y * rowSize] = new Cell(startX, startY, cellWidth, cellHeight);
}
}
canvas.setOnMouseClicked(event -> {
if (event.getButton() != MouseButton.PRIMARY) {
return;
}
final int column = (int) ((event.getX() / this.width) * rowSize);
final int row = (int) ((event.getY() / this.height) * columnSize);
final Cell cell = cells[column + row * rowSize];
if (cell.isInside(event.getX(), event.getY())) {
event.consume();
this.onCellCLicked.accept(column + row * rowSize);
}
});
render();
}
private void render() {
graphics.setFill(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setFill(color);
for (int x = 0; x < rowSize - 1; x++) {
final float start = cells[x].x + cells[x].width;
graphics.fillRect(start, gapSize, gapSize, height - gapSize * 2);
}
for (int y = 0; y < columnSize - 1; y++) {
final float start = cells[y * rowSize].y + cells[y * rowSize].height;
graphics.fillRect(gapSize, start, width - gapSize * 2, gapSize);
}
if (edges) {
graphics.fillRect(0, 0, width, gapSize);
graphics.fillRect(0, 0, gapSize, height);
graphics.fillRect(width - gapSize, 0, gapSize, height);
graphics.fillRect(0, height - gapSize, width, gapSize);
}
}
public void fill(Color color, int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.setFill(color);
graphics.fillRect(x, y, width, height);
}
public void clear(int cell) {
final float x = cells[cell].x();
final float y = cells[cell].y();
final float width = cells[cell].width();
final float height = cells[cell].height();
graphics.clearRect(x, y, width, height);
graphics.setFill(backgroundColor);
graphics.fillRect(x, y, width, height);
}
public void clearAll() {
for (int i = 0; i < cells.length; i++) {
clear(i);
}
}
@Override
public void drawPlayerMove(int player, int move) {
final float x = cells[move].x() + gapSize;
final float y = cells[move].y() + gapSize;
final float width = cells[move].width() - gapSize * 2;
final float height = cells[move].height() - gapSize * 2;
graphics.setFill(color);
graphics.setFont(Font.font("Arial", 40)); // TODO different font and size
graphics.fillText(String.valueOf(player), x + width, y + height);
}
public void drawDot(Color color, int cell) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
final float width = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
graphics.setFill(color);
graphics.fillOval(x, y, width, height);
}
public void drawInnerDot(Color color, int cell, boolean slightlyBigger) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
float multiplier = slightlyBigger?1.4f:1.5f;
final float width = (cells[cell].width() - gapSize * 2)/multiplier;
final float height = (cells[cell].height() - gapSize * 2)/multiplier;
float offset = slightlyBigger?5f:4f;
graphics.setFill(color);
graphics.fillOval(x + width/offset, y + height/offset, width, height);
}
private void drawDotScaled(Color color, int cell, double scale) {
final float cx = cells[cell].x() + gapSize;
final float cy = cells[cell].y() + gapSize;
final float fullWidth = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
final float scaledWidth = (float)(fullWidth * scale);
final float offsetX = (fullWidth - scaledWidth) / 2;
graphics.setFill(color);
graphics.fillOval(cx + offsetX, cy, scaledWidth, height);
}
public Timeline flipDot(Color fromColor, Color toColor, int cell) {
final int steps = 60;
final long duration = 250;
final double interval = duration / (double) steps;
final Timeline timeline = new Timeline();
for (int i = 0; i <= steps; i++) {
final double t = i / (double) steps;
final KeyFrame keyFrame = new KeyFrame(Duration.millis(i * interval),
_ -> {
clear(cell);
final double scale = t <= 0.5 ? 1 - 2 * t : 2 * t - 1;
final Color currentColor = t < 0.5 ? fromColor : toColor;
drawDotScaled(currentColor, cell, scale);
}
);
timeline.getKeyFrames().add(keyFrame);
}
return timeline;
}
public Canvas getCanvas() {
return canvas;
}
}
public interface GameCanvas<T extends TurnBasedGame<T>> extends GameDrawer<T>{
Canvas getCanvas();
}

View File

@@ -0,0 +1,7 @@
package org.toop.app.canvas;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface GameDrawer<T extends TurnBasedGame<T>> {
void redraw(T gameCopy);
}

View File

@@ -0,0 +1,53 @@
package org.toop.app.canvas;
import javafx.scene.paint.Color;
import org.toop.app.App;
import org.toop.game.games.reversi.BitboardReversi;
import java.util.function.Consumer;
public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi>{
public ReversiBitCanvas() {
super(Color.GRAY, new Color(0f,0.4f,0.2f,1f), (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, 8, 8, 5, true);
canvas.setOnMouseMoved(event -> {
double mouseX = event.getX();
double mouseY = event.getY();
int cellId = -1;
BitGameCanvas.Cell hovered = null;
for (BitGameCanvas.Cell cell : cells) {
if (cell.isInside(mouseX, mouseY)) {
hovered = cell;
cellId = turnCoordsIntoCellId(mouseX, mouseY);
break;
}
}
});
}
private int turnCoordsIntoCellId(double x, double y) {
final int column = (int) ((x / this.width) * rowSize);
final int row = (int) ((y / this.height) * columnSize);
return column + row * rowSize;
}
public void drawStartingDots() {
drawDot(Color.BLACK, 28);
drawDot(Color.WHITE, 36);
drawDot(Color.BLACK, 35);
drawDot(Color.WHITE, 27);
}
@Override
public void redraw(BitboardReversi gameCopy) {
clearAll();
drawStartingDots();
for (int i = 0; i < gameCopy.getBoard().length; i++) {
if (gameCopy.getBoard()[i] == 0) {
drawDot(Color.WHITE, i);
} else if (gameCopy.getBoard()[i] == 1) {
drawDot(Color.BLACK, i);
}
}
}
}

View File

@@ -7,8 +7,8 @@ import org.toop.game.games.reversi.ReversiR;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
public final class ReversiCanvas extends GameCanvas<ReversiR> {
@Deprecated
public final class ReversiCanvas extends BaseGameCanvas<ReversiR> {
private Move[] currentlyHighlightedMoves = null;
public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked, Consumer<Integer> newCellEntered) {
super(color, new Color(0f,0.4f,0.2f,1f), width, height, 8, 8, 5, true, onCellClicked, newCellEntered);

View File

@@ -0,0 +1,76 @@
package org.toop.app.canvas;
import javafx.scene.paint.Color;
import org.toop.app.App;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
import java.util.function.Consumer;
public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
public TicTacToeBitCanvas() {
super(
Color.GRAY,
Color.TRANSPARENT,
(App.getHeight() / 4) * 3,
(App.getHeight() / 4) * 3,
3,
3,
30,
false
);
}
@Override
public void redraw(BitboardTicTacToe gameCopy) {
clearAll();
drawMoves(gameCopy.getBoard());
}
private void drawMoves(int[] gameBoard){
// Draw each square
for (int i = 0; i < 9; i++){
// If square isn't empty, draw player move
if (gameBoard[i] != AbstractGame.EMPTY){
drawPlayerMove(gameBoard[i], i);
}
}
}
public void drawPlayerMove(int player, int move) {
switch (player) {
case 0 -> drawX(Color.RED, move);
case 1 -> drawO(Color.BLUE, move);
default -> super.drawPlayerMove(player, move);
}
}
public void drawX(Color color, int cell) {
graphics.setStroke(color);
graphics.setLineWidth(gapSize);
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
final float width = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
graphics.strokeLine(x, y, x + width, y + height);
graphics.strokeLine(x + width, y, x, y + height);
}
public void drawO(Color color, int cell) {
graphics.setStroke(color);
graphics.setLineWidth(gapSize);
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
final float width = cells[cell].width() - gapSize * 2;
final float height = cells[cell].height() - gapSize * 2;
graphics.strokeOval(x, y, width, height);
}
}

View File

@@ -5,8 +5,8 @@ import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
import java.util.function.Consumer;
public final class TicTacToeCanvas extends GameCanvas<BitboardTicTacToe> {
@Deprecated
public final class TicTacToeCanvas extends BaseGameCanvas<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

@@ -5,21 +5,20 @@ 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.app.canvas.BaseGameCanvas;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
import org.toop.app.widget.view.GameView;
import org.toop.framework.eventbus.EventFlow;
import org.toop.game.gameThreads.OnlineThreadBehaviour;
import org.toop.framework.gameFramework.model.game.AbstractGame;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public abstract class AbstractGameController<T extends TurnBasedGame<T>> implements UpdatesGameUI, ThreadBehaviour<T> {
@Deprecated
public abstract class AbstractGameController<T extends TurnBasedGame<T>> implements UpdatesGameUI, ThreadBehaviour {
protected final EventFlow eventFlow = new EventFlow();
protected final List<Consumer<?>> listeners = new ArrayList<>();
@@ -27,19 +26,19 @@ public abstract class AbstractGameController<T extends TurnBasedGame<T>> impleme
// Logger for logging ofcourse
protected final Logger logger = LogManager.getLogger(this.getClass());
// Reference to primary view
// Reference to gameView view
protected final GameView primary;
// Reference to game canvas
protected final GameCanvas<T> canvas;
protected final BaseGameCanvas<T> canvas;
private final Player<T>[] players; // List of players, can't be changed.
protected final TurnBasedGame<T> game; // Reference to game instance
private final ThreadBehaviour<T> gameThreadBehaviour;
private final ThreadBehaviour 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) {
protected AbstractGameController(BaseGameCanvas<T> canvas, Player<T>[] players, T game, ThreadBehaviour gameThreadBehaviour, String gameType) {
logger.info("Creating AbstractGameController");
this.canvas = canvas;

View File

@@ -0,0 +1,134 @@
package org.toop.app.gameControllers;
import javafx.geometry.Pos;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.app.canvas.GameCanvas;
import org.toop.app.widget.WidgetContainer;
import org.toop.app.widget.view.GameView;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.controller.UpdatesGameUI;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.game.gameThreads.OnlineThreadBehaviour;
import org.toop.game.players.LocalPlayer;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class GenericGameController<T extends TurnBasedGame<T>> implements GameController {
protected final EventFlow eventFlow = new EventFlow();
protected final List<Consumer<?>> listeners = new ArrayList<>();
// Logger for logging ofcourse
protected final Logger logger = LogManager.getLogger(this.getClass());
// Reference to gameView view
protected final GameView gameView;
// Reference to game canvas
protected final GameCanvas<T> canvas;
protected final TurnBasedGame<T> game; // Reference to game instance
private final ThreadBehaviour gameThreadBehaviour;
// TODO: Change gameType to automatically happen with either dependency injection or something else.
// TODO: Make visualisation of moves a behaviour.
public GenericGameController(GameCanvas<T> canvas, T game, ThreadBehaviour gameThreadBehaviour, String gameType) {
logger.info("Creating: " + this.getClass());
this.canvas = canvas;
this.game = game;
this.gameThreadBehaviour = gameThreadBehaviour;
gameView = new GameView(null, null, null, gameType);
gameView.add(Pos.CENTER, canvas.getCanvas());
WidgetContainer.getCurrentView().transitionNext(gameView, true);
addListeners();
}
public void start(){
logger.info("Starting GameManager");
updateUI();
gameThreadBehaviour.start();
}
public void stop(){
logger.info("Stopping GameManager");
removeListeners();
gameThreadBehaviour.stop();
}
public Player<T> getCurrentPlayer(){
return game.getPlayer(getCurrentPlayerIndex());
};
public int getCurrentPlayerIndex(){
return game.getCurrentTurn();
}
private void addListeners(){
eventFlow
.listen(GUIEvents.RefreshGameCanvas.class, this::onUpdateGameUI, false)
.listen(GUIEvents.GameEnded.class, this::onGameFinish, false)
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer lp){lp.setMove(event.move());}}, false);
}
private void removeListeners(){
eventFlow.unsubscribeAll();
}
private void onUpdateGameUI(GUIEvents.RefreshGameCanvas event){
this.updateUI();
}
private void onGameFinish(GUIEvents.GameEnded event){
logger.info("Game Finished");
String name = event.winner() == -1 ? null : getPlayer(event.winner()).getName();
gameView.gameOver(event.winOrTie(), name);
stop();
}
public Player<T> getPlayer(int player){
if (player < 0 || player >= 2){ // TODO: Make game turn player count
logger.error("Invalid player index");
throw new IllegalArgumentException("player out of range");
}
return game.getPlayer(player);
}
private boolean isOnline(){
return this.gameThreadBehaviour instanceof SupportsOnlinePlay;
}
public void onYourTurn(NetworkEvents.YourTurnResponse event){
if (isOnline()){
((OnlineThreadBehaviour<T>) this.gameThreadBehaviour).onYourTurn(event);
}
}
public void onMoveReceived(NetworkEvents.GameMoveResponse event){
if (isOnline()){
((OnlineThreadBehaviour<T>) this.gameThreadBehaviour).onMoveReceived(event);
}
}
public void gameFinished(NetworkEvents.GameResultResponse event){
if (isOnline()){
((OnlineThreadBehaviour<T>) this.gameThreadBehaviour).gameFinished(event);
}
}
@Override
public void updateUI() {
canvas.redraw(game.deepCopy());
}
}

View File

@@ -14,7 +14,7 @@ import org.toop.game.gameThreads.OnlineThreadBehaviour;
import org.toop.game.players.LocalPlayer;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.game.games.reversi.ReversiR;
@Deprecated
public class ReversiController extends AbstractGameController<ReversiR> {
// TODO: Refactor GUI update methods to follow designed system
public ReversiController(Player<ReversiR>[] players, boolean local) {

View File

@@ -13,7 +13,7 @@ import org.toop.game.gameThreads.OnlineThreadBehaviour;
import org.toop.game.players.LocalPlayer;
import org.toop.app.widget.WidgetContainer;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
@Deprecated
public class TicTacToeController extends AbstractGameController<BitboardTicTacToe> {
public TicTacToeController(Player<BitboardTicTacToe>[] players, boolean local) {
@@ -22,7 +22,7 @@ public class TicTacToeController extends AbstractGameController<BitboardTicTacTo
new TicTacToeCanvas(Color.GRAY, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {new EventFlow().addPostEvent(GUIEvents.PlayerAttemptedMove.class, c).postEvent();}),
players,
BitboardTicTacToe,
local ? new LocalThreadBehaviour(BitboardTicTacToe, players) : new OnlineThreadBehaviour<>(BitboardTicTacToe, players), // TODO: Player order matters here, this won't work atm
local ? new LocalThreadBehaviour(BitboardTicTacToe) : new OnlineThreadBehaviour<>(BitboardTicTacToe, players), // TODO: Player order matters here, this won't work atm
"TicTacToe");
initUI();

View File

@@ -2,10 +2,14 @@ package org.toop.app.widget.view;
import javafx.application.Platform;
import org.toop.app.GameInformation;
import org.toop.app.gameControllers.AbstractGameController;
import org.toop.app.gameControllers.ReversiController;
import org.toop.app.gameControllers.TicTacToeController;
import org.toop.app.canvas.ReversiBitCanvas;
import org.toop.app.canvas.TicTacToeBitCanvas;
import org.toop.app.gameControllers.GenericGameController;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.game.gameThreads.LocalThreadBehaviour;
import org.toop.game.games.reversi.BitboardReversi;
import org.toop.game.games.tictactoe.BitboardTicTacToe;
import org.toop.game.players.ArtificialPlayer;
import org.toop.game.players.LocalPlayer;
import org.toop.app.widget.Primitive;
@@ -22,10 +26,12 @@ import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import org.toop.local.AppSettings;
import java.util.Arrays;
public class LocalMultiplayerView extends ViewWidget {
private final GameInformation information;
private AbstractGameController<?> gameController;
private GameController gameController;
public LocalMultiplayerView(GameInformation.Type type) {
this(new GameInformation(type));
@@ -49,6 +55,7 @@ public class LocalMultiplayerView extends ViewWidget {
switch (information.type) {
case TICTACTOE:
System.out.println("TicTacToe");
if (information.players[0].isHuman) {
players[0] = new LocalPlayer<>(information.players[0].name);
} else {
@@ -59,20 +66,24 @@ public class LocalMultiplayerView extends ViewWidget {
} else {
players[1] = new ArtificialPlayer<>(new TicTacToeAIR(), information.players[1].name);
}
System.out.println(Arrays.toString(players));
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
new ShowEnableTutorialWidget(
() -> new TicTacToeTutorialWidget(() -> {
gameController = new TicTacToeController(players);
BitboardTicTacToe game = new BitboardTicTacToe(players); // TODO: ThreadBehaviour might need to be created by game idk
gameController = new GenericGameController<BitboardTicTacToe>(new TicTacToeBitCanvas(), game, new LocalThreadBehaviour<BitboardTicTacToe>(game), "TicTacToe");
gameController.start();
}),
() -> Platform.runLater(() -> {
gameController = new TicTacToeController(players);
BitboardTicTacToe game = new BitboardTicTacToe(players); // TODO: ThreadBehaviour might need to be created by game idk
gameController = new GenericGameController<BitboardTicTacToe>(new TicTacToeBitCanvas(), game, new LocalThreadBehaviour<BitboardTicTacToe>(game), "TicTacToe");
gameController.start();
}),
() -> AppSettings.getSettings().setFirstTTT(false)
);
} else {
gameController = new TicTacToeController(players);
BitboardTicTacToe game = new BitboardTicTacToe(players); // TODO: ThreadBehaviour might need to be created by game idk
gameController = new GenericGameController<BitboardTicTacToe>(new TicTacToeBitCanvas(), game, new LocalThreadBehaviour<BitboardTicTacToe>(game), "TicTacToe");
gameController.start();
}
break;
@@ -90,17 +101,20 @@ public class LocalMultiplayerView extends ViewWidget {
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
new ShowEnableTutorialWidget(
() -> new ReversiTutorialWidget(() -> {
gameController = new ReversiController(players);
BitboardReversi game = new BitboardReversi(players);
gameController = new GenericGameController<>(new ReversiBitCanvas(), game, new LocalThreadBehaviour<>(game), "Reversi");
gameController.start();
}),
() -> Platform.runLater(() -> {
gameController = new ReversiController(players);
BitboardReversi game = new BitboardReversi(players);
gameController = new GenericGameController<>(new ReversiBitCanvas(), game, new LocalThreadBehaviour<>(game), "Reversi");
gameController.start();
}),
() -> AppSettings.getSettings().setFirstReversi(false)
);
} else {
gameController = new ReversiController(players);
BitboardReversi game = new BitboardReversi(players);
gameController = new GenericGameController<>(new ReversiBitCanvas(), game, new LocalThreadBehaviour<>(game), "Reversi");
gameController.start();
}
break;

View File

@@ -0,0 +1,7 @@
package org.toop.framework.gameFramework.controller;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
public interface GameController extends Controllable, SupportsOnlinePlay, UpdatesGameUI {
}

View File

@@ -14,7 +14,7 @@ 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 {
/** Indicates whether the game loop or event processing is active. */
protected final AtomicBoolean isRunning = new AtomicBoolean();

View File

@@ -9,5 +9,5 @@ import org.toop.framework.gameFramework.model.player.Player;
* <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 {
}

View File

@@ -12,6 +12,7 @@ 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> {
@Deprecated
public abstract class AbstractAI<T extends TurnBasedGame<T>> implements MoveProvider<T> {
// Concrete AI implementations should override findBestMove(T game, int depth)
}

View File

@@ -1,12 +1,9 @@
package org.toop.game;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBasedGame<T> {
private final int columnSize;
@@ -21,9 +18,7 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
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;
@@ -60,12 +55,13 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
return output;
}
public BitboardGame(BitboardGame other) {
public BitboardGame(BitboardGame<T> other) {
this.columnSize = other.columnSize;
this.rowSize = other.rowSize;
this.playerBitboard = Arrays.copyOf(other.playerBitboard, other.playerBitboard.length);
this.currentTurn = other.currentTurn;
this.players = Arrays.copyOf(other.players, other.players.length); // TODO: Make this a deep copy
}
public int getColumnSize() {

View File

@@ -20,9 +20,8 @@ public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractTh
* Creates a new behaviour for a local turn-based game.
*
* @param game the game instance
* @param players the list of players in turn order
*/
public LocalThreadBehaviour(T game, Player<T>[] players) {
public LocalThreadBehaviour(T game) {
super(game);
}

View File

@@ -5,6 +5,8 @@ import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.game.BitboardGame;
import java.util.Arrays;
public class BitboardReversi extends BitboardGame<BitboardReversi> {
@Override
@@ -62,6 +64,7 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
@Override
public int[] getLegalMoves(){
System.out.println(Arrays.toString(translateLegalMoves(getLegalMoves2())));
return translateLegalMoves(getLegalMoves2());
}