1

I want to implement an agent for 3-Men's Morris game-which is very similar to tic-tac-toe game- and i want to use Minimax strategy with Alpha-Beta Pruning, here's my code in Python based on this post and this post on StackOverflow , but it doesn't work!! it gives a wrong solution,even when one of successors of current state is 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) and (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 for player based on rules
  for 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')
    for 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 which is selected here is not really the best move!
    return best_move
Community
  • 1
  • 1
Iman Mirzadeh
  • 33
  • 1
  • 6
  • *"a wrong solution"* - examples? Inputs and expected and actual outputs would be helpful. – jonrsharpe Apr 28 '15 at 21:54
  • @jonrsharpe input outputs depends on a specific problem.but in general, assume if you do one of the possible next moves,you'll win the game!so you MUST do that but the rootAlphaBeta return (best_move) is not that move! – Iman Mirzadeh Apr 28 '15 at 21:57
  • 2
    *"input outputs depends on a specific problem"* - so **pick one** and show that. Please read http://stackoverflow.com/help/mcve – jonrsharpe Apr 28 '15 at 22:05
  • try to implement the [Minimax](http://en.wikipedia.org/wiki/Minimax#Pseudocode) or [Alpha-beta pruning](http://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning#Pseudocode) pseudocodes – Nick Dandoulakis Apr 28 '15 at 22:34

0 Answers0