forked from aimacode/aima-julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.jl
523 lines (449 loc) · 18.2 KB
/
games.jl
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import Base.display;
export AbstractGame, Figure52Game, TicTacToeGame, ConnectFourGame,
TicTacToeState, ConnectFourState,
minimax_decision, alphabeta_full_search, alphabeta_search,
display,
random_player, alphabeta_player, play_game;
abstract type AbstractGame end;
#=
Game is an abstract game that contains an initial state.
Games have a corresponding utility function, terminal test, set of legal moves, and transition model.
=#
struct Game <: AbstractGame
initial::String
function Game(initial_state::String)
return new(initial_state);
end
end
function actions(game::T, state::String) where {T <: AbstractGame}
println("actions() is not implemented yet for ", typeof(game), "!");
nothing;
end
function result(game::T, state::String, move::String) where {T <: AbstractGame}
println("result() is not implemented yet for ", typeof(game), "!");
nothing;
end
function utility(game::T, state::String, player::String) where {T <: AbstractGame}
println("utility() is not implemented yet for ", typeof(game), "!");
nothing;
end
function terminal_test(game::T, state::String) where {T <: AbstractGame}
if (length(actions(game, state)) == 0)
return true;
else
return false;
end
end
function to_move(game::T, state::String) where {T <: AbstractGame}
println("to_move() is not implemented yet for ", typeof(game), "!");
nothing;
end
function display(game::T, state::String) where {T <: AbstractGame}
println(state);
end
#=
Figure52Game is the game represented by the game tree in Fig. 5.2.
=#
struct Figure52Game <: AbstractGame
initial::String
nodes::Dict
utilities::Dict
function Figure52Game()
return new("A", Dict([
Pair("A", Dict("A1"=>"B", "A2"=>"C", "A3"=>"D")),
Pair("B", Dict("B1"=>"B1", "B2"=>"B2", "B3"=>"B3")),
Pair("C", Dict("C1"=>"C1", "C2"=>"C2", "C3"=>"C3")),
Pair("D", Dict("D1"=>"D1", "D2"=>"D2", "D3"=>"D3")),
]),
Dict([
Pair("B1", 3),
Pair("B2", 12),
Pair("B3", 8),
Pair("C1", 2),
Pair("C2", 4),
Pair("C3", 6),
Pair("D1", 14),
Pair("D2", 5),
Pair("D3", 2),
]));
end
end
function actions(game::Figure52Game, state::String)
return collect(keys(get(game.nodes, state, Dict())));
end
function result(game::Figure52Game, state::String, move::String)
return game.nodes[state][move];
end
function utility(game::Figure52Game, state::String, player::String)
if (player == "MAX")
return game.utilities[state];
else
return -game.utilities[state];
end
end
function terminal_test(game::Figure52Game, state::String)
return !(state in ["A", "B", "C", "D"]);
end
function to_move(game::Figure52Game, state::String)
return if_((state in ["B", "C", "D"]), "MIN", "MAX");
end
struct TicTacToeState
turn::String
utility::Int64
board::Dict
moves::AbstractVector
function TicTacToeState(turn::String, utility::Int64, board::Dict, moves::AbstractVector)
return new(turn, utility, board, moves);
end
end
#=
TicTacToeGame is a AbstractGame implementation of the Tic-tac-toe game.
=#
struct TicTacToeGame <: AbstractGame
initial::TicTacToeState
h::Int64
v::Int64
k::Int64
function TicTacToeGame(initial::TicTacToeState)
return new(initial, 3, 3, 3);
end
function TicTacToeGame()
return new(TicTacToeState("X", 0, Dict(), collect((x, y) for x in 1:3 for y in 1:3)), 3, 3, 3);
end
end
function actions(game::TicTacToeGame, state::TicTacToeState)
return state.moves;
end
function result(game::TicTacToeGame, state::TicTacToeState, move::Tuple{Signed, Signed})
if (!(move in state.moves))
return state;
end
local board::Dict = copy(state.board);
board[move] = state.turn;
local moves::Array{eltype(state.moves), 1} = collect(state.moves);
for (i, element) in enumerate(moves)
if (element == move)
deleteat!(moves, i);
break;
end
end
return TicTacToeState(if_((state.turn == "X"), "O", "X"), compute_utility(game, board, move, state.turn), board, moves);
end
function utility(game::TicTacToeGame, state::TicTacToeState, player::String)
return if_((player == "X"), state.utility, -state.utility);
end
function terminal_test(game::TicTacToeGame, state::TicTacToeState)
return ((state.utility != 0) || (length(state.moves) == 0));
end
function to_move(game::TicTacToeGame, state::TicTacToeState)
return state.turn;
end
function display(game::TicTacToeGame, state::TicTacToeState)
for x in 1:game.h
for y in 1:game.v
print(get(state.board, (x, y), "."));
end
println();
end
end
function compute_utility(game::TicTacToeGame, board::Dict, move::Tuple{Signed, Signed}, player::String)
if (k_in_row(game, board, move, player, (0, 1)) ||
k_in_row(game, board, move, player, (1, 0)) ||
k_in_row(game, board, move, player, (1, -1)) ||
k_in_row(game, board, move, player, (1, 1)))
return if_((player == "X"), 1, -1);
else
return 0;
end
end
function k_in_row(game::TicTacToeGame, board::Dict, move::Tuple{Signed, Signed}, player::String, delta::Tuple{Signed, Signed})
local delta_x::Int64 = Int64(getindex(delta, 1));
local delta_y::Int64 = Int64(getindex(delta, 2));
local x::Int64 = Int64(getindex(move, 1));
local y::Int64 = Int64(getindex(move, 2));
local n::Int64 = Int64(0);
while (get(board, (x,y), nothing) == player)
n = n + 1;
x = x + delta_x;
y = y + delta_y;
end
x = Int64(getindex(move, 1));
y = Int64(getindex(move, 2));
while (get(board, (x,y), nothing) == player)
n = n + 1;
x = x - delta_x;
y = y - delta_y;
end
n = n - 1; #remove the duplicate check on get(board, move, nothing)
return n >= game.k;
end
const ConnectFourState = TicTacToeState;
#=
ConnectFourGame is a AbstractGame implementation of the Connect Four game.
=#
struct ConnectFourGame <: AbstractGame
initial::ConnectFourState
h::Int64
v::Int64
k::Int64
function ConnectFourGame(initial::ConnectFourState)
return new(initial, 3, 3, 3);
end
function ConnectFourGame()
return new(ConnectFourState("X", 0, Dict(), collect((x, y) for x in 1:7 for y in 1:6)), 7, 6, 4);
end
end
function actions(game::ConnectFourGame, state::ConnectFourState)
return collect((x,y) for (x, y) in state.moves if ((y == 0) || ((x, y - 1) in state.board)));
end
function result(game::ConnectFourGame, state::ConnectFourState, move::Tuple{Signed, Signed})
if (!(move in state.moves))
return state;
end
local board::Dict = copy(state.board);
board[move] = state.turn;
local moves::Array{eltype(state.moves), 1} = collect(state.moves);
for (i, element) in enumerate(moves)
if (element == move)
deleteat!(moves, i);
break;
end
end
return ConnectFourState(if_((state.turn == "X"), "O", "X"), compute_utility(game, board, move, state.turn), board, moves);
end
function utility(game::ConnectFourGame, state::ConnectFourState, player::String)
return if_((player == "X"), state.utility, -state.utility);
end
function terminal_test(game::ConnectFourGame, state::ConnectFourState)
return ((state.utility != 0) || (length(state.moves) == 0));
end
function to_move(game::ConnectFourGame, state::ConnectFourState)
return state.turn;
end
function display(game::ConnectFourGame, state::ConnectFourState)
for x in 1:game.h
for y in 1:game.v
print(get(state.board, (x, y), "."));
end
println();
end
end
function compute_utility(game::ConnectFourGame, board::Dict, move::Tuple{Signed, Signed}, player::String)
if (k_in_row(game, board, move, player, (0, 1)) ||
k_in_row(game, board, move, player, (1, 0)) ||
k_in_row(game, board, move, player, (1, -1)) ||
k_in_row(game, board, move, player, (1, 1)))
return if_((player == "X"), 1, -1);
else
return 0;
end
end
function k_in_row(game::ConnectFourGame, board::Dict, move::Tuple{Signed, Signed}, player::String, delta::Tuple{Signed, Signed})
local delta_x::Int64 = Int64(getindex(delta, 1));
local delta_y::Int64 = Int64(getindex(delta, 2));
local x::Int64 = Int64(getindex(move, 1));
local y::Int64 = Int64(getindex(move, 2));
local n::Int64 = Int64(0);
while (get(board, (x,y), nothing) == player)
n = n + 1;
x = x + delta_x;
y = y + delta_y;
end
x = Int64(getindex(move, 1));
y = Int64(getindex(move, 2));
while (get(board, (x,y), nothing) == player)
n = n + 1;
x = x - delta_x;
y = y - delta_y;
end
n = n - 1; #remove the duplicate check on get(board, move, nothing)
return n >= game.k;
end
function minimax_max_value(game::T, player::String, state::String) where {T <: AbstractGame}
if (terminal_test(game, state))
return utility(game, state, player)
end
local v::Float64 = -Inf64;
v = reduce(max, vcat(v, collect(minimax_min_value(game, player, result(game, state, action))
for action in actions(game, state))));
return v;
end
function minimax_min_value(game::T, player::String, state::String) where {T <: AbstractGame}
if (terminal_test(game, state))
return utility(game, state, player);
end
local v::Float64 = Inf64;
v = reduce(min, vcat(v, collect(minimax_max_value(game, player, result(game, state, action))
for action in actions(game, state))));
return v;
end
"""
minimax_decision(state, game)
Calculate the best move by searching through moves, all the way to the leaves (terminal states) (Fig 5.3).
"""
function minimax_decision(state::String, game::T) where {T <: AbstractGame}
local player = to_move(game, state);
return argmax(actions(game, state),
(function(action::String,; relevant_game::AbstractGame=game, relevant_player::String=player, relevant_state::String=state)
return minimax_min_value(relevant_game, relevant_player, result(relevant_game, relevant_state, action));
end));
end
function alphabeta_full_search_max_value(game::T, player::String, state::String, alpha::Number, beta::Number) where {T <: AbstractGame}
if (terminal_test(game, state))
return utility(game, state, player)
end
local v::Float64 = -Inf64;
for action in actions(game, state)
v = max(v, alphabeta_full_search_min_value(game, player, result(game, state, action), alpha, beta));
if (v >= beta)
return v;
end
alpha = max(alpha, v);
end
return v;
end
function alphabeta_full_search_min_value(game::T, player::String, state::String, alpha::Number, beta::Number) where {T <: AbstractGame}
if (terminal_test(game, state))
return utility(game, state, player);
end
local v::Float64 = Inf64;
for action in actions(game, state)
v = min(v, alphabeta_full_search_max_value(game, player, result(game, state, action), alpha, beta));
if (v <= alpha)
return v;
end
beta = min(beta, v);
end
return v;
end
"""
alphabeta_full_search(state, game)
Search the given game to find the best action using alpha-beta pruning (Fig 5.7).
"""
function alphabeta_full_search(state::String, game::T) where {T <: AbstractGame}
local player::String = to_move(game, state);
return argmax(actions(game, state),
(function(action::String,; relevant_game::AbstractGame=game, relevant_state::String=state, relevant_player::String=player)
return alphabeta_full_search_min_value(relevant_game, relevant_player, result(relevant_game, relevant_state, action), -Inf64, Inf64);
end));
end
function alphabeta_search_max_value(game::T, player::String, cutoff_test_fn::Function, evaluation_fn::Function, state::String, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff_test_fn(state, depth))
return evaluation_fn(state);
end
local v::Float64 = -Inf64;
for action in actions(game, state)
v = max(v, alphabeta_search_min_value(game, player, cutoff_test_fn, evaluation_fn, result(game, state, action), alpha, beta, depth + 1));
if (v >= beta)
return v;
end
alpha = max(alpha, v);
end
return v;
end
function alphabeta_search_max_value(game::T, player::String, cutoff_test_fn::Function, evaluation_fn::Function, state::TicTacToeState, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff_test_fn(state, depth))
return evaluation_fn(state);
end
local v::Float64 = -Inf64;
for action in actions(game, state)
v = max(v, alphabeta_search_min_value(game, player, cutoff_test_fn, evaluation_fn, result(game, state, action), alpha, beta, depth + 1));
if (v >= beta)
return v;
end
alpha = max(alpha, v);
end
return v;
end
function alphabeta_search_min_value(game::T, player::String, cutoff_test_fn::Function, evaluation_fn::Function, state::String, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff_test_fn(state, depth))
return evaluation_fn(state);
end
local v::Float64 = Inf64;
for action in actions(game, state)
v = min(v, alphabeta_search_max_value(game, player, cutoff_test_fn, evaluation_fn, result(game, state, action), alpha, beta, depth + 1));
if (v >= alpha)
return v;
end
beta = min(alpha, v);
end
return v;
end
function alphabeta_search_min_value(game::T, player::String, cutoff_test_fn::Function, evaluation_fn::Function, state::TicTacToeState, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff_test_fn(state, depth))
return evaluation_fn(state);
end
local v::Float64 = Inf64;
for action in actions(game, state)
v = min(v, alphabeta_search_max_value(game, player, cutoff_test_fn, evaluation_fn, result(game, state, action), alpha, beta, depth + 1));
if (v >= alpha)
return v;
end
beta = min(alpha, v);
end
return v;
end
"""
alphabeta_search(state, game)
Search the given game to find the best action using alpha-beta pruning. However, this function also uses a
cutoff test to cut off the search early and apply a heuristic evaluation function to turn nonterminal
states into terminal states.
"""
function alphabeta_search(state::String, game::T; d::Int64=4, cutoff_test_fn::Union{Nothing, Function}=nothing, evaluation_fn::Union{Nothing, Function}=nothing) where {T <: AbstractGame}
local player::String = to_move(game, state);
if (typeof(cutoff_test_fn) <: Nothing)
cutoff_test_fn = (function(state::String, depth::Int64; dvar::Int64=d, relevant_game::AbstractGame=game)
return ((depth > dvar) || terminal_test(relevant_game, state));
end);
end
if (typeof(evaluation_fn) <: Nothing)
evaluation_fn = (function(state::String, ; relevant_game::AbstractGame=game, relevant_player::String=player)
return utility(relevant_game, state, relevant_player);
end);
end
return argmax(actions(game, state),
(function(action::String,; relevant_game::AbstractGame=game, relevant_state::String=state, relevant_player::String=player, cutoff_test::Function=cutoff_test_fn, eval_fn::Function=evaluation_fn)
return alphabeta_search_min_value(relevant_game, relevant_player, cutoff_test, eval_fn, result(relevant_game, relevant_state, action), -Inf64, Inf64, 0);
end));
end
function alphabeta_search(state::TicTacToeState, game::T; d::Int64=4, cutoff_test_fn::Union{Nothing, Function}=nothing, evaluation_fn::Union{Nothing, Function}=nothing) where {T <: AbstractGame}
local player::String = to_move(game, state);
if (typeof(cutoff_test_fn) <: Nothing)
cutoff_test_fn = (function(state::TicTacToeState, depth::Int64; dvar::Int64=d, relevant_game::AbstractGame=game)
return ((depth > dvar) || terminal_test(relevant_game, state));
end);
end
if (typeof(evaluation_fn) <: Nothing)
evaluation_fn = (function(state::TicTacToeState, ; relevant_game::AbstractGame=game, relevant_player::String=player)
return utility(relevant_game, state, relevant_player);
end);
end
return argmax(actions(game, state),
(function(action::Tuple{Signed, Signed},; relevant_game::AbstractGame=game, relevant_state::TicTacToeState=state, relevant_player::String=player, cutoff_test::Function=cutoff_test_fn, eval_fn::Function=evaluation_fn)
return alphabeta_search_min_value(relevant_game, relevant_player, cutoff_test, eval_fn, result(relevant_game, relevant_state, action), -Inf64, Inf64, 0);
end));
end
function random_player(game::T, state::String) where {T <: AbstractGame}
return rand(RandomDeviceInstance, actions(game, state));
end
function random_player(game::T, state::TicTacToeState) where {T <: AbstractGame}
return rand(RandomDeviceInstance, actions(game, state));
end
function alphabeta_player(game::T, state::String) where {T <: AbstractGame}
return alphabeta_search(state, game);
end
function alphabeta_player(game::T, state::TicTacToeState) where {T <: AbstractGame}
return alphabeta_search(state, game);
end
function play_game(game::T, players::Vararg{Function}) where {T <: AbstractGame}
state = game.initial;
while (true)
for player in players
move = player(game, state);
state = result(game, state, move);
if (terminal_test(game, state))
return utility(game, state, to_move(game, game.initial));
end
end
end
end