Skip to content

Commit

Permalink
updated example AI code for 0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisSoemers committed Apr 3, 2020
1 parent 68a97c3 commit dab3151
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/experiments/RunCustomMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;

import game.Game;
import player.GameLoader;
import player.utils.GameLoader;
import random.RandomAI;
import search.mcts.MCTS;
import util.AI;
Expand Down Expand Up @@ -38,7 +38,7 @@ public class RunCustomMatch
//-------------------------------------------------------------------------

/** Name of game we wish to play */
static final String GAME_NAME = "board/space/blocking/Amazons.lud";
static final String GAME_NAME = "Amazons.lud";

/** Number of games to play */
static final int NUM_GAMES = 10;
Expand Down
4 changes: 2 additions & 2 deletions src/experiments/RunLudiiEvalGamesSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.Arrays;

import game.Game;
import player.GameLoader;
import player.experiments.EvalGamesSet;
import player.utils.GameLoader;
import random.RandomAI;
import search.mcts.MCTS;
import util.AI;
Expand All @@ -24,7 +24,7 @@ public class RunLudiiEvalGamesSet
//-------------------------------------------------------------------------

/** Name of game we wish to play */
static final String GAME_NAME = "board/space/blocking/Amazons.lud";
static final String GAME_NAME = "Amazons.lud";

/** Whether to create a small GUI that can be used to manually interrupt the experiment */
static final boolean USE_GUI = false;
Expand Down
26 changes: 13 additions & 13 deletions src/experiments/Tutorial.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import java.util.List;

import game.Game;
import game.types.GameType;
import main.FastArrayList;
import mcts.ExampleUCT;
import player.GameLoader;
import player.utils.GameLoader;
import random.RandomAI;
import util.AI;
import util.Context;
import util.Move;
import util.Trial;
import util.model.Model;
import util.state.GameType;
import util.state.containerState.ContainerState;

/**
Expand All @@ -34,15 +34,15 @@ public static void main(final String[] args)
System.out.println("Built-in games = " + Arrays.toString(games));

// one of the games is "Amazons.lud". Let's load it
Game game = GameLoader.loadGameFromName("board/space/blocking/Amazons.lud");
Game game = GameLoader.loadGameFromName("Amazons.lud");
game.create();

// the game's "stateFlags" contain properties of the game that may be
// important for some AI algorithms to know about
final long stateFlags = game.stateFlags();

// for example, we may like to know whether our game has stochastic elements
final boolean isStochastic = ((stateFlags & GameType.Stochastic) != 0);
final boolean isStochastic = ((stateFlags & GameType.Stochastic) != 0L);
if (isStochastic)
System.out.println(game.name() + " is stochastic.");
else
Expand All @@ -66,13 +66,13 @@ public static void main(final String[] args)
// for every container state we find (often just 1), we'll print a few things:

// print the collection of locations that are empty
System.out.println("Empty locations = " + containerState.emptyChunkSet());
System.out.println("Empty locations = " + containerState.emptyChunkSetCell());

// for every location that is owned by a player, print the owner
System.out.println("Who = " + containerState.cloneWho().toChunkString());
System.out.println("Who = " + containerState.cloneWhoCell().toChunkString());

// for every location that is occupied by a piece, print what piece occupies it
System.out.println("What = " + containerState.cloneWhat().toChunkString());
System.out.println("What = " + containerState.cloneWhatCell().toChunkString());
}

// print the full list of all legal moves
Expand All @@ -88,9 +88,9 @@ public static void main(final String[] args)
// let's print our empty/who/what again, see how they have changed
for (final ContainerState containerState : trial.state().containerStates())
{
System.out.println("Empty locations = " + containerState.emptyChunkSet());
System.out.println("Who = " + containerState.cloneWho().toChunkString());
System.out.println("What = " + containerState.cloneWhat().toChunkString());
System.out.println("Empty locations = " + containerState.emptyChunkSetCell());
System.out.println("Who = " + containerState.cloneWhoCell().toChunkString());
System.out.println("What = " + containerState.cloneWhatCell().toChunkString());
}

// request legal moves again and play one of them again
Expand All @@ -102,9 +102,9 @@ public static void main(final String[] args)
// let's have a final look at how our state looks after this second move
for (final ContainerState containerState : trial.state().containerStates())
{
System.out.println("Empty locations = " + containerState.emptyChunkSet());
System.out.println("Who = " + containerState.cloneWho().toChunkString());
System.out.println("What = " + containerState.cloneWhat().toChunkString());
System.out.println("Empty locations = " + containerState.emptyChunkSetCell());
System.out.println("Who = " + containerState.cloneWhoCell().toChunkString());
System.out.println("What = " + containerState.cloneWhatCell().toChunkString());
}

//---------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions src/mcts/ExampleDUCT.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import util.Move;
import util.Trial;
import util.action.Action;
import util.action.ActionPass;
import utils.AIUtils;

/**
Expand Down Expand Up @@ -345,9 +344,7 @@ public Node(final Node parent, final Context context)

if (legalMovesPerPlayer.get(p).isEmpty())
{
final Move passMove = new Move(new ActionPass());
passMove.setMover(p);
legalMovesPerPlayer.get(p).add(passMove);
legalMovesPerPlayer.get(p).add(Game.createPassMove(context));
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/mcts/ExampleUCT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import util.Context;
import util.Move;
import util.Trial;
import util.action.ActionPass;
import utils.AIUtils;

/**
Expand Down Expand Up @@ -286,7 +285,7 @@ private static class Node
* Constructor
*
* @param parent
* @param moveFromparent
* @param moveFromParent
* @param context
*/
public Node(final Node parent, final Move moveFromParent, final Context context)
Expand All @@ -304,9 +303,7 @@ public Node(final Node parent, final Move moveFromParent, final Context context)
if (unexpandedMoves.isEmpty())
{
// We need to add a forced pass move
final Move passMove = new Move(new ActionPass());
passMove.setMover(context.trial().state().mover());
unexpandedMoves.add(passMove);
unexpandedMoves.add(Game.createPassMove(context));
}

if (parent != null)
Expand Down
9 changes: 3 additions & 6 deletions src/random/RandomAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import java.util.concurrent.ThreadLocalRandom;

import game.Game;
import game.types.GameType;
import main.FastArrayList;
import util.AI;
import util.Context;
import util.Move;
import util.action.ActionPass;
import util.state.GameType;
import utils.AIUtils;

/**
Expand Down Expand Up @@ -50,14 +49,12 @@ public RandomAI()

if (legalMoves.isEmpty())
{
final Move passMove = new Move(new ActionPass());
passMove.setMover(player);
return passMove;
return Game.createPassMove(context);
}

// If we're playing a simultaneous-move game, some of the legal moves may be
// for different players. Extract only the ones that we can choose.
if ((game.stateFlags() & GameType.Simultaneous) != 0)
if ((game.stateFlags() & GameType.Simultaneous) != 0L)
legalMoves = AIUtils.extractMovesForMover(legalMoves, player);

final int r = ThreadLocalRandom.current().nextInt(legalMoves.size());
Expand Down

0 comments on commit dab3151

Please sign in to comment.