def move(board, col): global move_start_time move_start_time = timer() head = bitboard.board_to_pos(board, col) m_bb, o_bb = bitboard.board_to_bbs(board, col) #threats = {} # analyze board for threats #m_rotations = bitboard.get_rotations(m_bb) #o_rotations = bitboard.get_rotations(o_bb) #for r in range(8): # prow = bitboard.get_prow(m_bb, o_bb, r, True) # if prow in win_prows: # winning_i = bitboard.rowi_to_i(win_prows[prow], r, 0) # return bitboard.i_to_yx(winning_i) alpha = -WIN_SCORE beta = WIN_SCORE depth = 4 minimax(head, depth, alpha, beta, True) best_pos = get_children(head, True, 1)[0] move_bib = bitboard.pos_to_c_bb(head ^ best_pos) move_y, move_x = bitboard.move_to_yx(move_bib) if (board[move_y][move_x] != ' '): move_y, move_x = make_forced_move(board) print("threat:", timer() - move_start_time) return move_y, move_x
def move(board, col, **kwargs): global s global first global move_start_time global exits global offtime global offtime_pos, offtime_depth offtime = False move_start_time = timer() head = bb.board_to_pos(board, col) depth = START_DEPTH children = get_children(head, True)[0:10] best_poses = {depth: children[0]} #print(get_cur_time()) while get_cur_time() < move_stop_time and depth < MAX_DEPTH: best = -WIN_SCORE alpha = -WIN_SCORE beta = WIN_SCORE #alphabeta(head, depth, alpha, beta, True) for n_pos in children: val = alphabeta(n_pos, depth - 1, alpha, beta, False, lambda: get_cur_time() > move_stop_time) mmx_scores[n_pos] = val if val > best: best = val best_poses[depth] = n_pos best = val if val > best else best alpha = best if best > alpha else alpha if alpha >= beta: break if get_cur_time() < move_stop_time and depth in best_poses: depth += 1 childrens[head].sort(key=lambda c: scores[c] if c not in mmx_scores else mmx_scores[c], reverse=True) #alphabeta(head, depth, alpha, beta, True) #best_pos = max(childrens[head], key=lambda c: scores[c] if c not in mmx_scores else 100*mmx_scores[c]) best_pos = best_poses[ depth - 1] if depth > START_DEPTH else best_poses[START_DEPTH] #best_pos = max(children, key=lambda c: scores[c]) move_y, move_x = bb.move_to_yx(bb.find_move(head, best_pos)) if (board[move_y][move_x] != ' '): move_y, move_x = make_forced_move(board) else: offtime = True offtime_pos = best_pos offtime_depth = depth if kwargs.get('time', False): print("pdab:", "d:", depth - 1, "\tt:", get_cur_time(), len(scores), len(childrens)) return move_y, move_x
s += str(i % 10) for j in range(len(board[0]) - 1): s += str(board[i][j]) + "|" s += str(board[i][len(board[0]) - 1]) s += "*\n" s += (len(board[0]) * 2 + 1) * "*" print(s) if __name__ == '__main__': board = [] for i in range(8): board.append([" "] * 8) import random for i in range(10): board[random.randrange(0, 8)][random.randrange(0, 8)] = 'w' board[random.randrange(0, 8)][random.randrange(0, 8)] = 'b' board = bb.pos_to_board(0x2500860002000003000d201000860300) print_board(board) #m_bb, o_bb = bb.board_to_bbs(board, 'w') print(hex(bb.board_to_pos(board, 'w'))) #bb.print_bb(bb.get_relevant(bb.board_to_pos(board, 'w'))) #bb.print_bb(-0x303030303030304) #bb.score_bb(m_bb, o_bb) import cProfile #print(move(board, 'w')) cProfile.runctx("print(move(board, 'w'))", globals(), locals())