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

@@ -2,23 +2,28 @@ 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 implements TurnBasedGame<BitboardGame> {
public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBasedGame<T> {
private final int columnSize;
private final int rowSize;
private Player<T>[] players;
// long is 64 bits. Every game has a limit of 64 cells maximum.
private final long[] playerBitboard;
private int currentTurn;
public BitboardGame(int columnSize, int rowSize, int playerCount) {
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;
@@ -34,6 +39,7 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
j++;
}
}
System.out.println(Arrays.toString(output));
return output;
}
@@ -43,6 +49,7 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
protected int[] translateBoard(){
int[] output = new int[64];
Arrays.fill(output, -1);
for(int i = 0; i < this.playerBitboard.length; i++){
for (int j = 0; j < 64; j++){
if ((this.playerBitboard[i] & (1L << j)) != 0){
@@ -82,10 +89,12 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
}
public int getCurrentTurn() {
return currentTurn;
return getCurrentPlayerIndex();
}
public int getCurrentPlayer() {
public Player<T> getPlayer(int index) {return players[index];}
public int getCurrentPlayerIndex() {
return currentTurn % playerBitboard.length;
}
@@ -93,6 +102,10 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
return (currentTurn + 1) % playerBitboard.length;
}
public Player<T> getCurrentPlayer(){
return players[getCurrentPlayerIndex()];
}
public void nextTurn() {
currentTurn++;
}

View File

@@ -1,14 +1,15 @@
package org.toop.game.reversi;
package org.toop.game.games.reversi;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.game.BitboardGame;
public class BitboardReversi extends BitboardGame {
public class BitboardReversi extends BitboardGame<BitboardReversi> {
@Override
public Player<BitboardGame> getPlayer(int index) {
return null;
public int[] getBoard() {
return translateBoard();
}
public record Score(int black, int white) {}
@@ -16,8 +17,8 @@ public class BitboardReversi extends BitboardGame {
private final long notAFile = 0xfefefefefefefefeL;
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
public BitboardReversi() {
super(8, 8, 2);
public BitboardReversi(Player<BitboardReversi>[] players) {
super(8, 8, 2, players);
// Black (player 0)
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
@@ -26,7 +27,7 @@ public class BitboardReversi extends BitboardGame {
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8))); }
public long getLegalMoves2() {
final long player = getPlayerBitboard(getCurrentPlayer());
final long player = getPlayerBitboard(getCurrentPlayerIndex());
final long opponent = getPlayerBitboard(getNextPlayer());
long legalMoves = 0L;
@@ -43,7 +44,7 @@ public class BitboardReversi extends BitboardGame {
}
public long getFlips(long move) {
final long player = getPlayerBitboard(getCurrentPlayer());
final long player = getPlayerBitboard(getCurrentPlayerIndex());
final long opponent = getPlayerBitboard(getNextPlayer());
long flips = 0L;
@@ -66,7 +67,7 @@ public class BitboardReversi extends BitboardGame {
@Override
public PlayResult play(int move) {
return new PlayResult(playBit(translateMove(move)), getCurrentPlayer());
return new PlayResult(playBit(translateMove(move)), getCurrentPlayerIndex());
}
// TODO: Implement
@@ -76,13 +77,13 @@ public class BitboardReversi extends BitboardGame {
public GameState playBit(long move) {
final long flips = getFlips(move);
long player = getPlayerBitboard(getCurrentPlayer());
long player = getPlayerBitboard(getCurrentPlayerIndex());
long opponent = getPlayerBitboard(getNextPlayer());
player |= move | flips;
opponent &= ~flips;
setPlayerBitboard(getCurrentPlayer(), player);
setPlayerBitboard(getCurrentPlayerIndex(), player);
setPlayerBitboard(getNextPlayer(), opponent);
nextTurn();

View File

@@ -1,9 +1,11 @@
package org.toop.game.tictactoe;
package org.toop.game.games.tictactoe;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.game.BitboardGame;
public class BitboardTicTacToe extends BitboardGame {
public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
private final long[] winningLines = {
0b111000000L, // top row
0b000111000L, // middle row
@@ -15,8 +17,8 @@ public class BitboardTicTacToe extends BitboardGame {
0b001010100L // anti-diagonal
};
public BitboardTicTacToe() {
super(3, 3, 2);
public BitboardTicTacToe(Player<BitboardTicTacToe>[] players) {
super(3, 3, 2, players);
}
@Override
@@ -24,7 +26,12 @@ public class BitboardTicTacToe extends BitboardGame {
return translateLegalMoves(getLegalMoves2());
}
public long getLegalMoves2() {
@Override
public PlayResult play(int move) {
return new PlayResult(play2(translateMove(move)), getCurrentPlayerIndex());
}
public long getLegalMoves2() {
final long xBitboard = getPlayerBitboard(0);
final long oBitboard = getPlayerBitboard(1);
@@ -32,22 +39,22 @@ public class BitboardTicTacToe extends BitboardGame {
return (~taken) & 0x1ffL;
}
@Override
public GameState play(long move) {
long playerBitboard = getPlayerBitboard(getCurrentPlayer());
public GameState play2(long move) {
long playerBitboard = getPlayerBitboard(getCurrentPlayerIndex());
playerBitboard |= move;
setPlayerBitboard(getCurrentPlayer(), playerBitboard);
setPlayerBitboard(getCurrentPlayerIndex(), playerBitboard);
nextTurn();
if (checkWin(playerBitboard)) {
return GameState.WIN;
}
if (getLegalMoves() <= 0L || checkEarlyDraw()) {
if (getLegalMoves2() <= 0L || checkEarlyDraw()) {
return GameState.DRAW;
}
nextTurn();
return GameState.NORMAL;
}
@@ -81,4 +88,15 @@ public class BitboardTicTacToe extends BitboardGame {
return true;
}
@Override
public int[] getBoard() {
return translateBoard();
}
// TODO: Implement
@Override
public BitboardTicTacToe deepCopy() {
return this;
}
}