-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
54 lines (45 loc) · 1.42 KB
/
ai.py
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
import core
"""
this file contains the core function of ai
"""
"""
get the all possible policy
"""
def available_policy(board):
return [x for x in range(9) if board[x]==0]
"""
Max Method
"""
def best_policy_and_util(board,label):
possible_policies =available_policy(board)
best_policy = core.INIT_POLICY
(has_empty,winner)=core.get_game_state(board)
max_util = core.INIT_MAX_UTIL
if (has_empty and winner == 0 ):
for policy in possible_policies:
image_board = core.apply_policy(board,policy,label)
(_ , r) = worst_policy_and_util(image_board,-label)
if(r>max_util):
best_policy = policy
max_util=r
else:
max_util = winner*10
return (best_policy,max_util)
"""
Min method
"""
def worst_policy_and_util(board,label):
possible_policies = available_policy(board)
worst_policy = core.INIT_POLICY
(has_empty,winner)=core.get_game_state(board)
min_util = core.INIT_MIN_UTIL
if (has_empty and winner == 0 ):
for policy in possible_policies:
image_board = core.apply_policy(board,policy,label)
(_ , temp_util) =best_policy_and_util(image_board,-label)
if(temp_util<min_util):
worst_policy = policy
min_util=temp_util
else:
min_util = winner*10
return (worst_policy,min_util)