-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRandomPlayer.java
More file actions
37 lines (31 loc) · 823 Bytes
/
RandomPlayer.java
File metadata and controls
37 lines (31 loc) · 823 Bytes
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
package ticTacToe;
import java.util.Random;
/**
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
public class RandomPlayer implements Player {
private final Random random;
private final int m, n;
public RandomPlayer(final Random random, int m, int n) {
this.random = random;
this.n = n;
this.m = m;
}
public RandomPlayer() {
this(3, 3);
}
public RandomPlayer(int m, int n) {
this(new Random(), m, n);
}
@Override
public Move move(final Position position, final Cell cell) {
while (true) {
int r = random.nextInt(m);
int c = random.nextInt(n);
final Move move = new Move(r, c, cell);
if (position.isValid(move)) {
return move;
}
}
}
}