def expand(pos, mtm): my_bib, other_bib = gbib.pos_to_bibs(pos) if gbib.is_tie(my_bib | other_bib) or gbib.is_win(my_bib) or gbib.is_win( other_bib): return pos, mtm else: if pos not in unvisited_children and pos not in visited_children: # if i have not created children for this position if mtm: children = [ pos | (0x1 << m + 64) for m in gbib.likely_moves(my_bib | other_bib) ] else: children = [ pos | (0x1 << m) for m in gbib.likely_moves(my_bib | other_bib) ] unvisited_children[pos] = children for child in children: if child in parentss: parentss[child].append(pos) else: parentss[child] = [pos] else: children = unvisited_children[pos] return max(children, key=lambda c: score(c, mtm) ), not mtm # if we choose the best one to expand on, profit
def get_children( pos, mtm): # mtm is true if the children will be the result of my move if pos in childrens: return childrens[pos] m_bib, o_bib = gbib.pos_to_bibs(pos) childrens[pos] = gbib.make_children(m_bib, o_bib, mtm, scores[pos], scores) return childrens[pos]
def rollout(pos, mtm): # return True for win or tie my_bib, other_bib = gbib.pos_to_bibs(pos) while not gbib.is_tie(my_bib | other_bib): #print_board(gbib.bib_to_board(my_bib, other_bib)) if mtm: if gbib.is_win(other_bib): return False else: move = random_move(my_bib | other_bib) my_bib = my_bib | 0x1 << move else: if gbib.is_win(my_bib): return True else: move = random_move(my_bib | other_bib) other_bib = other_bib | 0x1 << move mtm = not mtm return False
if (board[move_y][move_x] != ' '): move_y, move_x = make_forced_move(board) print("dab:", "s:", s, "\tt:", timer() - start, (timer() - start) / s, len(scores), len(childrens)) return move_y, move_x if __name__ == '__main__': board = gbib.make_empty_board(8) board[1][1] = 'w' board[2][2] = 'w' board[3][3] = 'w' board[4][4] = 'w' board[5][5] = 'b' gbib.print_board(board) m_bib, o_bib = gbib.make_bitboards(board, 'w') print(gbib.bibs_to_pos(m_bib, o_bib)) head = gbib.bibs_to_pos(m_bib, o_bib) from timeit import default_timer as timer start = timer() move_y, move_x = move(board, 'w') print((timer() - start)) print(move_y, move_x) for child in childrens[head]: m_bib, o_bib = gbib.pos_to_bibs(child) gbib.print_board(gbib.pos_to_board(child)) print("Score:", scores[child])