-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPlayer.java
37 lines (33 loc) · 908 Bytes
/
RandomPlayer.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
import java.util.*;
/**
* A rando player for a Sokoban puzzle.
*
* @author Dr Mark C. Sinclair
* @version September 2021
*
*/
public class RandomPlayer implements Player {
/**
* Default constructor
*/
public RandomPlayer() {
rnd = new Random();
}
/**
* Select a random direction from the vector of choices.
*
* @param choices possible directions for the player to choose from
* @return a random direction
*/
@Override
public Direction move(Vector<Direction> choices) {
if (choices == null)
throw new IllegalArgumentException("cannot have null choices");
if (choices.isEmpty())
throw new IllegalArgumentException("cannot have empty choices");
int size = choices.size();
int idx = rnd.nextInt(size);
return choices.get(idx);
}
private Random rnd = null;
}