-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimaxAlgorithm.py
More file actions
45 lines (38 loc) · 1.28 KB
/
MinimaxAlgorithm.py
File metadata and controls
45 lines (38 loc) · 1.28 KB
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
from config import EMPTYCELL
from config import BLACKCELL
from config import WHITECELL
from state import State
from MovesGenerator import MovesGenerator
from StateEvaluator import is_gameover
from StateEvaluator import evaluate
obj = MovesGenerator()
limit = 5
def minimax(state):
global limit
states = obj.getNextStates(state)
if(states):
return max(map(lambda state: [state, min_play(state, limit, state.whoDidThis)], states), key=lambda x: x[1])
else:
return (state, -1)
def min_play(state, limit, oppColor=BLACKCELL):
limit -= 1
if is_gameover(state) or limit <= 0:
state_value = evaluate(state, oppColor)
return state_value
states = obj.getNextStates(state)
if (states):
return min(map(lambda state: max_play(state, limit, state.whoDidThis), states))
else:
state.reverse_State()
return max_play(state, limit)
def max_play(state, limit, oppColor=BLACKCELL):
limit -= 1
if is_gameover(state) or limit <= 0:
state_value = evaluate(state, oppColor)
return state_value
states = obj.getNextStates(state)
if (states):
return max(map(lambda state: min_play(state, limit, state.whoDidThis), states))
else:
state.reverse_State()
return min_play(state, limit)