Top

tictactoe.hash.state module

rom funcy import  memoize
rom tictactoe.hash         import Hashable
rom tictactoe.settings     import GAME_MODES, TTT_3_IN_A_ROW, TTT_4_IN_A_ROW, TTT_5_IN_A_ROW
rom tictactoe.verification import verify_binary, verify_hash
lass GridState(Hashable):
   MODE = TTT_3_IN_A_ROW
   def __init__(self,hash=0):
       verify_hash(hash=hash,mode=GAME_MODES[self.MODE]['GRID_STATE'])
       self._hash = hash
       self._binary = None
   @classmethod
   def from_binary(cls,binary):
       verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID_STATE'])
       return cls(hash=int(binary, 2))
   @property
   def hash(self):
       return self._hash
   @hash.setter
   def hash(self,value):
       pass
   @property
   def binary(self):
       if self._binary is None:
           self._binary = bin(self.hash)[2:].zfill(9)
       return self._binary
   @binary.setter
   def binary(self,value):
       pass
   @property
   def free(self):
       return self.binary.count('0')
   @property
   def taken(self):
       return self.binary.count('1')
   @memoize
   def get_taken_cells(self):
      return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '1']
   @memoize
   def get_free_cells(self):
      return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '0']
lass GridState4(GridState):
   MODE = TTT_4_IN_A_ROW
lass GridState5(GridState):
   MODE = TTT_5_IN_A_ROW

Module variables

var GAME_MODES

var TTT_3_IN_A_ROW

var TTT_4_IN_A_ROW

var TTT_5_IN_A_ROW

Classes

class GridState

class GridState(Hashable):

    MODE = TTT_3_IN_A_ROW

    def __init__(self,hash=0):

        verify_hash(hash=hash,mode=GAME_MODES[self.MODE]['GRID_STATE'])
        self._hash = hash
        self._binary = None

    @classmethod
    def from_binary(cls,binary):

        verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID_STATE'])
        return cls(hash=int(binary, 2))

    @property
    def hash(self):
        return self._hash

    @hash.setter
    def hash(self,value):
        pass

    @property
    def binary(self):
        if self._binary is None:
            self._binary = bin(self.hash)[2:].zfill(9)

        return self._binary

    @binary.setter
    def binary(self,value):
        pass

    @property
    def free(self):
        return self.binary.count('0')

    @property
    def taken(self):
        return self.binary.count('1')

    @memoize
    def get_taken_cells(self):
       return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '1']

    @memoize
    def get_free_cells(self):
       return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '0']

Ancestors (in MRO)

  • GridState
  • tictactoe.hash.Hashable
  • __builtin__.object

Class variables

var MODE

Instance variables

var binary

var free

var hash

var taken

Methods

def __init__(

self, hash=0)

def __init__(self,hash=0):
    verify_hash(hash=hash,mode=GAME_MODES[self.MODE]['GRID_STATE'])
    self._hash = hash
    self._binary = None

def from_binary(

cls, binary)

@classmethod
def from_binary(cls,binary):
    verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID_STATE'])
    return cls(hash=int(binary, 2))

def from_hash(

cls, hash)

Alternative method for constructing a Hashable instance. All Hashable instances must have a method for constructing an instance from a hash. Either the __init__ method or the from_hash method must implement this needed feature.

@classmethod
@abc.abstractmethod
def from_hash(cls,hash):
    """
        Alternative method for constructing a `Hashable` instance. All `Hashable`
        instances must have a method for constructing an instance from a *hash*.
        Either the **__init__** method or the **from_hash** method must implement
        this needed feature.
    """

def get_free_cells(

*args, **kwargs)

@memoize
def get_free_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '0']

def get_taken_cells(

*args, **kwargs)

@memoize
def get_taken_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '1']

class GridState4

class GridState4(GridState):

    MODE = TTT_4_IN_A_ROW

Ancestors (in MRO)

Class variables

var MODE

Inheritance: GridState.MODE

Instance variables

var binary

Inheritance: GridState.binary

var free

Inheritance: GridState.free

var hash

Inheritance: GridState.hash

var taken

Inheritance: GridState.taken

Methods

def __init__(

self, hash=0)

Inheritance: GridState.__init__

def __init__(self,hash=0):
    verify_hash(hash=hash,mode=GAME_MODES[self.MODE]['GRID_STATE'])
    self._hash = hash
    self._binary = None

def from_binary(

cls, binary)

Inheritance: GridState.from_binary

@classmethod
def from_binary(cls,binary):
    verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID_STATE'])
    return cls(hash=int(binary, 2))

def from_hash(

cls, hash)

Inheritance: GridState.from_hash

Alternative method for constructing a Hashable instance. All Hashable instances must have a method for constructing an instance from a hash. Either the __init__ method or the from_hash method must implement this needed feature.

@classmethod
@abc.abstractmethod
def from_hash(cls,hash):
    """
        Alternative method for constructing a `Hashable` instance. All `Hashable`
        instances must have a method for constructing an instance from a *hash*.
        Either the **__init__** method or the **from_hash** method must implement
        this needed feature.
    """

def get_free_cells(

*args, **kwargs)

Inheritance: GridState.get_free_cells

@memoize
def get_free_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '0']

def get_taken_cells(

*args, **kwargs)

Inheritance: GridState.get_taken_cells

@memoize
def get_taken_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '1']

class GridState5

class GridState5(GridState):

    MODE = TTT_5_IN_A_ROW

Ancestors (in MRO)

Class variables

var MODE

Inheritance: GridState.MODE

Instance variables

var binary

Inheritance: GridState.binary

var free

Inheritance: GridState.free

var hash

Inheritance: GridState.hash

var taken

Inheritance: GridState.taken

Methods

def __init__(

self, hash=0)

Inheritance: GridState.__init__

def __init__(self,hash=0):
    verify_hash(hash=hash,mode=GAME_MODES[self.MODE]['GRID_STATE'])
    self._hash = hash
    self._binary = None

def from_binary(

cls, binary)

Inheritance: GridState.from_binary

@classmethod
def from_binary(cls,binary):
    verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID_STATE'])
    return cls(hash=int(binary, 2))

def from_hash(

cls, hash)

Inheritance: GridState.from_hash

Alternative method for constructing a Hashable instance. All Hashable instances must have a method for constructing an instance from a hash. Either the __init__ method or the from_hash method must implement this needed feature.

@classmethod
@abc.abstractmethod
def from_hash(cls,hash):
    """
        Alternative method for constructing a `Hashable` instance. All `Hashable`
        instances must have a method for constructing an instance from a *hash*.
        Either the **__init__** method or the **from_hash** method must implement
        this needed feature.
    """

def get_free_cells(

*args, **kwargs)

Inheritance: GridState.get_free_cells

@memoize
def get_free_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '0']

def get_taken_cells(

*args, **kwargs)

Inheritance: GridState.get_taken_cells

@memoize
def get_taken_cells(self):
   return [i+1 for i,b in enumerate(reversed(self.binary)) if b == '1']