def alphaBeta(board, color,reverse, depth, strategy, alpha = -sys.maxint, beta = sys.maxint) : if depth == 0 : if strategy == 1 : return evalue(board) else: return value(board) moves = [] for i in range(8) : for j in range(8) : if gameplay.valid(board, color, (i,j)) : moves.append((i,j)) if len(moves) == 0 : if (gameplay.gameOver(board)) : return evalue(board) else: if (color == "B" and not reverse) or (color == "W" and reverse) : newBoard = deepcopy(board) gameplay.doMove(newBoard,color,'pass') val = max(alpha, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) return val else : newBoard = deepcopy(board) gameplay.doMove(newBoard,color,'pass') val = min(beta, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) return val else: if (color == "B" and not reverse) or (color == "W" and reverse) : for move in moves : newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) alpha = max(alpha, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) if beta <= alpha : break return alpha else : for move in moves : newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) beta = min(beta, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) if beta <= alpha: break return beta
def maxvalue(board, color, ori_color, deepest, search_depth, alpha, beta): search_depth = search_depth + 1 if search_depth == deepest or gameplay.gameOver(board): return calculate(board, search_depth, ori_color) moves = [] bestmove = "pass" for i in range(8): for j in range(8): if gameplay.valid(board, color, (i, j)): moves.append((i, j)) v = -float('inf') if len(moves) == 0: if search_depth == 1: return float('inf'), "pass" else: return float('inf') # print 'max',search_depth # print moves for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, move) min_value = minvalue(newBoard, gameplay.opponent(color), ori_color, deepest, search_depth, alpha, beta) if min_value > v: # if search_depth==1: # print "v=",v,"min_value=",min_value v = min_value if search_depth == 1: bestmove = move if v >= beta: if search_depth == 1: return v, bestmove else: return v alpha = max(alpha, v) if search_depth == 1: return v, bestmove else: return v
def maxvalue(board,color,ori_color,deepest,search_depth,alpha,beta): search_depth=search_depth+1 if search_depth == deepest or gameplay.gameOver(board): return calculate(board,search_depth,ori_color) moves=[] bestmove="pass" for i in range(8): for j in range(8): if gameplay.valid(board, color, (i,j)): moves.append((i,j)) v=-float('inf') if len(moves) == 0: if search_depth==1: return float('inf'),"pass" else: return float('inf') # print 'max',search_depth # print moves for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) min_value=minvalue(newBoard,gameplay.opponent(color),ori_color,deepest,search_depth,alpha,beta) if min_value>v: # if search_depth==1: # print "v=",v,"min_value=",min_value v=min_value if search_depth==1: bestmove=move if v>=beta: if search_depth==1: return v,bestmove else: return v alpha=max(alpha,v) if search_depth==1: return v,bestmove else: return v
def alpha_beta_search(node, alpha, beta, timeout): if timeout(): return node.value() # Timeout do nothing if (node.depth == 0): if (god.gameOver(node.board)): return node.end_value() else: return node.value() nextMoves = node.validMoves() if (len(nextMoves) == 0): nextMoves = ['pass'] # Game not end, we can pass on. for pos in nextMoves: # init next child node nextnode = TreeNode(node) god.doMove(nextnode.board, node.color, pos) # Update from children res = alpha_beta_search(nextnode, alpha, beta, timeout) # Update bounds if (node.color == 'W') != (node.reversed): # min if betterThan(beta, res, node.color, node.reversed): beta = res else: # max if betterThan(alpha, res, node.color, node.reversed): alpha = res # Pruning if (alpha >= beta): #if VERBOSE: print "==Pruning %f..%f in %d" % (alpha, beta, node.depth) break if (node.color == 'W') != (node.reversed): # min return beta else: return alpha
def endState(state): return gameplay.gameOver(state)
def alphaBeta(board, color, reverse, depth, strategy, alpha=-sys.maxint, beta=sys.maxint): if depth == 0: if strategy == 1: return evalue(board) else: return value(board) moves = [] for i in range(8): for j in range(8): if gameplay.valid(board, color, (i, j)): moves.append((i, j)) if len(moves) == 0: if (gameplay.gameOver(board)): return evalue(board) else: if (color == "B" and not reverse) or (color == "W" and reverse): newBoard = deepcopy(board) gameplay.doMove(newBoard, color, 'pass') val = max( alpha, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) return val else: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, 'pass') val = min( beta, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) return val else: if (color == "B" and not reverse) or (color == "W" and reverse): for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, move) alpha = max( alpha, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) if beta <= alpha: break return alpha else: for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, move) beta = min( beta, alphaBeta(newBoard, gameplay.opponent(color), reverse, depth - 1, strategy, alpha, beta)) if beta <= alpha: break return beta