-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.py
48 lines (37 loc) · 1.24 KB
/
minimax.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
class Node:
def __init__(self, is_leaf=False, val=None):
self.is_leaf = is_leaf
self._value = None
self.children = None
self.value = val
def set_children(self, *children):
self.children = []
for child in children:
self.children.append(child)
@property
def value(self):
return self._value
@value.setter
def value(self, val):
self._value = val
def create_minimax_game():
root = Node()
left = Node()
right = Node()
root.set_children(left, right)
left.set_children(Node(val=3), Node(val=5))
right.set_children(Node(val=3), Node(val=5))
return root
def minimax(node, is_player1):
if node.children[0].value is not None:
if is_player1:
return max(map(lambda som_node: som_node.value, node.children))
else:
return min(map(lambda som_node: som_node.value, node.children))
else:
if is_player1:
return max(map(lambda som_node: minimax(som_node, not is_player1), node.children))
else:
return min(map(lambda som_node: minimax(som_node, not is_player1), node.children))
if __name__ == '__main__':
print(f'return is {minimax(create_minimax_game(),True)}')