algorithm - Python - Alpha-Beta Purning for Minimax -
i want implement agent 3-men's morris game-which similar tic-tac-toe game- , want use minimax strategy alpha-beta pruning, here's code in python based on this post , this post on stackoverflow , doesn't work!! gives wrong solution,even when 1 of successors of current state solution
def alpha_beta(state,alpha,beta,turn,depth): if int(terminal_test(state,turn)) == int(my_number): return 1 #win elif (int(terminal_test(state,turn))!=0) , (int(terminal_test(state,turn))!=int(my_number)) : return -1 #loose else: if int(depth) == 13: return 0 #reached limit moves = successors(state,turn,int(depth)) #valid moves player based on rules move in moves: state = make_move(state,move,turn) current_eval = -alpha_beta(state, -beta, -alpha, 2-int(turn),int(depth)+1) state = undo_move(state,move,turn) if current_eval >= beta: return beta if current_eval > alpha: alpha = current_eval return alpha def rootalphabeta(state,depth, turn): best_move = none max_eval = float('-infinity') moves = successors(state,turn,int(depth)) alpha = float('infinity') move in moves: state = make_move(state,move,turn) alpha = -alpha_beta(state, float('-infinity'), alpha, 2-int(turn),int(depth)+1) state = undo_move(state,move,turn) if alpha > max_eval: max_eval = alpha best_move = move #best_move selected here not best move! return best_move
Comments
Post a Comment