mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Bitboard implemented with scuffed TicTacToe translation done by game. This should be done by the view.
This commit is contained in:
@@ -9,10 +9,11 @@ import javafx.scene.paint.Color;
|
|||||||
import javafx.scene.text.Font;
|
import javafx.scene.text.Font;
|
||||||
import javafx.util.Duration;
|
import javafx.util.Duration;
|
||||||
import org.toop.framework.gameFramework.model.game.AbstractGame;
|
import org.toop.framework.gameFramework.model.game.AbstractGame;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
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) {
|
protected record Cell(float x, float y, float width, float height) {
|
||||||
public boolean isInside(double x, double y) {
|
public boolean isInside(double x, double y) {
|
||||||
return x >= this.x && x <= this.x + width &&
|
return x >= this.x && x <= this.x + width &&
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package org.toop.app.canvas;
|
|||||||
|
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import org.toop.framework.gameFramework.model.game.AbstractGame;
|
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;
|
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) {
|
public TicTacToeCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
|
||||||
super(color, Color.TRANSPARENT, width, height, 3, 3, 30, false, onCellClicked,null);
|
super(color, Color.TRANSPARENT, width, height, 3, 3, 30, false, onCellClicked,null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.toop.app.gameControllers;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.gameFramework.controller.UpdatesGameUI;
|
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.framework.gameFramework.view.GUIEvents;
|
||||||
import org.toop.app.canvas.GameCanvas;
|
import org.toop.app.canvas.GameCanvas;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
@@ -18,7 +19,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
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 EventFlow eventFlow = new EventFlow();
|
||||||
|
|
||||||
protected final List<Consumer<?>> listeners = new ArrayList<>();
|
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;
|
protected final GameCanvas<T> canvas;
|
||||||
|
|
||||||
private final Player<T>[] players; // List of players, can't be changed.
|
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;
|
private final ThreadBehaviour<T> gameThreadBehaviour;
|
||||||
|
|
||||||
// TODO: Change gameType to automatically happen with either dependency injection or something else.
|
// TODO: Change gameType to automatically happen with either dependency injection or something else.
|
||||||
// TODO: Make visualisation of moves a behaviour.
|
// TODO: Make visualisation of moves a behaviour.
|
||||||
protected AbstractGameController(GameCanvas<T> canvas, Player<T>[] players, T game, ThreadBehaviour<T> gameThreadBehaviour, String gameType) {
|
protected AbstractGameController(GameCanvas<T> canvas, Player<T>[] players, T game, ThreadBehaviour<T> gameThreadBehaviour, String gameType) {
|
||||||
logger.info("Creating AbstractGameController");
|
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.canvas = canvas;
|
||||||
this.players = players;
|
this.players = players;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class ReversiController extends AbstractGameController<ReversiR> {
|
|||||||
|
|
||||||
private void onHoverMove(GUIEvents.PlayerMoveHovered event){
|
private void onHoverMove(GUIEvents.PlayerMoveHovered event){
|
||||||
int cellEntered = event.move();
|
int cellEntered = event.move();
|
||||||
canvas.drawPlayerHover(-1, cellEntered, game);
|
//canvas.drawPlayerHover(-1, cellEntered, game);
|
||||||
/*// (information.players[game.getCurrentTurn()].isHuman) {
|
/*// (information.players[game.getCurrentTurn()].isHuman) {
|
||||||
int[] legalMoves = game.getLegalMoves();
|
int[] legalMoves = game.getLegalMoves();
|
||||||
boolean isLegalMove = false;
|
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 SequentialTransition animation = new SequentialTransition();
|
||||||
|
|
||||||
final Color fromColor = getCurrentPlayerIndex() == 0? Color.WHITE : Color.BLACK;
|
final Color fromColor = getCurrentPlayerIndex() == 0? Color.WHITE : Color.BLACK;
|
||||||
final Color toColor = getCurrentPlayerIndex() == 0? Color.BLACK : Color.WHITE;
|
final Color toColor = getCurrentPlayerIndex() == 0? Color.BLACK : Color.WHITE;
|
||||||
|
|
||||||
if (animate && flipped != null) {
|
/*if (animate && flipped != null) {
|
||||||
for (final int flip : flipped) {
|
for (final int flip : flipped) {
|
||||||
canvas.clear(flip);
|
canvas.clear(flip);
|
||||||
canvas.drawDot(fromColor, flip);
|
canvas.drawDot(fromColor, flip);
|
||||||
animation.getChildren().addFirst(canvas.flipDot(fromColor, toColor, flip));
|
animation.getChildren().addFirst(canvas.flipDot(fromColor, toColor, flip));
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
animation.setOnFinished(_ -> {
|
animation.setOnFinished(_ -> {
|
||||||
|
|
||||||
|
|||||||
@@ -12,17 +12,17 @@ import org.toop.game.gameThreads.LocalThreadBehaviour;
|
|||||||
import org.toop.game.gameThreads.OnlineThreadBehaviour;
|
import org.toop.game.gameThreads.OnlineThreadBehaviour;
|
||||||
import org.toop.game.players.LocalPlayer;
|
import org.toop.game.players.LocalPlayer;
|
||||||
import org.toop.app.widget.WidgetContainer;
|
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) {
|
public TicTacToeController(Player<BitboardTicTacToe>[] players, boolean local) {
|
||||||
TicTacToeR ticTacToeR = new TicTacToeR(players);
|
BitboardTicTacToe BitboardTicTacToe = new BitboardTicTacToe(players);
|
||||||
super(
|
super(
|
||||||
new TicTacToeCanvas(Color.GRAY, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {new EventFlow().addPostEvent(GUIEvents.PlayerAttemptedMove.class, c).postEvent();}),
|
new TicTacToeCanvas(Color.GRAY, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {new EventFlow().addPostEvent(GUIEvents.PlayerAttemptedMove.class, c).postEvent();}),
|
||||||
players,
|
players,
|
||||||
ticTacToeR,
|
BitboardTicTacToe,
|
||||||
local ? new LocalThreadBehaviour(ticTacToeR, players) : new OnlineThreadBehaviour<>(ticTacToeR, players), // TODO: Player order matters here, this won't work atm
|
local ? new LocalThreadBehaviour(BitboardTicTacToe, players) : new OnlineThreadBehaviour<>(BitboardTicTacToe, players), // TODO: Player order matters here, this won't work atm
|
||||||
"TicTacToe");
|
"TicTacToe");
|
||||||
|
|
||||||
initUI();
|
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());}});
|
//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);
|
this(players, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ public class TicTacToeController extends AbstractGameController<TicTacToeR> {
|
|||||||
int[] board = game.getBoard();
|
int[] board = game.getBoard();
|
||||||
|
|
||||||
// Draw each square
|
// 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 square isn't empty, draw player move
|
||||||
if (board[i] != AbstractGame.EMPTY){
|
if (board[i] != AbstractGame.EMPTY){
|
||||||
canvas.drawPlayerMove(board[i], i);
|
canvas.drawPlayerMove(board[i], i);
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.toop.framework.gameFramework.model.game;
|
||||||
|
|
||||||
|
public interface BoardProvider {
|
||||||
|
int[] getBoard();
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package org.toop.framework.gameFramework.model.game;
|
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 getCurrentTurn();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,28 @@ package org.toop.game;
|
|||||||
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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 columnSize;
|
||||||
private final int rowSize;
|
private final int rowSize;
|
||||||
|
|
||||||
|
private Player<T>[] players;
|
||||||
|
|
||||||
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
||||||
private final long[] playerBitboard;
|
private final long[] playerBitboard;
|
||||||
private int currentTurn;
|
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.columnSize = columnSize;
|
||||||
this.rowSize = rowSize;
|
this.rowSize = rowSize;
|
||||||
|
|
||||||
|
this.players = players;
|
||||||
|
|
||||||
this.playerBitboard = new long[playerCount];
|
this.playerBitboard = new long[playerCount];
|
||||||
this.currentTurn = 0;
|
this.currentTurn = 0;
|
||||||
|
|
||||||
@@ -34,6 +39,7 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
|
|||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println(Arrays.toString(output));
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +49,7 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
|
|||||||
|
|
||||||
protected int[] translateBoard(){
|
protected int[] translateBoard(){
|
||||||
int[] output = new int[64];
|
int[] output = new int[64];
|
||||||
|
Arrays.fill(output, -1);
|
||||||
for(int i = 0; i < this.playerBitboard.length; i++){
|
for(int i = 0; i < this.playerBitboard.length; i++){
|
||||||
for (int j = 0; j < 64; j++){
|
for (int j = 0; j < 64; j++){
|
||||||
if ((this.playerBitboard[i] & (1L << j)) != 0){
|
if ((this.playerBitboard[i] & (1L << j)) != 0){
|
||||||
@@ -82,10 +89,12 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getCurrentTurn() {
|
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;
|
return currentTurn % playerBitboard.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +102,10 @@ public abstract class BitboardGame implements TurnBasedGame<BitboardGame> {
|
|||||||
return (currentTurn + 1) % playerBitboard.length;
|
return (currentTurn + 1) % playerBitboard.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Player<T> getCurrentPlayer(){
|
||||||
|
return players[getCurrentPlayerIndex()];
|
||||||
|
}
|
||||||
|
|
||||||
public void nextTurn() {
|
public void nextTurn() {
|
||||||
currentTurn++;
|
currentTurn++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.BitboardGame;
|
import org.toop.game.BitboardGame;
|
||||||
|
|
||||||
public class BitboardReversi extends BitboardGame {
|
public class BitboardReversi extends BitboardGame<BitboardReversi> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Player<BitboardGame> getPlayer(int index) {
|
public int[] getBoard() {
|
||||||
return null;
|
return translateBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Score(int black, int white) {}
|
public record Score(int black, int white) {}
|
||||||
@@ -16,8 +17,8 @@ public class BitboardReversi extends BitboardGame {
|
|||||||
private final long notAFile = 0xfefefefefefefefeL;
|
private final long notAFile = 0xfefefefefefefefeL;
|
||||||
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
||||||
|
|
||||||
public BitboardReversi() {
|
public BitboardReversi(Player<BitboardReversi>[] players) {
|
||||||
super(8, 8, 2);
|
super(8, 8, 2, players);
|
||||||
|
|
||||||
// Black (player 0)
|
// Black (player 0)
|
||||||
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
|
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))); }
|
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8))); }
|
||||||
|
|
||||||
public long getLegalMoves2() {
|
public long getLegalMoves2() {
|
||||||
final long player = getPlayerBitboard(getCurrentPlayer());
|
final long player = getPlayerBitboard(getCurrentPlayerIndex());
|
||||||
final long opponent = getPlayerBitboard(getNextPlayer());
|
final long opponent = getPlayerBitboard(getNextPlayer());
|
||||||
|
|
||||||
long legalMoves = 0L;
|
long legalMoves = 0L;
|
||||||
@@ -43,7 +44,7 @@ public class BitboardReversi extends BitboardGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public long getFlips(long move) {
|
public long getFlips(long move) {
|
||||||
final long player = getPlayerBitboard(getCurrentPlayer());
|
final long player = getPlayerBitboard(getCurrentPlayerIndex());
|
||||||
final long opponent = getPlayerBitboard(getNextPlayer());
|
final long opponent = getPlayerBitboard(getNextPlayer());
|
||||||
|
|
||||||
long flips = 0L;
|
long flips = 0L;
|
||||||
@@ -66,7 +67,7 @@ public class BitboardReversi extends BitboardGame {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PlayResult play(int move) {
|
public PlayResult play(int move) {
|
||||||
return new PlayResult(playBit(translateMove(move)), getCurrentPlayer());
|
return new PlayResult(playBit(translateMove(move)), getCurrentPlayerIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
@@ -76,13 +77,13 @@ public class BitboardReversi extends BitboardGame {
|
|||||||
public GameState playBit(long move) {
|
public GameState playBit(long move) {
|
||||||
final long flips = getFlips(move);
|
final long flips = getFlips(move);
|
||||||
|
|
||||||
long player = getPlayerBitboard(getCurrentPlayer());
|
long player = getPlayerBitboard(getCurrentPlayerIndex());
|
||||||
long opponent = getPlayerBitboard(getNextPlayer());
|
long opponent = getPlayerBitboard(getNextPlayer());
|
||||||
|
|
||||||
player |= move | flips;
|
player |= move | flips;
|
||||||
opponent &= ~flips;
|
opponent &= ~flips;
|
||||||
|
|
||||||
setPlayerBitboard(getCurrentPlayer(), player);
|
setPlayerBitboard(getCurrentPlayerIndex(), player);
|
||||||
setPlayerBitboard(getNextPlayer(), opponent);
|
setPlayerBitboard(getNextPlayer(), opponent);
|
||||||
|
|
||||||
nextTurn();
|
nextTurn();
|
||||||
|
|||||||
@@ -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.GameState;
|
||||||
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.BitboardGame;
|
import org.toop.game.BitboardGame;
|
||||||
|
|
||||||
public class BitboardTicTacToe extends BitboardGame {
|
public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
|
||||||
private final long[] winningLines = {
|
private final long[] winningLines = {
|
||||||
0b111000000L, // top row
|
0b111000000L, // top row
|
||||||
0b000111000L, // middle row
|
0b000111000L, // middle row
|
||||||
@@ -15,8 +17,8 @@ public class BitboardTicTacToe extends BitboardGame {
|
|||||||
0b001010100L // anti-diagonal
|
0b001010100L // anti-diagonal
|
||||||
};
|
};
|
||||||
|
|
||||||
public BitboardTicTacToe() {
|
public BitboardTicTacToe(Player<BitboardTicTacToe>[] players) {
|
||||||
super(3, 3, 2);
|
super(3, 3, 2, players);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -24,7 +26,12 @@ public class BitboardTicTacToe extends BitboardGame {
|
|||||||
return translateLegalMoves(getLegalMoves2());
|
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 xBitboard = getPlayerBitboard(0);
|
||||||
final long oBitboard = getPlayerBitboard(1);
|
final long oBitboard = getPlayerBitboard(1);
|
||||||
|
|
||||||
@@ -32,22 +39,22 @@ public class BitboardTicTacToe extends BitboardGame {
|
|||||||
return (~taken) & 0x1ffL;
|
return (~taken) & 0x1ffL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public GameState play2(long move) {
|
||||||
public GameState play(long move) {
|
long playerBitboard = getPlayerBitboard(getCurrentPlayerIndex());
|
||||||
long playerBitboard = getPlayerBitboard(getCurrentPlayer());
|
|
||||||
playerBitboard |= move;
|
playerBitboard |= move;
|
||||||
|
|
||||||
setPlayerBitboard(getCurrentPlayer(), playerBitboard);
|
setPlayerBitboard(getCurrentPlayerIndex(), playerBitboard);
|
||||||
|
nextTurn();
|
||||||
|
|
||||||
if (checkWin(playerBitboard)) {
|
if (checkWin(playerBitboard)) {
|
||||||
return GameState.WIN;
|
return GameState.WIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getLegalMoves() <= 0L || checkEarlyDraw()) {
|
if (getLegalMoves2() <= 0L || checkEarlyDraw()) {
|
||||||
return GameState.DRAW;
|
return GameState.DRAW;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextTurn();
|
|
||||||
|
|
||||||
return GameState.NORMAL;
|
return GameState.NORMAL;
|
||||||
}
|
}
|
||||||
@@ -81,4 +88,15 @@ public class BitboardTicTacToe extends BitboardGame {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] getBoard() {
|
||||||
|
return translateBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement
|
||||||
|
@Override
|
||||||
|
public BitboardTicTacToe deepCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user