-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimax.py
63 lines (46 loc) · 1.59 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf8 -*-
__author__ = 'Suilan Estévez Velarde'
from tools import oo
from game_logic import *
def minimax(game, player, depth, h, moves):
"""Retorna el mejor tablero para el jugador
correspondiente
"""
best, value = maxplay(game, None, player, depth, h, moves)
return best
def maxplay(game, play, player, depth, h, moves, alpha=-oo , beta=oo):
"""Retorna la mejor jugada tablero para el jugador"""
best = None
best_value = -oo
if game.winner() != EMPTY:
return play, -1
#return play, 1 if game.winner() == player else -1
if not depth:
return play, h(game, player)
for x,y in moves(game, player):
b, value = minplay(game.clone_play(x,y), (x,y), player, depth - 1, h, moves)
alpha = max(alpha, value)
if alpha >= beta:
break
if value > best_value:
best = (x,y)
best_value = value
return best, best_value
def minplay(game, play, player, depth, h, moves, alpha=-oo , beta=oo):
"""Retorna la mejor jugada para el jugador contrario"""
best = None
best_value = oo
if game.winner() != EMPTY:
return play, 1
#return play, 1 if game.winner() == player else -1
if not depth:
return play, h(game, player)
for x,y in moves(game, player):
_, value = maxplay(game.clone_play(x,y), (x,y), player, depth - 1, h, moves)
beta = min(beta, value)
if beta <= alpha:
break
if value < best_value:
best = (x,y)
best_value = value
return best, best_value