Removed old AI and old files. Added a new generic random AI. game no longer deals with translation.

This commit is contained in:
2025-12-05 01:05:11 +01:00
parent 3c283092a3
commit b3a97a8978
37 changed files with 179 additions and 1481 deletions

View File

@@ -2,6 +2,17 @@ package org.toop.framework.gameFramework.controller;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
import org.toop.framework.networking.events.NetworkEvents;
public interface GameController extends Controllable, SupportsOnlinePlay, UpdatesGameUI {
public interface GameController extends Controllable, UpdatesGameUI {
/** Called when it is this player's turn to make a move. */
void onYourTurn(NetworkEvents.YourTurnResponse event);
/** Called when a move from another player is received. */
void onMoveReceived(NetworkEvents.GameMoveResponse event);
/** Called when the game has finished, with the final result. */
void gameFinished(NetworkEvents.GameResultResponse event);
void sendMove(long clientId, long move);
}

View File

@@ -1,108 +0,0 @@
package org.toop.framework.gameFramework.model.game;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.Arrays;
public abstract class AbstractGame<T extends TurnBasedGame<T>> implements TurnBasedGame<T> {
private final int playerCount; // How many players are playing
private final Player<T>[] players;
private int turn = 0; // What turn it is in the game
/** Constant representing an empty position on the board. */
public static final int EMPTY = -1;
/** Number of rows in the game board. */
private final int rowSize;
/** Number of columns in the game board. */
private final int columnSize;
/** The game board stored as a one-dimensional array. */
private final int[] board;
protected AbstractGame(int rowSize, int columnSize, int playerCount, Player<T>[] players) {
assert rowSize > 0 && columnSize > 0;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.players = players;
board = new int[rowSize * columnSize];
Arrays.fill(board, EMPTY);
this.playerCount = playerCount;
}
protected AbstractGame(AbstractGame<T> other){
this.rowSize = other.rowSize;
this.columnSize = other.columnSize;
this.board = other.board.clone();
this.playerCount = other.playerCount;
this.turn = other.turn;
// TODO: Make this a deep copy, add deep copy interface to Player
this.players = other.players;
}
public static boolean contains(int[] array, int value) {
// O(n)
for (int element : array){
if (element == value) return true;
}
return false;
}
public Player<T> getPlayer(int index) {
return players[index];
}
public int getPlayerCount(){return this.playerCount;}
protected void nextTurn() {
turn += 1;
}
public int getCurrentTurn() {
return turn % playerCount;
}
protected void setBoard(int position) {
setBoard(position, getCurrentTurn());
}
protected void setBoard(int position, int player) {
this.board[position] = player;
}
/**
* Returns the number of rows in the board.
*
* @return number of rows
*/
public int getRowSize() {
return this.rowSize;
}
/**
* Returns the number of columns in the board.
*
* @return number of columns
*/
public int getColumnSize() {
return this.columnSize;
}
/**
* Returns a copy of the current board state.
*
* @return a cloned array representing the board
*/
public int[] getBoard() {
return this.board.clone();
}
}

View File

@@ -1,5 +1,5 @@
package org.toop.framework.gameFramework.model.game;
public interface BoardProvider {
int[] getBoard();
long[] getBoard();
}

View File

@@ -12,7 +12,7 @@ public interface Playable {
*
* @return an array of integers representing legal moves
*/
int[] getLegalMoves();
long getLegalMoves();
/**
* Plays the given move and returns the resulting game state.
@@ -20,5 +20,5 @@ public interface Playable {
* @param move the move to apply
* @return the {@link GameState} and additional info after the move
*/
PlayResult play(int move);
PlayResult play(long move);
}

View File

@@ -10,11 +10,11 @@ import org.toop.framework.networking.events.NetworkEvents;
public interface SupportsOnlinePlay {
/** Called when it is this player's turn to make a move. */
void onYourTurn(NetworkEvents.YourTurnResponse event);
void onYourTurn(long clientId);
/** Called when a move from another player is received. */
void onMoveReceived(NetworkEvents.GameMoveResponse event);
void onMoveReceived(long move);
/** Called when the game has finished, with the final result. */
void gameFinished(NetworkEvents.GameResultResponse event);
void gameFinished(String condition);
}

View File

@@ -2,6 +2,7 @@ package org.toop.framework.gameFramework.model.game.threadBehaviour;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
@@ -15,7 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* Subclasses implement the actual game-loop logic.
*/
public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implements ThreadBehaviour {
protected GameController controller;
/** Indicates whether the game loop or event processing is active. */
protected final AtomicBoolean isRunning = new AtomicBoolean();
@@ -33,4 +34,9 @@ public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implem
public AbstractThreadBehaviour(T game) {
this.game = game;
}
@Override
public void setController(GameController controller) {
this.controller = controller;
}
}

View File

@@ -1,5 +1,6 @@
package org.toop.framework.gameFramework.model.game.threadBehaviour;
import org.toop.framework.gameFramework.controller.GameController;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
import org.toop.framework.gameFramework.model.player.Player;
@@ -10,4 +11,5 @@ import org.toop.framework.gameFramework.model.player.Player;
* Defines how a game's execution is started, stopped, and which player is active.
*/
public interface ThreadBehaviour extends Controllable {
void setController(GameController controller);
}

View File

@@ -39,7 +39,7 @@ public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Play
* @return an integer representing the chosen move
* @throws UnsupportedOperationException if the method is not overridden
*/
public int getMove(T gameCopy) {
public long getMove(T gameCopy) {
logger.error("Method getMove not implemented.");
throw new UnsupportedOperationException("Not supported yet.");
}

View File

@@ -3,5 +3,5 @@ package org.toop.framework.gameFramework.model.player;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface MoveProvider<T extends TurnBasedGame<T>> {
int getMove(T game);
long getMove(T game);
}