Exemple #1
0
def create_player_hash_table(mode=TTT_3_IN_A_ROW):
    verify_game_mode(game_mode=mode)

    cell_klass = {
        TTT_3_IN_A_ROW : Cell,
        TTT_4_IN_A_ROW : Cell4,
        TTT_5_IN_A_ROW : Cell5
    }

    players = (FREE_SPACE, PLAYER_1, PLAYER_2)
    player_hashes = lambda players,m : [compute_all_hash_moves(player=p,mode=m) for p in players]

    hashes = list(chain.from_iterable(player_hashes(players,mode)))

    player_hash_table = dict([(h,cell_klass[mode].from_hash(hash=h)) for h in hashes])

    return HashTable(table=player_hash_table)
Exemple #2
0
def decompose_grid_hash(hash,mode=TTT_3_IN_A_ROW):

    verify_game_mode(game_mode=mode)
    m = GAME_MODES[mode]['GRID']
    verify_hash(hash=hash,mode=GAME_MODES[mode]['GRID'])
    length = MODES[m]['length']
    binary = bin(hash)[2:].zfill(length)
    binary_cells = [c for c in
                        reversed(
                                    [binary[bin_range:bin_range+3]
                                    for bin_range in xrange(0, len(binary), 3)]
                                )
                    ]

    bits_to_push = lambda c : ((c+1) * 3) - 3

    return [ (1 << ((int(b, 2) >> 1) + bits_to_push(c=cell)))
            for cell, b in enumerate(binary_cells) ]
    def __init__(self,game_mode=TTT_3_IN_A_ROW):

        verify_game_mode(game_mode=game_mode)
        self._mode = GAME_MODES[game_mode]['LINE']
        self._length  = MODES[self.mode]['length']
Exemple #4
0
def compute_all_hash_moves(player,mode=TTT_3_IN_A_ROW):
    verify_game_mode(game_mode=mode)
    key = lambda m : GAME_MODES[m]['GRID_STATE']
    length = MODES[key(m=mode)]['length'] + 1
    return [compute_hash(cell=c, player=player,mode=mode) for c in xrange(1, length, 1)]