Esempio n. 1
0
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
Esempio n. 2
0
def alphabeta(pos, depth, alpha, beta, me):
    global s
    #print(hex(pos), depth, alpha, beta, me, end = "\r")
    if depth == 0 or scores[pos] == WIN_SCORE or scores[
            pos] == -WIN_SCORE or gbib.is_tie(pos):
        s += 1
        return scores[pos]

    if me:
        best = -WIN_SCORE
        children = get_children(pos, True)[0:10]
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, False)
            mmx_scores[n_pos] = val
            best = val if val > best else best
            alpha = best if best > alpha else alpha
            if alpha >= beta:
                childrens[pos].sort(
                    key=lambda c: scores[c]
                    if c not in mmx_scores else 100 * mmx_scores[c],
                    reverse=me)
                return best
        childrens[pos].sort(key=lambda c: scores[c]
                            if c not in mmx_scores else 100 * mmx_scores[c],
                            reverse=me)
        return best
    else:
        best = WIN_SCORE
        children = get_children(pos, False)[0:10]
        #print(scores[children[0]], "vs", scores[children[-1]])
        #if WIN_SCORE >= scores[children[0]] >= WIN_SCORE - 10000 or -WIN_SCORE <= scores[children[0]] <= -WIN_SCORE + 10000:
        #    gbib.print_board(gbib.pos_to_board(pos))
        #print("Score:", scores[pos])
        #for child in children:
        #    gbib.print_board(gbib.pos_to_board(pos))
        #    print("Score:", scores[pos])
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, True)
            mmx_scores[n_pos] = val
            best = val if val < best else best
            beta = best if best < beta else beta
            if beta <= alpha:
                childrens[pos].sort(
                    key=lambda c: scores[c]
                    if c not in mmx_scores else 100 * mmx_scores[c],
                    reverse=me)
                return best
        childrens[pos].sort(key=lambda c: scores[c]
                            if c not in mmx_scores else 100 * mmx_scores[c],
                            reverse=me)
        return best
Esempio n. 3
0
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
Esempio n. 4
0
def quick_alphabeta(pos, depth, alpha, beta, me):
    if depth == 0 or pos not in childrens or scores[
            pos] == WIN_SCORE or scores[pos] == -WIN_SCORE or gbib.is_tie(pos):
        return scores[pos]

    if me:
        best = -WIN_SCORE
        for n_pos in childrens[pos]:
            val = quick_alphabeta(n_pos, depth - 1, alpha, beta, False)
            best = val if val > best else best
            alpha = best if best > alpha else alpha
            if alpha >= beta:
                return best
        return best
    else:
        best = WIN_SCORE
        for n_pos in childrens[pos]:
            val = quick_alphabeta(n_pos, depth - 1, alpha, beta, True)
            best = val if val < best else best
            beta = best if best < beta else beta
            if beta <= alpha:
                return best
        return best
Esempio n. 5
0
def alphabeta(pos, depth, alpha, beta, me):
    global s
    #print(hex(pos), depth, alpha, beta, me, scores[pos], end = "\r")
    #gbib.print_board(gbib.pos_to_board(pos))
    if depth == 0 or scores[pos] == WIN_SCORE or scores[
            pos] == -WIN_SCORE or gbib.is_tie(pos):
        s += 1
        return scores[pos]

    if me:
        best = -WIN_SCORE
        children = get_children(pos, True)[0:5]
        i = 0
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, False)
            best = val if val > best else best
            alpha = best if best > alpha else alpha
            if alpha >= beta:
                quits[depth].append(i)
                return best
            i += 1
        quits[depth].append(i)
        return best
    else:
        best = WIN_SCORE
        children = get_children(pos, False)[0:5]
        i = 0
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, True)
            best = val if val < best else best
            beta = best if best < beta else beta
            if beta <= alpha:
                quits[depth].append(i)
                return best
            i += 1
        quits[depth].append(i)
        return best