-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoePositionCreator.java
210 lines (177 loc) · 7 KB
/
tictactoePositionCreator.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.io.FileWriter;
import java.util.*;
public class tictactoePositionCreator {
private static Random r = new Random();
public static char[] getPosition(){
char[] board = {'_','_','_','_','_','_','_','_','_','X','9'};
List<String> availableIndexes = new ArrayList<>(List.of("0", "1", "2", "3", "4", "5", "6", "7", "8"));
int randStop = r.nextInt(1,7);
boolean isX = true;
boolean isWin = false;
int winIndex = 9;
while (availableIndexes.size() >= randStop){
int randMoveIndex = r.nextInt(0,availableIndexes.size());
int randMove = Integer.parseInt(availableIndexes.get(randMoveIndex));
availableIndexes.remove(randMoveIndex);
board[randMove] = isX ? 'X' : 'O';
isX = !isX;
if(isWin(board)){
// undo last move
board[randMove] = 'a';
isX = !isX;
isWin = true;
winIndex = randMove;
break;
}
}
// 2nd last is whose move it is
board[board.length-2] = isX ? 'X' : 'O';
// 1st last is if already a win, 9 == no win
board[board.length-1] = isWin ? (char) (winIndex+'0'): '9';
return board;
}
static List<String> positions = new ArrayList<>();
static List<Integer> correctIndexes = new ArrayList<>();
static HashSet<tictactoePos> positionsAutoGenerated = new HashSet<>();
public static void createDataAutoGenerated(int numIters){
for (int i = 0;i<numIters;i++){
char[] randPos = getPosition();
int winAlreadyFoundIndex = randPos[randPos.length-1] - '0';
int index;
if(winAlreadyFoundIndex != 9){
// win found so pick that
index = winAlreadyFoundIndex;
}
else{
boolean isXTurn = randPos[randPos.length-2] == 'X';
index = tryFindImediateWin(randPos,isXTurn);
}
if(index == -1){
System.out.println("No immediate win found, skipping position");
}
else{
System.out.println("Adding position");
positionsAutoGenerated.add(new tictactoePos(Arrays.copyOf(randPos,randPos.length-2),index));
}
}
System.out.println("Exiting auto creation after " + numIters + " iterations");
System.out.println(positionsAutoGenerated.size() + " Unique datapoints");
System.out.println("Saving to files...");
saveToFile(positionsAutoGenerated,"AutoGenerated");
}
public static void createData(){
Scanner scanner = new Scanner(System.in);
while (true){
char[] randPos = getPosition();
printpos(randPos);
int winAlreadyFoundIndex = randPos[randPos.length-1] - '0';
int index = inputIndex(scanner,winAlreadyFoundIndex);
if(index == -1){
System.out.println("Exiting creation");
System.out.println("Positions: " + positions.toString());
System.out.println("Indexes: " + correctIndexes.toString());
break;
}
else{
positions.add(Arrays.toString(Arrays.copyOf(randPos,randPos.length-2)));
correctIndexes.add(index);
}
}
scanner.close();
}
public static int inputIndex(Scanner scanner,int winAlreadyFoundIndex){
if(winAlreadyFoundIndex != 9){
// randomly already found a way to win
System.out.print("Please enter selected move index (enter -1 to exit) (Win already found press enter to use default of: " + winAlreadyFoundIndex + ")>> ");
String line = scanner.nextLine();
if(line.isEmpty()){
// enter key pressed
return winAlreadyFoundIndex;
}
else{
return Integer.parseInt(line);
}
}
else{
System.out.print("Please enter selected move index (enter -1 to exit)>> ");
return scanner.nextInt();
}
// Prompt the user for input
// Read an integer from stdin
// Close the scanner
}
private static void printpos(char[] pos){
System.out.println("Position: ");
System.out.println("Whose turn? : " + pos[pos.length-2]);
for(int i = 0;i<pos.length-2;i++){
System.out.print(pos[i]);
if((i+1) % 3 == 0){
System.out.print(" " + (i-2) + " " + (i-1) + " " + (i));
System.out.println();
}
}
System.out.println();
}
// for refererence:
// [0,1,2]
// [3,4,5]
// [6,7,8]
private static boolean isWin(char[] pos){
return is3Together(0,1,2,pos) ||
is3Together(3,4,5,pos) ||
is3Together(6,7,8,pos) ||
is3Together(0,3,6,pos) ||
is3Together(1,4,7,pos) ||
is3Together(2,5,8,pos) ||
is3Together(0,4,8,pos) ||
is3Together(2,4,6,pos);
}
private static boolean is3Together(int x,int y, int z,char[] pos){
return pos[x] == pos[y] && pos[x] == pos[z] && pos[x] != '_';
}
private static int tryFindImediateWin(char[] pos,boolean isXTurn){
char testChar = isXTurn ? 'X' : 'O';
for(int i = 0;i<pos.length-2;i++){
if(pos[i] == '_'){
// empty so we can try to see if this is winning
pos[i] = testChar;
if(isWin(pos)){
pos[i] = '_';
return i;
}
pos[i] = '_';
}
}
return -1;
}
private static <T> void saveToFile(List<tictactoePos> objects,String fileName){
try {
FileWriter fileWriterPositions = new FileWriter(fileName + "_positions.txt");
FileWriter fileWriterIndexes = new FileWriter(fileName + "_indexes.txt");
for(tictactoePos p : objects){
fileWriterPositions.write("\"" + p.getPosWithoutBrackets() + "\",");
fileWriterIndexes.write(p.correctMoveIndex + ",");
}
fileWriterPositions.close();
fileWriterIndexes.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static <T> void saveToFile(HashSet<tictactoePos> objects,String fileName){
try {
FileWriter fileWriterPositions = new FileWriter(fileName + "_positions.txt");
FileWriter fileWriterIndexes = new FileWriter(fileName + "_indexes.txt");
for(tictactoePos p : objects){
fileWriterPositions.write("\"" + p.getPosWithoutBrackets() + "\",");
fileWriterIndexes.write(p.correctMoveIndex + ",");
}
fileWriterPositions.close();
fileWriterIndexes.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}