-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.js
289 lines (231 loc) · 7.29 KB
/
tic-tac-toe.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// Tic-Tac-Toe-Bitboard Functions ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// helper function: remove set of elements from set
function removeSetFromSet(originalSet, toBeRemovedSet) {
toBeRemovedSet.forEach(Set.prototype.delete, originalSet);
}
function set_bits(bits){
let result = 0;
for (const b of bits){
result |= (1 << b);
}
return result;
}
function set_bit(n){
return 1 << n;
}
function empty(state){
let Free = new Set(Array(9).keys())
let new_arr = Array.from(Array(9).keys()).filter(n => ((state & (1 << n)) != 0));
removeSetFromSet(Free,new Set(new_arr));
new_arr = Array.from(Array(9).keys()).filter(n => ((state & (1 << (9+n))) != 0));
removeSetFromSet(Free,new Set(new_arr));
return Free;
}
function next_states(state, player){
let Empty = empty(state);
let Result = [];
for (n of Empty){
let next_state = state | set_bit(player * 9 + n);
Result.push(next_state);
}
return Result;
}
All_Lines = [ set_bits([0,1,2]), set_bits([3,4,5]), set_bits([6,7,8]),
set_bits([0,3,6]), set_bits([1,4,7]), set_bits([2,5,8]),
set_bits([0,4,8]), set_bits([2,4,6]) ]
function utility(state, player){
for (mask of All_Lines){
if((state & mask) == mask){
return 1 - 2 * player;
}
if(((state >> 9)& mask)==mask){
return -1 +2 * player;
}
}
if (((state&511)|(state>>9))!=511){
return null;
}
return 0;
}
// s1 = set_bits([0, 2, 3, 6, 1+9, 4+9, 5+9])
// console.log("u1: ",utility(s1,0));
// s2 = set_bits([0, 2, 6, 8, 1+9, 4+9, 7+9])
// console.log("u2: ",utility(s2,0));
// s3 = set_bits([0, 2, 5, 6, 7, 1+9, 3+9, 4+9, 8+9])
// console.log("u3: ",utility(s3,0));
// s4 = set_bits([0, 2, 5, 6, 1+9, 3+9, 4+9]);
// console.log("u4: ",utility(s4,0));
function finished(state){
return utility(state,0)!=null;
}
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Output Functions /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
const getDOMElements = () => {
resultElements = [];
for (let i of Array(3).keys()){
for (let j of Array(3).keys()){
resultElements.push(document.getElementById(i.toString()+j.toString()));
document.getElementById( i.toString()+j.toString() ).onclick = ()=>{player_move(i.toString()+","+j.toString())}
}
}
return resultElements;
}
const editTextOfDOMElement = (e, text) => {
e.innerHTML = text;
}
function printBoard(state){
let x_state= state&511;
let o_state= state>>9;
let mask = 1;
for (let cell of Array(9).keys()){
if (x_state&mask){
editTextOfDOMElement(boardFields[cell],"X");
}
if (o_state&mask){
editTextOfDOMElement(boardFields[cell],"O");
}
mask = mask<<1;
}
}
function printStatus(msg){
status_field = document.getElementById("status-field")
editTextOfDOMElement(status_field, msg)
}
// helper function: turn decimal into binary
function dec2bin(dec) {
return (dec >> 0).toString(2);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Minimax Functions /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// helper function: choose random element
const choose = (choices) => {
var index = Math.floor(Math.random() * choices.length);
return choices[index]
}
let cache = {};
const memoize = (fn) => {
return (...args) => {
let arg1 = args[0];
let arg2 = args[1];
if ([arg1,arg2] in cache) {
//console.log("using mem func");
return cache[[arg1,arg2]];
} else {
let result = fn(arg1,arg2);
cache[[arg1,arg2]] = result;
return result;
}
}
}
function other(p){
if(p==0){
return 1;
}
return 0;
}
let num_value_calls=0;
const value = memoize(
(state, player) => {
num_value_calls+=1;
if (finished(state)) {
return utility(state, player)
}
let other_player = other(player)
let ns = next_states(state, player);
let result = [];
for (const s of ns) {
let new_val= value(s,other_player);
result.push(-new_val);
}
return Math.max(...result)
}
)
const best_move = (state, player) => {
let ns = next_states(state, player)
let bestVal = value(state, player)
let bestMoves = []
for(const s of ns) {
let other_val= value(s, other(player));
if(-other_val == bestVal) {
bestMoves.push(s)
}
}
bestState = choose(bestMoves)
return [bestVal, bestState]
}
function ki_move(){
res= best_move(state, players[0]);
val=res[0];
state =res[1];
console.log("KI made move - new state: " , dec2bin(state));
printBoard(state);
if (finished(state)){
find_winner(state);
return;
}
printStatus("KI made move - it't your turn!")
}
function player_move(field) {
if (!game_finished){
let res = field.split(",");
let row=parseInt(res[0]);
let col=parseInt(res[1]);
let mask = set_bit(9+row*3+col);
if ( ( (state&mask)==0 ) && ( ((state<<9)&mask)==0 ) ){
state = state|mask;
console.log("Player made move - new state: " , dec2bin(state));
printBoard(state);
if (finished(state)){
find_winner(state);
return;
}
printStatus("Player made move - KI, it's your turn!")
ki_move();
}else{
printStatus("Wrong input... Try again!");
}
}
}
function find_winner(state){
if (finished(state)){
if (utility(state,0)==1){
printStatus("Player X won!")
}else if (utility(state,0)==-1){
printStatus("Player O won!")
}else{
printStatus("Untenschieden!")
}
game_finished=true;
return true
return false
}
}
function reset_game(){
state= start;
game_finished = false;
for(e of boardFields){
editTextOfDOMElement(e,"");
}
if(Math.floor(Math.random()*2)){
ki_move()
}else{
printStatus("You make the first move!");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Program /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
const players = [0,1]
const start = 0
let state = start;
let game_finished = false;
let boardFields = getDOMElements();
if(Math.floor(Math.random()*2)){
ki_move()
}else{
printStatus("You make the first move!");
}