forked from Ludeme/LudiiExampleAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomAI.java
75 lines (60 loc) · 1.67 KB
/
RandomAI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package random;
import java.util.concurrent.ThreadLocalRandom;
import game.Game;
import main.FastArrayList;
import util.AI;
import util.Context;
import util.Move;
import util.action.ActionPass;
import util.state.GameType;
import utils.AIUtils;
/**
* Example third-party implementation of a random AI for Ludii
*
* @author Dennis Soemers
*/
public class RandomAI extends AI
{
//-------------------------------------------------------------------------
/** Our player index */
protected int player = -1;
//-------------------------------------------------------------------------
/**
* Constructor
*/
public RandomAI()
{
this.friendlyName = "Example Random AI";
}
//-------------------------------------------------------------------------
@Override
public Move selectAction
(
final Game game,
final Context context,
final double maxSeconds,
final int maxIterations,
final int maxDepth
)
{
FastArrayList<Move> legalMoves = game.moves(context).moves();
if (legalMoves.isEmpty())
{
final Move passMove = new Move(new ActionPass());
passMove.setMover(player);
return passMove;
}
// 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)
legalMoves = AIUtils.extractMovesForMover(legalMoves, player);
final int r = ThreadLocalRandom.current().nextInt(legalMoves.size());
return legalMoves.get(r);
}
@Override
public void initAI(final Game game, final int playerID)
{
this.player = playerID;
}
//-------------------------------------------------------------------------
}