def movepoint(board, color): moves = [] for i in range(8): for j in range(8): if gameplay.valid(board, color, (i, j)): moves.append((i, j)) return len(moves)
def movepoint(board,color): moves=[] for i in range(8): for j in range(8): if gameplay.valid(board, color, (i,j)): moves.append((i,j)) return len(moves)
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): moves = [] for i in range(8): for j in range(8): if valid(board, color, (i,j)): moves.append((i,j)) if len(moves) == 0: return "pass" bestMove = moves[random.randint(0,len(moves) - 1)] return bestMove
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): if gameplay.valid(board, color, 'pass'): return "pass" depth = 6 if time <= 170 and time > 100: depth = 5 if time <= 100 and time > 50: depth = 4 if time <= 50 and time > 20: depth = 3 if time <= 20 and time > 5: depth = 2 if time <= 5: depth = 1 (move, value) = max_val(board, -INF, INF, depth, color) return move
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 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 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) > 9: deepest = 7 else: deepest = 8 search_depth = 0 alpha = -float('inf') beta = float('inf') ori_color=color if not reversed: v,bestmove = maxvalue(board,color,ori_color,deepest,search_depth,alpha,beta) else: v,bestmove = minvalue(board,color,ori_color,deepest,search_depth,alpha,beta) 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 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) > 9: deepest = 7 else: deepest = 8 search_depth = 0 alpha = -float('inf') beta = float('inf') ori_color = color if not reversed: v, bestmove = maxvalue(board, color, ori_color, deepest, search_depth, alpha, beta) else: v, bestmove = minvalue(board, color, ori_color, deepest, search_depth, alpha, beta) 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 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