mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Server update with new dev changes (#305)
* merge widgets with development * readd previous game thread code * Revert "readd previous game thread code" This reverts commitd24feef73e. * Revert "Merge remote-tracking branch 'origin/Development' into Development" This reverts commit59d46cb73c, reversing changes made to38681c5db0. * Revert "merge widgets with development" This reverts commit38681c5db0. * Merge 292 into development (#293) Applied template method pattern to abstract player * Added documentation to player classes and improved method names (#295) * mcts v1 * bitboard optimization * bitboard fix & mcts v2 & mcts v3. v3 still in progress and v4 coming soon * main --------- Co-authored-by: ramollia <> Co-authored-by: Stef <stbuwalda@gmail.com> Co-authored-by: Stef <48526421+StefBuwalda@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
230f7480e4
commit
c64a2e2c65
@@ -1,5 +1,7 @@
|
||||
package org.toop.framework.game;
|
||||
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
@@ -10,6 +12,8 @@ public abstract class BitboardGame implements TurnBasedGame {
|
||||
private final int columnSize;
|
||||
private final int rowSize;
|
||||
|
||||
protected PlayResult state;
|
||||
|
||||
private Player[] players;
|
||||
|
||||
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
||||
@@ -20,21 +24,27 @@ public abstract class BitboardGame implements TurnBasedGame {
|
||||
public BitboardGame(int columnSize, int rowSize, int playerCount) {
|
||||
this.columnSize = columnSize;
|
||||
this.rowSize = rowSize;
|
||||
this.playerCount = playerCount;
|
||||
this.playerBitboard = new long[playerCount];
|
||||
}
|
||||
this.playerCount = playerCount;
|
||||
this.state = new PlayResult(GameState.NORMAL, -1);
|
||||
|
||||
@Override
|
||||
public void init(Player[] players) {
|
||||
this.players = players;
|
||||
this.playerBitboard = new long[playerCount];
|
||||
|
||||
Arrays.fill(playerBitboard, 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Player[] players) {
|
||||
this.players = players;
|
||||
|
||||
Arrays.fill(playerBitboard, 0L);
|
||||
}
|
||||
|
||||
public BitboardGame(BitboardGame other) {
|
||||
this.columnSize = other.columnSize;
|
||||
this.rowSize = other.rowSize;
|
||||
this.playerCount = other.playerCount;
|
||||
this.state = other.state;
|
||||
|
||||
this.playerBitboard = other.playerBitboard.clone();
|
||||
this.currentTurn = other.currentTurn;
|
||||
this.players = Arrays.stream(other.players)
|
||||
@@ -80,9 +90,17 @@ public abstract class BitboardGame implements TurnBasedGame {
|
||||
return players[getCurrentPlayerIndex()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayResult getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTerminal() {
|
||||
return state.state() == GameState.WIN || state.state() == GameState.DRAW;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public long[] getBoard() {return this.playerBitboard;}
|
||||
|
||||
public void nextTurn() {
|
||||
|
||||
@@ -32,47 +32,227 @@ public class BitboardReversi extends BitboardGame {
|
||||
}
|
||||
|
||||
public long getLegalMoves() {
|
||||
long legalMoves = 0L;
|
||||
|
||||
final long player = getPlayerBitboard(getCurrentPlayerIndex());
|
||||
final long opponent = getPlayerBitboard(getNextPlayer());
|
||||
|
||||
long legalMoves = 0L;
|
||||
final long empty = ~(player | opponent);
|
||||
|
||||
// north & south
|
||||
legalMoves |= computeMoves(player, opponent, 8, -1L);
|
||||
legalMoves |= computeMoves(player, opponent, -8, -1L);
|
||||
long mask;
|
||||
long direction;
|
||||
|
||||
// east & west
|
||||
legalMoves |= computeMoves(player, opponent, 1, notAFile);
|
||||
legalMoves |= computeMoves(player, opponent, -1, notHFile);
|
||||
// north
|
||||
mask = opponent;
|
||||
direction = (player << 8) & mask;
|
||||
|
||||
// north-east & north-west & south-east & south-west
|
||||
legalMoves |= computeMoves(player, opponent, 9, notAFile);
|
||||
legalMoves |= computeMoves(player, opponent, 7, notHFile);
|
||||
legalMoves |= computeMoves(player, opponent, -7, notAFile);
|
||||
legalMoves |= computeMoves(player, opponent, -9, notHFile);
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
legalMoves |= (direction << 8) & empty;
|
||||
|
||||
// south
|
||||
mask = opponent;
|
||||
direction = (player >>> 8) & mask;
|
||||
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
legalMoves |= (direction >>> 8) & empty;
|
||||
|
||||
// east
|
||||
mask = opponent & notAFile;
|
||||
direction = (player << 1) & mask;
|
||||
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
legalMoves |= (direction << 1) & empty & notAFile;
|
||||
|
||||
// west
|
||||
mask = opponent & notHFile;
|
||||
direction = (player >>> 1) & mask;
|
||||
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
legalMoves |= (direction >>> 1) & empty & notHFile;
|
||||
|
||||
// north-east
|
||||
mask = opponent & notAFile;
|
||||
direction = (player << 9) & mask;
|
||||
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
legalMoves |= (direction << 9) & empty & notAFile;
|
||||
|
||||
// north-west
|
||||
mask = opponent & notHFile;
|
||||
direction = (player << 7) & mask;
|
||||
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
legalMoves |= (direction << 7) & empty & notHFile;
|
||||
|
||||
// south-east
|
||||
mask = opponent & notAFile;
|
||||
direction = (player >>> 7) & mask;
|
||||
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
legalMoves |= (direction >>> 7) & empty & notAFile;
|
||||
|
||||
// south-west
|
||||
mask = opponent & notHFile;
|
||||
direction = (player >>> 9) & mask;
|
||||
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
legalMoves |= (direction >>> 9) & empty & notHFile;
|
||||
|
||||
return legalMoves;
|
||||
}
|
||||
|
||||
public long getFlips(long move) {
|
||||
long flips = 0L;
|
||||
|
||||
final long player = getPlayerBitboard(getCurrentPlayerIndex());
|
||||
final long opponent = getPlayerBitboard(getNextPlayer());
|
||||
|
||||
long flips = 0L;
|
||||
long mask;
|
||||
long direction;
|
||||
|
||||
// north & south
|
||||
flips |= computeFlips(move, player, opponent, 8, -1L);
|
||||
flips |= computeFlips(move, player, opponent, -8, -1L);
|
||||
// north
|
||||
mask = opponent;
|
||||
direction = (move << 8) & mask;
|
||||
|
||||
// east & west
|
||||
flips |= computeFlips(move, player, opponent, 1, notAFile);
|
||||
flips |= computeFlips(move, player, opponent, -1, notHFile);
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
direction |= (direction << 8) & mask;
|
||||
|
||||
// north-east & north-west & south-east & south-west
|
||||
flips |= computeFlips(move, player, opponent, 9, notAFile);
|
||||
flips |= computeFlips(move, player, opponent, 7, notHFile);
|
||||
flips |= computeFlips(move, player, opponent, -7, notAFile);
|
||||
flips |= computeFlips(move, player, opponent, -9, notHFile);
|
||||
if (((direction << 8) & player) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// south
|
||||
mask = opponent;
|
||||
direction = (move >>> 8) & mask;
|
||||
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
direction |= (direction >>> 8) & mask;
|
||||
|
||||
if (((direction >>> 8) & player) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// east
|
||||
mask = opponent & notAFile;
|
||||
direction = (move << 1) & mask;
|
||||
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
direction |= (direction << 1) & mask;
|
||||
|
||||
if (((direction << 1) & player & notAFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// west
|
||||
mask = opponent & notHFile;
|
||||
direction = (move >>> 1) & mask;
|
||||
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
direction |= (direction >>> 1) & mask;
|
||||
|
||||
if (((direction >>> 1) & player & notHFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// north-east
|
||||
mask = opponent & notAFile;
|
||||
direction = (move << 9) & mask;
|
||||
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
direction |= (direction << 9) & mask;
|
||||
|
||||
if (((direction << 9) & player & notAFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// north-west
|
||||
mask = opponent & notHFile;
|
||||
direction = (move << 7) & mask;
|
||||
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
direction |= (direction << 7) & mask;
|
||||
|
||||
if (((direction << 7) & player & notHFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// south-east
|
||||
mask = opponent & notAFile;
|
||||
direction = (move >>> 7) & mask;
|
||||
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
direction |= (direction >>> 7) & mask;
|
||||
|
||||
if (((direction >>> 7) & player & notAFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
// south-west
|
||||
mask = opponent & notHFile;
|
||||
direction = (move >>> 9) & mask;
|
||||
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
direction |= (direction >>> 9) & mask;
|
||||
|
||||
if (((direction >>> 9) & player & notHFile) != 0) {
|
||||
flips |= direction;
|
||||
}
|
||||
|
||||
return flips;
|
||||
}
|
||||
@@ -105,16 +285,20 @@ public class BitboardReversi extends BitboardGame {
|
||||
int winner = getWinner();
|
||||
|
||||
if (winner == -1) {
|
||||
return new PlayResult(GameState.DRAW, -1);
|
||||
state = new PlayResult(GameState.DRAW, -1);
|
||||
return state;
|
||||
}
|
||||
|
||||
return new PlayResult(GameState.WIN, winner);
|
||||
state = new PlayResult(GameState.WIN, winner);
|
||||
return state;
|
||||
}
|
||||
|
||||
return new PlayResult(GameState.TURN_SKIPPED, getCurrentPlayerIndex());
|
||||
state = new PlayResult(GameState.TURN_SKIPPED, getCurrentPlayerIndex());
|
||||
return state;
|
||||
}
|
||||
|
||||
return new PlayResult(GameState.NORMAL, getCurrentPlayerIndex());
|
||||
state = new PlayResult(GameState.NORMAL, getCurrentPlayerIndex());
|
||||
return state;
|
||||
}
|
||||
|
||||
public Score getScore() {
|
||||
@@ -141,35 +325,4 @@ public class BitboardReversi extends BitboardGame {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
private long computeMoves(long player, long opponent, int shift, long mask) {
|
||||
long moves = shift(player, shift, mask) & opponent;
|
||||
long captured = moves;
|
||||
|
||||
while (moves != 0) {
|
||||
moves = shift(moves, shift, mask) & opponent;
|
||||
captured |= moves;
|
||||
}
|
||||
|
||||
long landing = shift(captured, shift, mask);
|
||||
return landing & ~(player | opponent);
|
||||
}
|
||||
|
||||
private long computeFlips(long move, long player, long opponent, int shift, long mask) {
|
||||
long flips = 0L;
|
||||
long pos = move;
|
||||
|
||||
while (true) {
|
||||
pos = shift(pos, shift, mask);
|
||||
if (pos == 0) return 0L;
|
||||
|
||||
if ((pos & opponent) != 0) flips |= pos;
|
||||
else if ((pos & player) != 0) return flips;
|
||||
else return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
private long shift(long bit, int shift, long mask) {
|
||||
return shift > 0 ? (bit << shift) & mask : (bit >>> -shift) & mask;
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,8 @@ public class BitboardTicTacToe extends BitboardGame {
|
||||
public PlayResult play(long move) {
|
||||
// Player loses if move is invalid
|
||||
if ((move & getLegalMoves()) == 0 || Long.bitCount(move) != 1){
|
||||
return new PlayResult(GameState.WIN, getNextPlayer());
|
||||
state = new PlayResult(GameState.WIN, getNextPlayer());
|
||||
return state;
|
||||
}
|
||||
|
||||
// Move is legal, make move
|
||||
@@ -56,7 +57,8 @@ public class BitboardTicTacToe extends BitboardGame {
|
||||
|
||||
// Check if current player won
|
||||
if (checkWin(playerBitboard)) {
|
||||
return new PlayResult(GameState.WIN, getCurrentPlayerIndex());
|
||||
state = new PlayResult(GameState.WIN, getCurrentPlayerIndex());
|
||||
return state;
|
||||
}
|
||||
|
||||
// Proceed to next turn
|
||||
@@ -65,11 +67,13 @@ public class BitboardTicTacToe extends BitboardGame {
|
||||
|
||||
// Check for early draw
|
||||
if (getLegalMoves() == 0L || checkEarlyDraw()) {
|
||||
return new PlayResult(GameState.DRAW, -1);
|
||||
state = new PlayResult(GameState.DRAW, -1);
|
||||
return state;
|
||||
}
|
||||
|
||||
// Nothing weird happened, continue on as normal
|
||||
return new PlayResult(GameState.NORMAL, -1);
|
||||
state = new PlayResult(GameState.NORMAL, -1);
|
||||
return state;
|
||||
}
|
||||
|
||||
private boolean checkWin(long board) {
|
||||
|
||||
@@ -44,10 +44,15 @@ public class ArtificialPlayer extends AbstractPlayer {
|
||||
* @return the integer representing the chosen move
|
||||
* @throws ClassCastException if {@code gameCopy} is not of type {@code T}
|
||||
*/
|
||||
public long getMove(TurnBasedGame gameCopy) {
|
||||
protected long determineMove(TurnBasedGame gameCopy) {
|
||||
return ai.getMove(gameCopy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a deep copy of this AI player.
|
||||
*
|
||||
* @return a copy of this player
|
||||
*/
|
||||
@Override
|
||||
public ArtificialPlayer deepCopy() {
|
||||
return new ArtificialPlayer(this);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.toop.framework.game.players;
|
||||
package org.toop.game.players;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||
@@ -6,65 +6,83 @@ import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* Represents a local player who provides moves manually.
|
||||
*
|
||||
* @param <T> the type of turn-based game
|
||||
*/
|
||||
public class LocalPlayer extends AbstractPlayer {
|
||||
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
||||
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
||||
|
||||
private CompletableFuture<Long> LastMove;
|
||||
private CompletableFuture<Long> LastMove = new CompletableFuture<>();
|
||||
|
||||
/**
|
||||
* Creates a new local player with the given name.
|
||||
*
|
||||
* @param name the player's name
|
||||
*/
|
||||
public LocalPlayer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of another local player.
|
||||
*
|
||||
* @param other the player to copy
|
||||
*/
|
||||
public LocalPlayer(LocalPlayer other) {
|
||||
super(other);
|
||||
this.LastMove = other.LastMove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for and returns the player's next legal move.
|
||||
*
|
||||
* @param gameCopy a copy of the current game
|
||||
* @return the chosen move
|
||||
*/
|
||||
@Override
|
||||
public long getMove(TurnBasedGame gameCopy) {
|
||||
return getValidMove(gameCopy);
|
||||
protected long determineMove(TurnBasedGame gameCopy) {
|
||||
long legalMoves = gameCopy.getLegalMoves();
|
||||
long move;
|
||||
|
||||
do {
|
||||
move = getLastMove();
|
||||
} while ((legalMoves & move) == 0);
|
||||
|
||||
return move;
|
||||
}
|
||||
|
||||
public void setMove(long move) {
|
||||
/**
|
||||
* Sets the player's last move.
|
||||
*
|
||||
* @param move the move to set
|
||||
*/
|
||||
public void setLastMove(long move) {
|
||||
LastMove.complete(move);
|
||||
}
|
||||
|
||||
// TODO: helper function, would like to replace to get rid of this method
|
||||
public static boolean contains(int[] array, int value){
|
||||
for (int i : array) if (i == value) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private long getMove2(TurnBasedGame gameCopy) {
|
||||
LastMove = new CompletableFuture<>();
|
||||
long move = 0;
|
||||
/**
|
||||
* Waits for the next move from the player.
|
||||
*
|
||||
* @return the chosen move or 0 if interrupted
|
||||
*/
|
||||
private long getLastMove() {
|
||||
LastMove = new CompletableFuture<>(); // Reset the future
|
||||
try {
|
||||
move = LastMove.get();
|
||||
System.out.println(Long.toBinaryString(move));
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
// TODO: Add proper logging.
|
||||
e.printStackTrace();
|
||||
return LastMove.get();
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
return 0;
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
protected long getValidMove(TurnBasedGame gameCopy){
|
||||
// Get this player's valid moves
|
||||
long validMoves = gameCopy.getLegalMoves();
|
||||
// Make sure provided move is valid
|
||||
// TODO: Limit amount of retries?
|
||||
// TODO: Stop copying game so many times
|
||||
long move = getMove2(gameCopy.deepCopy());
|
||||
while ((validMoves & move) == 0) {
|
||||
System.out.println("Not a valid move, try again");
|
||||
move = getMove2(gameCopy.deepCopy());
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a deep copy of this local player.
|
||||
*
|
||||
* @return a copy of this player
|
||||
*/
|
||||
@Override
|
||||
public LocalPlayer deepCopy() {
|
||||
return new LocalPlayer(this.getName());
|
||||
return new LocalPlayer(this);
|
||||
}
|
||||
|
||||
/*public void register() {
|
||||
|
||||
@@ -5,30 +5,45 @@ import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
/**
|
||||
* Represents a player controlled remotely or over a network.
|
||||
* <p>
|
||||
* This class extends {@link AbstractPlayer} and can be used to implement game logic
|
||||
* where moves are provided by an external source (e.g., another user or a server).
|
||||
* Currently, this class is a placeholder and does not implement move logic.
|
||||
* </p>
|
||||
* Represents a player that participates online.
|
||||
*
|
||||
* @param <T> the type of turn-based game
|
||||
*/
|
||||
public class OnlinePlayer extends AbstractPlayer {
|
||||
|
||||
/**
|
||||
* Constructs a new OnlinePlayer.
|
||||
* <p>
|
||||
* Currently, no additional initialization is performed. Subclasses or
|
||||
* future implementations should provide mechanisms to receive moves from
|
||||
* an external source.
|
||||
* Creates a new online player with the given name.
|
||||
*
|
||||
* @param name the name of the player
|
||||
*/
|
||||
public OnlinePlayer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of another online player.
|
||||
*
|
||||
* @param other the player to copy
|
||||
*/
|
||||
public OnlinePlayer(OnlinePlayer other) {
|
||||
super(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* This method is not supported for online players.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
protected long determineMove(TurnBasedGame gameCopy) {
|
||||
throw new UnsupportedOperationException("An online player does not support determining move");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Player deepCopy() {
|
||||
return new OnlinePlayer(this);
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ServerPlayer extends AbstractPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(TurnBasedGame game) {
|
||||
public long determineMove(TurnBasedGame game) {
|
||||
lastMove = new CompletableFuture<>();
|
||||
System.out.println("Sending yourturn");
|
||||
client.send("SVR GAME YOURTURN {TURNMESSAGE: \"<bericht voor deze beurt>\"}\n");
|
||||
|
||||
@@ -1,165 +0,0 @@
|
||||
package org.toop.framework.game.players.ai;
|
||||
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class MiniMaxAI extends AbstractAI {
|
||||
|
||||
private final int maxDepth;
|
||||
private final Random random = new Random();
|
||||
|
||||
public MiniMaxAI(int depth) {
|
||||
this.maxDepth = depth;
|
||||
}
|
||||
|
||||
public MiniMaxAI(MiniMaxAI other) {
|
||||
this.maxDepth = other.maxDepth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MiniMaxAI deepCopy() {
|
||||
return new MiniMaxAI(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(TurnBasedGame game) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
if (legalMoves == 0) return 0;
|
||||
|
||||
List<Long> bestMoves = new ArrayList<>();
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
int aiPlayer = game.getCurrentTurn();
|
||||
|
||||
long movesLoop = legalMoves;
|
||||
while (movesLoop != 0) {
|
||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||
TurnBasedGame copy = game.deepCopy();
|
||||
PlayResult result = copy.play(move);
|
||||
|
||||
int score;
|
||||
switch (result.state()) {
|
||||
case WIN -> score = (result.player() == aiPlayer ? maxDepth : -maxDepth);
|
||||
case DRAW -> score = 0;
|
||||
default -> score = getMoveScore(copy, maxDepth - 1, false, aiPlayer, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(move);
|
||||
} else if (score == bestScore) {
|
||||
bestMoves.add(move);
|
||||
}
|
||||
|
||||
movesLoop &= movesLoop - 1;
|
||||
}
|
||||
|
||||
long chosenMove = bestMoves.get(random.nextInt(bestMoves.size()));
|
||||
return chosenMove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive minimax with alpha-beta pruning and heuristic evaluation.
|
||||
*
|
||||
* @param game Current game state
|
||||
* @param depth Remaining depth
|
||||
* @param maximizing True if AI is maximizing, false if opponent
|
||||
* @param aiPlayer AI's player index
|
||||
* @param alpha Alpha value
|
||||
* @param beta Beta value
|
||||
* @return score of the position
|
||||
*/
|
||||
private int getMoveScore(TurnBasedGame game, int depth, boolean maximizing, int aiPlayer, int alpha, int beta) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
|
||||
// Terminal state
|
||||
PlayResult lastResult = null;
|
||||
if (legalMoves == 0) {
|
||||
lastResult = new PlayResult(GameState.DRAW, -1);
|
||||
}
|
||||
|
||||
// If the game is over or depth limit reached, evaluate
|
||||
if (depth <= 0 || legalMoves == 0) {
|
||||
if (lastResult != null) return 0;
|
||||
return evaluateBoard(game, aiPlayer);
|
||||
}
|
||||
|
||||
int bestScore = maximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
long movesLoop = legalMoves;
|
||||
|
||||
while (movesLoop != 0) {
|
||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||
TurnBasedGame copy = game.deepCopy();
|
||||
PlayResult result = copy.play(move);
|
||||
|
||||
int score;
|
||||
switch (result.state()) {
|
||||
case WIN -> score = (result.player() == aiPlayer ? depth : -depth);
|
||||
case DRAW -> score = 0;
|
||||
default -> score = getMoveScore(copy, depth - 1, !maximizing, aiPlayer, alpha, beta);
|
||||
}
|
||||
|
||||
if (maximizing) {
|
||||
bestScore = Math.max(bestScore, score);
|
||||
alpha = Math.max(alpha, bestScore);
|
||||
} else {
|
||||
bestScore = Math.min(bestScore, score);
|
||||
beta = Math.min(beta, bestScore);
|
||||
}
|
||||
|
||||
// Alpha-beta pruning
|
||||
if (beta <= alpha) break;
|
||||
|
||||
movesLoop &= movesLoop - 1;
|
||||
}
|
||||
|
||||
return bestScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple heuristic evaluation for Reversi-like games.
|
||||
* Positive = good for AI, Negative = good for opponent.
|
||||
*
|
||||
* @param game OnlineTurnBasedGame state
|
||||
* @param aiPlayer AI's player index
|
||||
* @return heuristic score
|
||||
*/
|
||||
private int evaluateBoard(TurnBasedGame game, int aiPlayer) {
|
||||
long[] board = game.getBoard();
|
||||
int aiCount = 0;
|
||||
int opponentCount = 0;
|
||||
|
||||
// Count pieces for AI vs opponent
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
long bits = board[i];
|
||||
for (int j = 0; j < 64; j++) {
|
||||
if ((bits & (1L << j)) != 0) {
|
||||
// Assume player 0 occupies even indices, player 1 occupies odd
|
||||
if ((i * 64 + j) % game.getPlayerCount() == aiPlayer) aiCount++;
|
||||
else opponentCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mobility (number of legal moves)
|
||||
int mobility = Long.bitCount(game.getLegalMoves());
|
||||
|
||||
// Corner control (top-left, top-right, bottom-left, bottom-right)
|
||||
int corners = 0;
|
||||
long[] cornerMasks = {1L << 0, 1L << 7, 1L << 56, 1L << 63};
|
||||
for (long mask : cornerMasks) {
|
||||
for (long b : board) {
|
||||
if ((b & mask) != 0) corners += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Weighted sum
|
||||
return (aiCount - opponentCount) + 2 * mobility + 5 * corners;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.toop.framework.game.players.ai;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class RandomAI extends AbstractAI {
|
||||
|
||||
public RandomAI() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomAI deepCopy() {
|
||||
return new RandomAI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(TurnBasedGame game) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
int move = new Random().nextInt(Long.bitCount(legalMoves));
|
||||
return nthBitIndex(legalMoves, move);
|
||||
}
|
||||
|
||||
public static long nthBitIndex(long bb, int n) {
|
||||
while (bb != 0) {
|
||||
int tz = Long.numberOfTrailingZeros(bb);
|
||||
if (n == 0) {
|
||||
return 1L << tz;
|
||||
}
|
||||
bb &= bb - 1; // clear the least significant 1
|
||||
n--;
|
||||
}
|
||||
return 0L; // not enough 1s
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,6 @@ public interface TurnBasedGame extends DeepCopyable<TurnBasedGame> {
|
||||
int getWinner();
|
||||
long getLegalMoves();
|
||||
PlayResult play(long move);
|
||||
PlayResult getState();
|
||||
boolean isTerminal();
|
||||
}
|
||||
|
||||
@@ -40,12 +40,29 @@ public abstract class AbstractPlayer implements Player {
|
||||
* @return an integer representing the chosen move
|
||||
* @throws UnsupportedOperationException if the method is not overridden
|
||||
*/
|
||||
public long getMove(TurnBasedGame gameCopy) {
|
||||
logger.error("Method getMove not implemented.");
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
public final long getMove(TurnBasedGame game) {
|
||||
return determineMove(game.deepCopy());
|
||||
}
|
||||
|
||||
public final String getName(){
|
||||
|
||||
/**
|
||||
* Determines the player's move using a safe copy of the game.
|
||||
* <p>
|
||||
* This method is called by {@link #getMove(T)} and should contain
|
||||
* the player's strategy for choosing a move.
|
||||
*
|
||||
* @param gameCopy a deep copy of the game
|
||||
* @return the chosen move
|
||||
*/
|
||||
protected abstract long determineMove(TurnBasedGame gameCopy);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the player's name.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user