Skip to content

Java Framework (How to Run)

Raluca D. Gaina edited this page Jun 4, 2019 · 1 revision

Class Test.java contains functionality for running the framework. It follows several steps, as follows:

Step 1: Create the game instance

Game game = new Game(seed, boardSize, gameMode, gameIDstr);

  • seed = random seed used for board generation. The game is otherwise deterministic.
  • boardSize = size of the board for this game.
  • gameMode = mode of game, from utils.Types.GAME_MODE enum.
  • gameIDstr = string by which this game should be identified for replay purposes, can be left empty ("").

Step 2: Create key controllers for human player

KeyController ki1 = new KeyController(true); // Primary keys: arrows + space bar)
KeyController ki2 = new KeyController(false); // Secondary keys: WASD + right shift

Step 3: Create players

All players will be added into an ArrayList: ArrayList<Player> players = new ArrayList<>(); The order in which they are added into the list defines the player order (i.e. first player in the list will be player 0). This list MUST contain 4 players.

The general constructor followed by all players receives a random seed and a playerID. The player ID is an integer in [10, 13]. All player IDs are defined as types in utils.Types.TILETYPE, with the first player being utils.Types.TILETYPE.AGENT0.

See AI Players page for all AI player options.

After 4 players are added into the list, the game can be informed of the players: game.setPlayers(players);

Step 4: Running the game

Run.runGame(game, ki1, ki2, useSeparateThreads);

This method runs the given game one time. The useSeparateThreads flag may be set to true if the agents should run in parallel at every game tick when making decisions about their next actions (useful in speeding up execution with agents which take longer to make decisions, such as MCTS and RHEA).

Optional: Replaying games

game.setLogGame(true); may be used before running a game to log the given run.

Game.getLastReplayGame(); would then retrieve the last game for which actions were logged. This game instance can then be used to run the replay as with a normal game:

Game replay = Game.getLastReplayGame();
Run.runGame(replay, ki1, ki2, useSeparateThreads);

The GameLog.JSON_GAMELOGS_PATH and GameLog.GAMELOGS_PATH static variables define where the logged games will be stored, depending on whether Game.LOG_JSON is set to true (so the game will be logged in JSON format) or false (the game log will be serialized), respectively.

Optional: Running multiple games

Run.runGames(game, new long[]{seed}, N, useSeparateThreads);

This method runs the given game N times, possibly in separate threads, using the random seeds supplied in an array. If N random seeds are given, then each instance of the game will use a new board generated with the corresponding random seed.