Added a method getWinner() to game interface.Controller now tells gameThreads how to deal with drawing UI and sending a move to server.

This commit is contained in:
2025-12-05 12:16:03 +01:00
parent 38de4f2156
commit 64016bd3f0
13 changed files with 97 additions and 44 deletions

View File

@@ -8,6 +8,8 @@ import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.function.Consumer;
/**
* Handles local turn-based game logic at a fixed update rate.
* <p>
@@ -22,7 +24,7 @@ public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends A
*
* @param game the game instance
*/
public LocalFixedRateThreadBehaviour(T game) {
public LocalFixedRateThreadBehaviour(T game, Consumer<Long> onSendMove) {
super(game);
}
@@ -60,7 +62,8 @@ public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends A
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
long move = currentPlayer.getMove(game.deepCopy());
PlayResult result = game.play(move);
new EventFlow().addPostEvent(GUIEvents.RefreshGameCanvas.class).postEvent();
updateUI();
GameState state = result.state();
switch (state) {

View File

@@ -8,6 +8,8 @@ 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.function.Consumer;
/**
* Handles local turn-based game logic in its own thread.
* <p>
@@ -49,7 +51,8 @@ public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractTh
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
long move = currentPlayer.getMove(game.deepCopy());
PlayResult result = game.play(move);
controller.updateUI();
updateUI();
GameState state = result.state();
switch (state) {

View File

@@ -53,7 +53,7 @@ public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractT
public void onYourTurn(long clientId) {
if (!isRunning.get()) return;
long move = game.getPlayer(game.getCurrentTurn()).getMove(game.deepCopy());
controller.sendMove(clientId, move);
sendMove(clientId, move);
}
/**
@@ -63,7 +63,8 @@ public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractT
public void onMoveReceived(long move) {
if (!isRunning.get()) return;
game.play(move);
new EventFlow().addPostEvent(GUIEvents.RefreshGameCanvas.class).postEvent();
updateUI();
}
/**
@@ -72,9 +73,8 @@ public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractT
*/
public void gameFinished(String condition) {
switch(condition.toUpperCase()){
case "WIN" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, game.getCurrentTurn()).postEvent();
case "WIN", "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, game.getWinner()).postEvent();
case "DRAW" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, false, -1).postEvent();
case "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, (game.getCurrentTurn() + 1)%2).postEvent();
default -> {
logger.error("Invalid condition");
throw new RuntimeException("Unknown condition");

View File

@@ -97,17 +97,13 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
final long skippedLegalMoves = getLegalMoves();
if (skippedLegalMoves == 0) {
final long black = getPlayerBitboard(0);
final long white = getPlayerBitboard(1);
int winner = getWinner();
final int blackCount = Long.bitCount(black);
final int whiteCount = Long.bitCount(white);
if (blackCount == whiteCount) {
if (winner == -1) {
return new PlayResult(GameState.DRAW, -1);
}
return new PlayResult(GameState.WIN, blackCount > whiteCount ? 0 : 1);
return new PlayResult(GameState.WIN, winner);
}
return new PlayResult(GameState.TURN_SKIPPED, getCurrentPlayerIndex());
@@ -123,6 +119,24 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
);
}
public int getWinner(){
final long black = getPlayerBitboard(0);
final long white = getPlayerBitboard(1);
final int blackCount = Long.bitCount(black);
final int whiteCount = Long.bitCount(white);
if (blackCount == whiteCount){
return -1;
}
else if (blackCount > whiteCount){
return 0;
}
else{
return 1;
}
}
private long computeMoves(long player, long opponent, int shift, long mask) {
long moves = shift(player, shift, mask) & opponent;
long captured = moves;

View File

@@ -32,6 +32,10 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
return (~taken) & 0x1ffL;
}
public int getWinner(){
return getCurrentPlayerIndex();
}
public PlayResult play(long move) {
// Player loses if move is invalid
if ((move & getLegalMoves()) == 0 || Long.bitCount(move) != 1){