-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickAI.java
More file actions
88 lines (80 loc) · 2.44 KB
/
Copy pathquickAI.java
File metadata and controls
88 lines (80 loc) · 2.44 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.List;
public class quickAI {
//Tested
private CodeRunner runner;
public quickAI(CodeRunner runner){
this.runner = runner;
}
public int recommendMove(Board b, int movesDeep) {
double record = Double.NEGATIVE_INFINITY;
int best = (int) (Math.random() * 4);
for(int direction = 0; direction < 4; direction++){
Board moveOutcome = b.clone();
moveOutcome.moveContents(direction);
moveOutcome.createRandomTile(runner.TILE_MULTIPLIER, runner.POWER_SPAWN_VALUE);
double assessment = assessBoard(moveOutcome, movesDeep - 1);
if (assessment > record && b.canMove(direction)){
best = direction;
record = assessment;
}
}
return best;
}
public double assessBoard(Board b, int movesDeep) {
if(movesDeep <= 0){
return assessBoard(b);
}
double record = Double.NEGATIVE_INFINITY;
for(int direction = 0; direction < 4; direction++){
Board moveOutcome = b.clone();
moveOutcome.moveContents(direction);
moveOutcome.createRandomTile(runner.TILE_MULTIPLIER, runner.POWER_SPAWN_VALUE);
double assessment = assessBoard(moveOutcome, movesDeep - 1);
if (assessment > record && b.canMove(direction)){
record = assessment;
}
}
return record;
}
//Assess the value of a single board. Currently just finds the value of a single board.
public double assessBoard(Board board){
return countOpenSpaces(board) + 0 * monotonisity(board) / 20;
}
//Counts the number of open spaces in a board, Successfully tested
public int countOpenSpaces(Board board){
int count = 0;
for (int i = 0; i < board.playBoard.length; i++){
for (Tile t : board.playBoard[i]){
if (t == null)
count++;
}
}
return count;
}
public double monotonisity(Board board){
double monotonisityRightLeft = 0;
for (int i = 0; i < board.getSize(); i++){
Tile previousTile = null;
for (Tile t: board.playBoard[i]){
if(t != null){
if (previousTile != null)
monotonisityRightLeft += Math.signum(t.getValue()-previousTile.getValue());
previousTile = t;
}
}
}
double monotonisityUpDown = 0;
for (int j = 0; j < board.getSize(); j++){
Tile previousTile = null;
for (int i = 0; i < board.getSize(); i++){
Tile t = board.getSpace(i, j);
if(t != null){
if (previousTile != null)
monotonisityUpDown += Math.signum(t.getValue()-previousTile.getValue());
previousTile = t;
}
}
}
return Math.abs(monotonisityRightLeft) + Math.abs(monotonisityUpDown);
}
}