Top

tictactoe.hash.move module

mport weakref
rom tictactoe.hash.cell import Cell, create_and_verify_cell
rom tictactoe.settings  import TTT_3_IN_A_ROW, TTT_4_IN_A_ROW, TTT_5_IN_A_ROW
lass Move(Cell):
   _Refs   = weakref.WeakValueDictionary()
   _Hashes = weakref.WeakValueDictionary()
   MODE    = TTT_3_IN_A_ROW
   def __new__(cls,number,player):
       return create_and_verify_cell(number=number,
                                     player=player,
                                     cell_class=cls)
   @classmethod
   def from_hash(cls,hash):
       previous_move = cls._Hashes.get(hash,None)
       return previous_move if previous_move else super(Move,cls).from_hash(hash=hash)
lass Move4(Cell):
   _Refs   = weakref.WeakValueDictionary()
   _Hashes = weakref.WeakValueDictionary()
   MODE    = TTT_4_IN_A_ROW
   def __new__(cls,number,player):
       return create_and_verify_cell(number=number,
                                     player=player,
                                     cell_class=cls)
   @classmethod
   def from_hash(cls,hash):
       previous_move = cls._Hashes.get(hash,None)
       return previous_move if previous_move else super(Move4,cls).from_hash(hash=hash)
lass Move5(Cell):
   _Refs   = weakref.WeakValueDictionary()
   _Hashes = weakref.WeakValueDictionary()
   MODE    = TTT_5_IN_A_ROW
   def __new__(cls,number,player):
       return create_and_verify_cell(number=number,
                                     player=player,
                                     cell_class=cls)
   @classmethod
   def from_hash(cls,hash):
       previous_move = cls._Hashes.get(hash,None)
       return previous_move if previous_move else super(Move5,cls).from_hash(hash=hash)

Module variables

var TTT_3_IN_A_ROW

var TTT_4_IN_A_ROW

var TTT_5_IN_A_ROW

Classes

class Move

class Move(Cell):

    _Refs   = weakref.WeakValueDictionary()
    _Hashes = weakref.WeakValueDictionary()

    MODE    = TTT_3_IN_A_ROW

    def __new__(cls,number,player):
        return create_and_verify_cell(number=number,
                                      player=player,
                                      cell_class=cls)

    @classmethod
    def from_hash(cls,hash):
        previous_move = cls._Hashes.get(hash,None)
        return previous_move if previous_move else super(Move,cls).from_hash(hash=hash)

Ancestors (in MRO)

  • Move
  • tictactoe.hash.cell.Cell
  • tictactoe.hash.Hashable
  • __builtin__.object

Class variables

var MODE

Static methods

def decompose_binary(

index)

@staticmethod
def decompose_binary(index):
    return ((index / 3) + 1, index % 3)

def validate_binary(

binary)

@staticmethod
def validate_binary(binary):
    if binary.count('1') != 1:
        raise TicTacToeException(
            'Binary:{} is an invalid Cell. Only a single '\
            '1 can be found in the binary'.format(binary))

Instance variables

var binary

Returns the binary representation of the current Cell instance. However, it is important to notice that the binary representation is in Big Endian mode.

var hash

Returns the an int or a long representing hash of the current Cell instance. This is the same value that will be returned when the object is hashed through the python native hash() method.

var number

This property represents the current Cell number of the Tic Tac Toe game. So, a 3x3 Tic Tac Toe game in a 2d game will have at most 9 cells, and the possible Cell numbers allowed are anything between 1-9.

var player

Player's number.This can be either FREE_SPACE(0) for basically free space, PLAYER_1(1) or PLAYER_2(2).

Methods

def from_binary(

cls, binary)

This returns a new Cell instance by using a binary representation either through a string representing the a cell in 0's and 1's, or using a list/tuple with only valid items being again 0's and 1's.

The binary representation will be the size of the Grid.In other words if you wanted to represent Player 1's mark on cell number 2 in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple like this :

 binary = '000000000000000000000010000'
 binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
 binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']

 cell = Cell.from_binary(binary)
@classmethod
def from_binary(cls,binary):
    """
        This returns a new `Cell` instance by using a binary representation
        either through a *string* representing the a cell in 0's and 1's, or
        using a list/tuple with only valid items being again 0's and 1's.
        The binary representation will be the size of the `Grid`.In other words
        if you wanted to represent **Player 1**'s mark on cell number 2
        in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple
        like this :
             binary = '000000000000000000000010000'
             binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
             binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']
             cell = Cell.from_binary(binary)
    """
    verify_binary(binary=binary,mode=GAME_MODES[cls.MODE]['GRID'])
    if isinstance(binary,(list,tuple)):
        binary = "".join([str(n) for n in binary])
    binary = "".join([b for b in reversed(binary)])
    cls.validate_binary(binary=binary)
    cell, player = cls.decompose_binary(index=binary.index('1'))
    return cls(number=cell,player=player)

def from_hash(

cls, hash)

@classmethod
def from_hash(cls,hash):
    previous_move = cls._Hashes.get(hash,None)
    return previous_move if previous_move else super(Move,cls).from_hash(hash=hash)

class Move4

class Move4(Cell):

    _Refs   = weakref.WeakValueDictionary()
    _Hashes = weakref.WeakValueDictionary()

    MODE    = TTT_4_IN_A_ROW

    def __new__(cls,number,player):
        return create_and_verify_cell(number=number,
                                      player=player,
                                      cell_class=cls)

    @classmethod
    def from_hash(cls,hash):
        previous_move = cls._Hashes.get(hash,None)
        return previous_move if previous_move else super(Move4,cls).from_hash(hash=hash)

Ancestors (in MRO)

  • Move4
  • tictactoe.hash.cell.Cell
  • tictactoe.hash.Hashable
  • __builtin__.object

Class variables

var MODE

Static methods

def decompose_binary(

index)

@staticmethod
def decompose_binary(index):
    return ((index / 3) + 1, index % 3)

def validate_binary(

binary)

@staticmethod
def validate_binary(binary):
    if binary.count('1') != 1:
        raise TicTacToeException(
            'Binary:{} is an invalid Cell. Only a single '\
            '1 can be found in the binary'.format(binary))

Instance variables

var binary

Returns the binary representation of the current Cell instance. However, it is important to notice that the binary representation is in Big Endian mode.

var hash

Returns the an int or a long representing hash of the current Cell instance. This is the same value that will be returned when the object is hashed through the python native hash() method.

var number

This property represents the current Cell number of the Tic Tac Toe game. So, a 3x3 Tic Tac Toe game in a 2d game will have at most 9 cells, and the possible Cell numbers allowed are anything between 1-9.

var player

Player's number.This can be either FREE_SPACE(0) for basically free space, PLAYER_1(1) or PLAYER_2(2).

Methods

def from_binary(

cls, binary)

This returns a new Cell instance by using a binary representation either through a string representing the a cell in 0's and 1's, or using a list/tuple with only valid items being again 0's and 1's.

The binary representation will be the size of the Grid.In other words if you wanted to represent Player 1's mark on cell number 2 in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple like this :

 binary = '000000000000000000000010000'
 binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
 binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']

 cell = Cell.from_binary(binary)
@classmethod
def from_binary(cls,binary):
    """
        This returns a new `Cell` instance by using a binary representation
        either through a *string* representing the a cell in 0's and 1's, or
        using a list/tuple with only valid items being again 0's and 1's.
        The binary representation will be the size of the `Grid`.In other words
        if you wanted to represent **Player 1**'s mark on cell number 2
        in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple
        like this :
             binary = '000000000000000000000010000'
             binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
             binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']
             cell = Cell.from_binary(binary)
    """
    verify_binary(binary=binary,mode=GAME_MODES[cls.MODE]['GRID'])
    if isinstance(binary,(list,tuple)):
        binary = "".join([str(n) for n in binary])
    binary = "".join([b for b in reversed(binary)])
    cls.validate_binary(binary=binary)
    cell, player = cls.decompose_binary(index=binary.index('1'))
    return cls(number=cell,player=player)

def from_hash(

cls, hash)

@classmethod
def from_hash(cls,hash):
    previous_move = cls._Hashes.get(hash,None)
    return previous_move if previous_move else super(Move4,cls).from_hash(hash=hash)

class Move5

class Move5(Cell):

    _Refs   = weakref.WeakValueDictionary()
    _Hashes = weakref.WeakValueDictionary()

    MODE    = TTT_5_IN_A_ROW

    def __new__(cls,number,player):
        return create_and_verify_cell(number=number,
                                      player=player,
                                      cell_class=cls)

    @classmethod
    def from_hash(cls,hash):
        previous_move = cls._Hashes.get(hash,None)
        return previous_move if previous_move else super(Move5,cls).from_hash(hash=hash)

Ancestors (in MRO)

  • Move5
  • tictactoe.hash.cell.Cell
  • tictactoe.hash.Hashable
  • __builtin__.object

Class variables

var MODE

Static methods

def decompose_binary(

index)

@staticmethod
def decompose_binary(index):
    return ((index / 3) + 1, index % 3)

def validate_binary(

binary)

@staticmethod
def validate_binary(binary):
    if binary.count('1') != 1:
        raise TicTacToeException(
            'Binary:{} is an invalid Cell. Only a single '\
            '1 can be found in the binary'.format(binary))

Instance variables

var binary

Returns the binary representation of the current Cell instance. However, it is important to notice that the binary representation is in Big Endian mode.

var hash

Returns the an int or a long representing hash of the current Cell instance. This is the same value that will be returned when the object is hashed through the python native hash() method.

var number

This property represents the current Cell number of the Tic Tac Toe game. So, a 3x3 Tic Tac Toe game in a 2d game will have at most 9 cells, and the possible Cell numbers allowed are anything between 1-9.

var player

Player's number.This can be either FREE_SPACE(0) for basically free space, PLAYER_1(1) or PLAYER_2(2).

Methods

def from_binary(

cls, binary)

This returns a new Cell instance by using a binary representation either through a string representing the a cell in 0's and 1's, or using a list/tuple with only valid items being again 0's and 1's.

The binary representation will be the size of the Grid.In other words if you wanted to represent Player 1's mark on cell number 2 in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple like this :

 binary = '000000000000000000000010000'
 binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
 binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']

 cell = Cell.from_binary(binary)
@classmethod
def from_binary(cls,binary):
    """
        This returns a new `Cell` instance by using a binary representation
        either through a *string* representing the a cell in 0's and 1's, or
        using a list/tuple with only valid items being again 0's and 1's.
        The binary representation will be the size of the `Grid`.In other words
        if you wanted to represent **Player 1**'s mark on cell number 2
        in a 3x3(2d) Tic Tac Toe game, then you will need a str or list/tuple
        like this :
             binary = '000000000000000000000010000'
             binary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
             binary = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0']
             cell = Cell.from_binary(binary)
    """
    verify_binary(binary=binary,mode=GAME_MODES[cls.MODE]['GRID'])
    if isinstance(binary,(list,tuple)):
        binary = "".join([str(n) for n in binary])
    binary = "".join([b for b in reversed(binary)])
    cls.validate_binary(binary=binary)
    cell, player = cls.decompose_binary(index=binary.index('1'))
    return cls(number=cell,player=player)

def from_hash(

cls, hash)

@classmethod
def from_hash(cls,hash):
    previous_move = cls._Hashes.get(hash,None)
    return previous_move if previous_move else super(Move5,cls).from_hash(hash=hash)