-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIPlayer.java
More file actions
44 lines (34 loc) · 1.21 KB
/
Copy pathAIPlayer.java
File metadata and controls
44 lines (34 loc) · 1.21 KB
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
package tictactoe;
import java.util.ArrayList;
import java.util.Random;
public abstract class AIPlayer {
protected String difficulty;
protected char identifier;
public AIPlayer(char identifier, String difficulty) {
this.identifier = identifier;
this.difficulty = difficulty;
}
public abstract int getTurn(char[][] gameboard);
protected int getRandomMove(char[][] gameboard) {
ArrayList<Integer> open = getOpenSpaces(gameboard);
Random random = new Random();
return open.get(random.nextInt(open.size()));
}
protected ArrayList<Integer> getOpenSpaces(char[][] gameboard) {
ArrayList<Integer> open = new ArrayList<>();
for (int i = 0; i < gameboard.length; i++) {
for (int j = 0; j < gameboard.length; j++) {
if (gameboard[i][j] == '_' || gameboard[i][j] == ' ') {
open.add(translateToPos(gameboard.length, i, j));
}
}
}
return open;
}
public int translateToPos(int boardLength, int row, int col) {
return (boardLength - (row + 1)) * boardLength + (col + 1);
}
public String getDifficulty() {
return difficulty;
}
}