def cpu_move(): global board, decision, last, first, current, moving, cpucaptured if moving: return if first: first = False decision = ai.decide(board, cpu) cpucaptured = logic.captured(board, decision[1], decision[2][0]) board.remove(cpucaptured) board.move(decision[1], decision[2][0][0]) last = decision[2][0][0] grid.move(decision[1], decision[2][0][0]) moving = True else: grid.remove(cpucaptured) chain = decision[2] del chain[0] if chain: cpucaptured = logic.captured(board, last, chain[0]) board.remove(cpucaptured) board.move(last, chain[0][0]) grid.move(last, chain[0][0]) last = chain[0][0] moving = True else: first = True current = player board.children = ai.successors(board, player)
def move(): global captured, chains, lastpos, resolving, board, choice, grid, chaining if captured: # ambiguity needs to be resolved first add_ring(itertools.chain.from_iterable([c[0] for c in captured])) resolving = True else: # make the move on the board and remove captured pieces cap = logic.captured(board, lastpos, chains[0][0]) board.remove(cap) grid.remove(cap) board.move(lastpos, chains[0][0][0]) lastpos = chains[0][0][0] # advance in the chains for i in range(len(chains)): del chains[i][0] newchains = [] for chain in chains: if chain: newchains.append(chain) chains = newchains if chains: # continue chaining chaining = True add_ring([lastpos]) add_glow(set([chain[0][0] for chain in chains])) choice = True else: end_turn()
def player_move(sel): global moving, chains, captured, lastpos, board, choice, grid, resolving, chaining if moving: return if resolving and sel in itertools.chain.from_iterable([c[0] for c in captured]): # resolved for cap in captured: if sel in cap[0]: resolving = False for i, chain in enumerate(chains): if chain[0] != cap[1]: del chains[i] break captured = [] move() return if choice: if sel in [chain[0][0] for chain in chains]: # choosing where to advance choice = False rgroup.empty() ggroup.empty() grid.move(lastpos, sel) chains = [c for c in chains if c[0][0] == sel] # find if move is ambiguous first = [c[0] for c in chains] if any(tup[1] == A for tup in first) and any(tup[1] == W for tup in first): captured.append((logic.captured(board, lastpos, (sel, A)),(sel,A))) captured.append((logic.captured(board, lastpos, (sel, W)),(sel,W))) return elif not chaining: lastpos = None choice = False if not lastpos and sel and sel in board.colored(player): # first selection chains = [x[2] for x in board.children if x[1] == sel] add_ring([sel]) add_glow(set([chain[0][0] for chain in chains])) if chains: choice = True lastpos = sel return
def successors(board, color): if board.children: return board.children stack = [] children = [] for pos in board.colored(color): if pos not in board.moves: continue start = pos chain = [] tuples = [] b = board.copy() tuples = board.moves[pos] while tuples: for move in tuples: cb = b.copy() newpos = move[0] cb.remove(logic.captured(cb, pos, move)) cb.move(pos, newpos) if move[1] != P: tups = logic.refine(cb, newpos, cb.adjacent(newpos), [((start), 0)] + chain) else: tups = [] chain.append(move) stack.append((cb.copy(), newpos, chain[:], tups[:])) del chain[-1] b, pos, chain, tuples = stack[-1] del stack[-1] while not tuples: children.append((b, start, chain)) try: b, pos, chain, tuples = stack[-1] del stack[-1] except IndexError: break return children