如何处理非常大的Bitboard

时间:2015-05-19 07:38:34

标签: python c++ bitboard

我正在进行2人桌面游戏(例如 connect 4 ),参数电路板尺寸 h w 。我想使用 hw 大小的位板检查获胜条件。

在国际象棋这样的游戏中,棋盘大小是固定的,位板通常用某种64位整数表示。当 h w 不恒定且可能非常大(假设30 * 30)时,位板是个好主意吗?如果是这样,C / C ++中的任何数据类型是否都能处理大型位板以保持其性能?

由于我目前正在使用python,因此也非常感谢这种语言的解决方案! :)

提前致谢

1 个答案:

答案 0 :(得分:1)

我之前写过这段代码只是为了玩游戏概念。没有涉及的情报行为。只是随机移动来演示游戏。我想这对你来说并不重要,因为你只是在寻找快速检查获胜条件。这个实现很快,因为我尽力避免for循环,只使用内置的python / numpy函数(有一些技巧)。

import numpy as np

row_size = 6
col_size = 7
symbols = {1:'A', -1:'B', 0:' '}

def was_winning_move(S, P, current_row_idx,current_col_idx):
    #****** Column Win ******
    current_col = S[:,current_col_idx]
    P_idx= np.where(current_col== P)[0]
    #if the difference between indexes are one, that means they are consecutive.
    #we need at least 4 consecutive index. So 3 Ture value
    is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
    if is_idx_consecutive:
        return True

    #****** Column Win ****** 
    current_row = S[current_row_idx,:]
    P_idx= np.where(current_row== P)[0]
    is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
    if is_idx_consecutive:
        return True

    #****** Diag Win ****** 
    offeset_from_diag = current_col_idx - current_row_idx
    current_diag = S.diagonal(offeset_from_diag)
    P_idx= np.where(current_diag== P)[0]
    is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
    if is_idx_consecutive:
        return True
    #****** off-Diag Win ****** 
    #here 1) reverse rows, 2)find new index, 3)find offest and proceed as diag
    reversed_rows = S[::-1,:] #1
    new_row_idx = row_size - 1 - current_row_idx #2
    offeset_from_diag = current_col_idx - new_row_idx #3
    current_off_diag = reversed_rows.diagonal(offeset_from_diag)
    P_idx= np.where(current_off_diag== P)[0]
    is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
    if is_idx_consecutive:
        return True
    return False

def move_at_random(S,P):
    selected_col_idx = np.random.permutation(range(col_size))[0]
    #print selected_col_idx
    #we should fill in matrix from bottom to top. So find the last filled row in col and fill the upper row
    last_filled_row = np.where(S[:,selected_col_idx] != 0)[0]
    #it is possible that there is no filled array. like the begining of the game
    #in this case we start with last row e.g row : -1
    if last_filled_row.size != 0:
        current_row_idx = last_filled_row[0] - 1
    else:
        current_row_idx = -1
    #print 'col[{0}], row[{1}]'.format(selected_col,current_row)
    S[current_row_idx, selected_col_idx] = P
    return (S,current_row_idx,selected_col_idx)

def move_still_possible(S):
    return not (S[S==0].size == 0)

def print_game_state(S):
    B = np.copy(S).astype(object)
    for n in [-1, 0, 1]:
        B[B==n] = symbols[n]
    print B

def play_game():
    #initiate game state
    game_state = np.zeros((6,7),dtype=int)
    player = 1
    mvcntr = 1
    no_winner_yet = True
    while no_winner_yet and  move_still_possible(game_state):
        #get player symbol
        name = symbols[player]

        game_state, current_row, current_col = move_at_random(game_state, player)
        #print '******',player,(current_row, current_col)
        #print current game state
        print_game_state(game_state)
        #check if the move was a winning move
        if was_winning_move(game_state,player,current_row, current_col):
            print 'player %s wins after %d moves' % (name, mvcntr)
            no_winner_yet = False

        # switch player and increase move counter
        player *= -1
        mvcntr +=  1
    if no_winner_yet:
        print 'game ended in a draw'
        player = 0
    return game_state,player,mvcntr

if __name__ == '__main__':
    S, P, mvcntr =  play_game()

如果您有任何疑问,请与我联系

更新:说明:

在每次移动时,查看通过当前单元格的列,行,对角线和辅助对角线,并找到具有当前符号的连续单元格。避免扫描整个电路板。

enter image description here

在每个方向上提取细胞:

柱:

current_col = S[:,current_col_idx]

行:

current_row = S[current_row_idx,:]

对角线: 从中找到所需对角线的偏移量 主对角线:

diag_offset = current_col_idx - current_row_idx
current_diag = S.diagonal(offset)

非对角线:

反转矩阵的行:

S_reversed_rows = S[::-1,:]

在新矩阵中找到行索引

new_row_idx = row_size - 1 - current_row_idx
current_offdiag = S.diagonal(offset)

enter image description here