Skip to content

Instantly share code, notes, and snippets.

@tiwo
Last active April 25, 2017 09:00
Show Gist options
  • Save tiwo/b25fcc9cf81ac694ad99458416a2b5e6 to your computer and use it in GitHub Desktop.
Save tiwo/b25fcc9cf81ac694ad99458416a2b5e6 to your computer and use it in GitHub Desktop.
complete α-β evaluation of simple abstract games

written in notepad.exe and completely untested - Python has never seen this code :-)

# -*- coding: utf-8 -*-
class Game(object):
@property
def initial_position(self):
# not actually used
raise NotImplementedError
def terminal_value(self, position):
raise NotImplementedError
def alphabeta(self, position, max_player, α=-math.inf, β=+math.inf):
if self.terminal_value(position) is not None:
return self.terminal(position)
if max_player:
value = -math.inf
pick, value_seen, criterion = (max, -math.inf, operator.ge) \
if max_player else \
(min, +math.inf, operator.le)
for successor in self.generate_successors(position):
successor_value = self.alphabeta(successor, not max_player, α, β)
value_seen = pick(value_seen, successor_value)
α = pick(α, value)
if α >=β:
break
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment