示例#1
0
    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)
示例#2
0
 def from_binary(cls,binary):
     verify_binary(binary,mode=GAME_MODES[cls.MODE]['GRID'])
     return cls(hash=int(binary, 2))