-
Notifications
You must be signed in to change notification settings - Fork 2
/
alphabeta.py
74 lines (65 loc) · 2.11 KB
/
alphabeta.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random
import queue as q
INF = 10**18
global ptr
ptr = 0
arr = [-1,3,5,10,-4,-6,-10,-10]
class Node:
def __init__(self,val,num_child):
self.val = val
self.num_child = num_child
self.child = [None]*num_child
def random_branch():
# return random.randint(1,4)
return 2
def alphabeta(curr_player,depth,alpha,beta):
if depth>=3:
# val = random.randint(-100,100)
global ptr
val = arr[ptr]
ptr+=1
return (Node(val,0))
if curr_player == 0:
best = -INF
num_child = random_branch()
node = Node(best,num_child)
for i in range(num_child):
node_child = alphabeta(curr_player^1,depth+1,alpha,beta)
node.child[i] = node_child
best = max(best,node_child.val)
alpha = max(best,alpha)
# print("Player: "+str(curr_player)+" Value: "+str(node_child.val)+" Best: "+str(best)+" Alpha: "+str(alpha)+" Beta: "+str(beta))
if beta<=alpha:
ptr+=(2**(3-depth)-1)
# print("Breaking")
break
node.val = best
return node
else:
best = INF
num_child = random_branch()
node = Node(best,num_child)
for i in range(num_child):
node_child = alphabeta(curr_player^1,depth+1,alpha,beta)
node.child[i] = node_child
best = min(best,node_child.val)
beta = min(best,beta)
# print("Player: "+str(curr_player)+" Value: "+str(node_child.val)+" Best: "+str(best)+" Alpha: "+str(alpha)+" Beta: "+str(beta))
if beta<=alpha:
ptr+=(2**(3-depth)-1)
# print("Breaking")
break
node.val = best
return node
def bfs(root):
qq = q.Queue()
depth = 0
qq.put((root,depth))
while qq.empty()==False:
node = qq.get()
print("Depth: "+str(node[1])+" Value: "+str(node[0].val))
for i in range(node[0].num_child):
if node[0].child[i] is not None:
qq.put((node[0].child[i],node[1]+1))
root = alphabeta(0,0,-INF,INF)
bfs(root)