-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulate.java
More file actions
122 lines (99 loc) · 2.69 KB
/
Copy pathSimulate.java
File metadata and controls
122 lines (99 loc) · 2.69 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Canvas;
import java.util.HashMap;
class Move
{
private GameBoard state;
public double score;
public int bestMove = -1;
private static HashMap<String, Move> allMoves = new HashMap<String, Move>();
private final int maxItt = 3;
private int itteration;
private void getBestAction()
{
String s = toString();
Move get = allMoves.get(s);
if (get != null && get.itteration <= itteration && false)
{
// System.out.println("Saved");
bestMove = get.bestMove;
return;
} else {
allMoves.put(s, this);
}
if (itteration == maxItt)
score = state.score();
else {
double highestAverageScore = 0;
int bestAction = 0;
for (int i = 0; i < 4; i++)
{
double scoreSum = 0;
GameBoard c = new GameBoard(state, i);
int openSpaces = c.getOpenSpaces().size()/2;
if (openSpaces == 0)
{
scoreSum -= c.score();
continue;
}
for (int j = 0; j < openSpaces; j++)
{
Move m = new Move(c, itteration+1, j);
scoreSum += m.score; //* (j%2 == 0 ? .9 : .1);
}
if (highestAverageScore < scoreSum/openSpaces)
{
highestAverageScore = scoreSum/openSpaces;
bestAction = i;
}
}
score = highestAverageScore;
bestMove = bestAction;
}
}
public Move(GameBoard s)
{
state = s;
itteration = 0;
getBestAction();
}
public Move(GameBoard s, int itt, int verse)
{
s = new GameBoard(s);
s.addTile(verse);
state = s;
itteration = itt;
getBestAction();
}
public String toString()
{
String str = "";
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
str+= state.board[x][y]+",";
}
}
return str;
}
}
public class Simulate {
private GameBoard current;
private int move = 0;
public Simulate()
{
current = new GameBoard();
}
public void draw(Graphics window)
{
Move m = new Move(current);
current.move(m.bestMove);
current.addTile();
current.draw(window);
move++;
window.drawString(""+move, 600, 100);
}
}