示例#1
0
def alpha_beta_tree(state, depth, last_max, last_min, is_max):
  if(depth == 0): #end of recursive function
    return ai.Move("", get_state_evaluation(state))  #return the value of this position
    
  move_strings = get_possible_moves(state) #get the strings corresponding to possible moves
  if(len(move_strings) == 0): return ai.Move("",get_state_evaluation(state)) #handle edge case
  
  new_state = ai.Board_State()
  new_state.copy_board(state)
  new_state.execute_move(move_strings[0])
  if(is_max): best_move = alpha_beta_tree(new_state, depth-1, None, last_min, False)
  else: best_move = alpha_beta_tree(new_state, depth-1, last_max, None, True)
  best_move.tag = move_strings[0]  
  
  for move in move_strings[1:]:   #search each other move
    new_state.copy_board(state)   #copy the board
    new_state.execute_move(move)  #execute the move
    if(is_max): contender = alpha_beta_tree(new_state, depth-1, best_move.value, last_min, False) #recurse!
    else: contender = alpha_beta_tree(new_state, depth-1, last_max, best_move.value, True) #recurse!
    if((is_max and contender.value >= best_move.value) or (not is_max and contender.value <= best_move.value)):
      best_move = contender #contender is better
      best_move.tag = move  #update the tag
      #Do alpha beta - remove any value which cannot possiby replace an existing one
      if(last_max != None and last_min != None):
        if(best_move.value >= last_min or best_move.value <= last_max): return best_move 
      elif(last_max != None and best_move.value <= last_max): return best_move
      elif(last_min != None and best_move.value >= last_min):  return best_move
  
  return best_move
示例#2
0
def do_search_thread(move):
  global global_state   # use global state
  global top_max        # use global max
  global lock           # use global lock
  
  # print move
  
  new_state = ai.Board_State()  # create a new state
  new_state.copy_board(global_state) # copy the main state
  new_state.execute_move(move)  # execute the move
  
  lock.acquire() # get the lock
  gmax = top_max # copy the global max
  lock.release() # release the lock
  
  best_move = alpha_beta_min(new_state, MAX_DEPTH-1, gmax, 900000) # throw to tree search for next iteration
  if(best_move.tag == ''): return ai.Move("",-900000) # catch bad results

  lock.acquire() # get the lock
  if(top_max < best_move.value): # check if we're better
    top_max = best_move.value # store new best value
  lock.release() # release the lock
  
  best_move.tag = move
  return best_move
示例#3
0
def alpha_beta_min(state, depth, last_max, last_min):
  if(depth == 0): # end of recursive function
    return ai.Move("", get_state_evaluation(state))  # return the value of this position
    
  move_strings = get_possible_moves(state) # get the strings corresponding to possible moves
  if(len(move_strings) == 0):
    return ai.Move("end", get_state_evaluation(state)) # handle edge case
  
  new_state = ai.Board_State()    # create empty board object
  
  best_move = ai.Move("",last_min)  # preload move
  for move in move_strings:         # search each move
    new_state.copy_board(state)     # copy the board
    new_state.execute_move(move)    # execute the move
    contender = alpha_beta_max(new_state, depth-1, last_max, last_min).value # recurse!
    if(contender <= last_max): return ai.Move("",last_max) # no moves
    if(contender < last_min): # check result
      last_min = contender
      best_move = ai.Move(move, contender) # contender is better

  return best_move  # successfull move