Find file
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (46 sloc) 1.74 KB
#! /bin/env python
# -*- coding: utf-8 -*-
'''A helper module for generating the map the genome of a Critter represents.'''
import pickle
class StateMap(object):
'''A data structure mapping board states onto indices, and vice-versa.'''
def __init__(self):
'''Creates a new, empty statemap'''
self.index = {} = {}
def add(self, idx, game):
'''Adds a two-way relation between a board state and an index.'''
self.index[game] = idx [idx ] = game
def copy(self, other):
'''Copies the values of another statemap.'''
self.index = other.index.copy() =
def __len__(self):
'''Returns the number of mapped states.'''
return len(
statemap = StateMap()
try: # get precomputed statemap
with open('tictacstates.dat','rb') as f:
except IOError: # none exists, make one
from tictacboard import Board
print "Generating state data..."
states = set()
board = Board()
def search_states(brd):
'''Searches for unique, non-decided states to map to indices.'''
if brd.game_state() != Board.unresolved: return
for trn in Board.transforms:
if brd.transform(trn).squares in states: return
if len(brd.potential_winners()) == 1: return
for sq in brd.legal_moves():
for i, state in enumerate(states):
statemap.add(i, state)
print "%i states found"% len(statemap)
# save the statemap to disk is serialized form
with open('tictacstates.dat','wb') as f:
pickle.dump(statemap, f)