def nextMove(board, color, time, reversed = False): 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: return 'pass' if len(moves) > 8: depth = 3 elif len(moves) < 6: depth = 5 else: depth = 4 if time < 60: depth = 3 elif time < 40: depth = 2 best = None for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) moveVal = alphaBeta(newBoard, gameplay.opponent(color), reversed, depth) if best == None or betterThan(moveVal, best, color, reversed): bestMove = move best = moveVal return bestMove
def nextMove(board, color, time, reversed = False): # Init beginTime = timelib.time() # The beginning of this round root = TreeNode() # root node root.board = board d = max_depth(root.board) limits = math.floor(float(BOARD_DEPTH-d)/(BOARD_DEPTH-MAX_DEPTH)*(MAX_DEPTH-MIN_DEPTH))+MIN_DEPTH if VERBOSE: print "limits %f" % limits root.depth = min(d, limits) root.color = color root.reversed = reversed # Begin search if VERBOSE: print "==========Searching in depth %d" % (root.depth) # then use alpha-beta pruning search timeout = lambda : ((timelib.time() - beginTime) >= ROUND_TIME_LIMIT) or ((timelib.time() - beginTime) >= time*0.8) nextMoves = root.validMoves() if len(nextMoves) == 0: return 'pass' best = root.worst() for pos in nextMoves: nextnode = TreeNode(root) god.doMove(nextnode.board, root.color, pos) res = alpha_beta_search(nextnode, float('-inf'), float('inf'), timeout) if VERBOSE: print "===Try %s returned by %f", (pos, res) if betterThan(best, res, root.color, root.reversed): best = res move = pos if VERBOSE: print "===Update best %f with %s in %d", (best, pos, root.depth) return move
def successors(state, color): successors_list = [] moves = [] for i in range(8): for j in range(8): if gameplay.valid(state, color, (i, j)): moves.append((i, j)) for moves in moves: newBoard = deepcopy(state) gameplay.doMove(newBoard, color, moves) successors_list.append((moves, newBoard)) return successors_list
def bulk_func (state, color, limit, reversed, alpha= -sys.maxint, beta= sys.maxint): moves = [] for i in range(8): for j in range(8): if gameplay.valid(state, color, (i,j)): moves.append((i,j)) if len(moves) == 0: if limit ==0: return "pass", eval_f(state) else: return "pass", bulk_func(state, opponent(color), limit-1, reversed)[1] else: best = None bestMove= moves[0] for move in moves: newState = deepcopy(state) gameplay.doMove(newState,color,move) if limit ==0: poss_val= move, eval_f(newState) else: poss_val= bulk_func(newState, opponent(color), limit-1, reversed, alpha, beta) if best == None or betterThan(poss_val[1], best, color, reversed): bestMove = move best = poss_val[1] if color == "W" and reversed: if best <= alpha: return bestMove, best beta = min (beta,best) if color == "W" and not reversed: if best >= beta: return bestMove, best alpha = max(alpha,best) if color == "B" and not reversed: if best <= alpha: return bestMove, best beta = min (beta,best) if color == "B" and reversed: if best >= beta: return bestMove, best alpha = max(alpha,best) return bestMove, best
def nextMove(board, color, time, reversed=False): 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: return "pass" best = None for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, move) moveVal = value(newBoard) if best == None or betterThan(moveVal, best, color, reversed): bestMove = move best = moveVal return bestMove
def nextMove(board, color, time, reversed = False): 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: return "pass" best = None for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) moveVal = value(newBoard) if best == None or betterThan(moveVal, best, color, reversed): bestMove = move best = moveVal return bestMove
def nextMove(board, color, time, reversed = False): global depth global otime 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: return "pass" score = gameplay.score(board) num = score[0] + score[1] if len(moves) > 9: depth = 5 if len (moves) < 7: depth = 6 if time < 40 and num < ident_strategy: depth = 3 if depth > 6: depth = 6 if num >= ident_strategy: d = 8*8-num strategy = 1 else: d = depth strategy = 0 best = None for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) moveVal = alphaBeta(newBoard, gameplay.opponent(color), reversed, d, strategy) if best == None or betterThan(moveVal, best, color, reversed): bestMove = move best = moveVal otime = time return bestMove
def nextMove(board, color, time, reversed=False): global depth global otime 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: return "pass" score = gameplay.score(board) num = score[0] + score[1] if len(moves) > 9: depth = 5 if len(moves) < 7: depth = 6 if time < 40 and num < ident_strategy: depth = 3 if depth > 6: depth = 6 if num >= ident_strategy: d = 8 * 8 - num strategy = 1 else: d = depth strategy = 0 best = None for move in moves: newBoard = deepcopy(board) gameplay.doMove(newBoard, color, move) moveVal = alphaBeta(newBoard, gameplay.opponent(color), reversed, d, strategy) if best == None or betterThan(moveVal, best, color, reversed): bestMove = move best = moveVal otime = time return bestMove
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 alphaBeta(board, color, reversed, depth, alpha = -sys.maxint, beta = sys.maxint): moves = [] for i in range(8) : for j in range(8) : if gameplay.valid(board, color, (i,j)) : moves.append((i,j)) if depth == 0: return value(board) if len(moves) == 0: if (gameplay.gameOver(board)): return value(board) else: moves.insert(0, 'pass') if (color == "B" and not reversed) or (color == "W" and reversed) : for move in moves : newBoard = deepcopy(board) gameplay.doMove(newBoard,color,move) alpha = max(alpha, alphaBeta(newBoard, gameplay.opponent(color), reversed, depth - 1, 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), reversed, depth - 1, alpha, beta)) if beta <= alpha : break return beta
root = w.TreeNode() root.board = board3 root.depth = 3 root.color = "B" root.reversed = False nextMoves = root.validMoves() best = root.worst() bestmove = None for pos in nextMoves: nextnode = w.TreeNode(root) god.doMove(nextnode.board, root.color, pos) res = w.alpha_beta_search(nextnode, float("-inf"), float("inf"), None) res2 = d.alphaBeta(nextnode.board, "W", False, 3, 0) print "===Try %s valued %f returned by %f" % (pos, nextnode.value(), res) print "===Dff %s valued %f returned by %f" % (pos, nextnode.value(), res2) if w.betterThan(best, res, "B", False): best = res bestmove = pos god.printBoard(nextnode.board) print best print pos
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