Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Minusmax solution in Clear category for Xs and Os Champion by veky
from functools import lru_cache as cache
wins = {0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}
other = {'O':'X', 'X':'O'}
def play(pos, player, where):
mold = list(pos)
mold[where] = player
return ''.join(mold)
@cache(None)
def value(pos, player):
their = set(i for i, char in enumerate(pos) if char == other[player])
if any(combination <= their for combination in wins): return -1
else: return max(options(pos, player).values(), default=0)
def options(pos, player):
return {move: -value(play(pos, player, move), other[player])
for move, char in enumerate(pos) if char == '.'}
def x_and_o(table, player):
for move, move_value in options(''.join(table), player).items():
if move_value == value(''.join(table), player): return divmod(move, 3)
June 27, 2018