-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombos.java
More file actions
150 lines (128 loc) · 3.8 KB
/
Copy pathCombos.java
File metadata and controls
150 lines (128 loc) · 3.8 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.Random;
/*
* Generate permutations, generate random responses
*
*
*/
public class Combos {
private BoardCombo[] boards;
private int[] response;
private int score;
public Combos(Combos c) {
boards = new BoardCombo[197073];
response = new int[197073];
score = c.getScore();
for (int i = 0; i < 197073; i++) {
boards[i] = new BoardCombo(c.getBoard(i).getBoard());
response[i] = c.getResponse(i);
}
}
public Combos() {
boards = new BoardCombo[197073];
response = new int[197073];
long startTime = System.nanoTime();
int index = 0;
for (int i = 0; i < 9; i++) {
boards[index] = new BoardCombo();
boards[index].set(i+1, 1);
index++;
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
for (int k = 0; k < 9; k++) {
if (!(i == j || i == k || j == k)) {
boards[index] = new BoardCombo();
boards[index].set(i+1, 1);
boards[index].set(j+1, 2);
boards[index].set(k+1, 1);
index++;
}
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
for (int k = 0; k < 9; k++)
for (int l = 0; l < 9; l++)
for (int m = 0; m < 9; m++) {
if (!(i == j || i == k || i == l || i == m || j == k || j == l || j == m || k == l || k == m || l == m)) {
boards[index] = new BoardCombo();
boards[index].set(i+1, 1);
boards[index].set(j+1, 2);
boards[index].set(k+1, 1);
boards[index].set(l+1, 2);
boards[index].set(m+1, 1);
index++;
}
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
for (int k = 0; k < 9; k++)
for (int l = 0; l < 9; l++)
for (int m = 0; m < 9; m++)
for (int n = 0; n < 9; n++)
for (int o = 0; o < 9; o++) {
if (!(i == j || i == k || i == l || i == m || i == n || i == o || j == k || j == l || j == m || j == n || j == o ||
k == l || k == m || k == n || k == o || l == m || l == n || l == o || m == n || m == o || n == o)) {
boards[index] = new BoardCombo();
boards[index].set(i+1, 1);
boards[index].set(j+1, 2);
boards[index].set(k+1, 1);
boards[index].set(l+1, 2);
boards[index].set(m+1, 1);
boards[index].set(n+1, 2);
boards[index].set(o+1, 1);
index++;
}
}
for (int i = 0; i < 197073; i++)
response[i] = boards[i].getRandomMove();
setScore();
long endTime = System.nanoTime();
//System.out.println("Combos constructor took " + (endTime - startTime) / 1000000 + " milliseconds to populate 197,073 boards and responses.");
}
// Modifies 1/freq of the responses: 1 = all, 2 = 50%, 3 = 33%...
public void mutateResponse(int freq) {
Random rand = new Random();
for (int i = rand.nextInt(freq); i < 197073; i+=freq)
response[i] = boards[i].getRandomMove();
setScore();
}
public void setScore() {
score = 0;
BoardCombo b;
for (int i = 0; i < 197073; i++) {
b = new BoardCombo(boards[i].getBoard());
b.set(response[i], 2);
while (!b.isFull() && !b.checkWin())
b.randomMove();
if (b.checkWin() && b.lastMove() == 2)
score += 5;
else if (b.checkWin())
score -= 5;
else
score++;
}
}
public int getScore() {
return score;
}
public BoardCombo getBoard(int index) {
return boards[index];
}
public int getResponse(int index) {
return response[index];
}
public int findResponse(char[] c) {
boolean found = true;
for (int i = 0; i < 197073; i++) {
for (int j = 1; j <= 9; j++)
if (boards[i].get(j) != c[j-1]) {
found = false;
break;
}
if (found)
return response[i];
found = true;
}
return -1;
}
}