Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. def minmax_decision(state):
  2.     def max_value(state):
  3.         if is_terminal(state):
  4.             return utility_of(state)
  5.         v = -infinity
  6.         for (a, s) in successors_of(state):
  7.             v = max(v, min_value(s))
  8.         print('V: ' + str(v))
  9.         return v
  10.  
  11.     def min_value(state):
  12.         if is_terminal(state):
  13.             return utility_of(state)
  14.         v = infinity
  15.         for (a, s) in successors_of(state):
  16.             v = min(v, max_value(s))
  17.         return v
  18.  
  19.     infinity = float('inf')
  20.     action, state = argmax(successors_of(state), lambda a: min_value(a[1]))
  21.     return action
  22.  
  23.  
  24. def is_terminal(state):
  25.     no_options = True
  26.     for entry in state:
  27.         if entry > 2:    # hvis bunken er større end 2, kan man stadig dele bunken.
  28.             no_options = False
  29.             break
  30.  
  31.     if no_options:
  32.         return True
  33.  
  34.     utility = utility_of(state)
  35.     if utility == 0 or utility == 1:
  36.         return True
  37.     else:
  38.         return False
  39.  
  40.  
  41. def utility_of(state):
  42.     '''
  43.    Checks who wins. if 0 min if 1 max
  44.    the check works, because we are sorting the state space to have the biggest number first.
  45.    If the first number in the state space is two, we cannot split the pile anymore.
  46.  
  47.    If the entry is 2 and modulo is 1, we are on a minimum level,
  48.    minimum cannot divide the piles anymore, and max wins.
  49.    '''
  50.     for entry in state:
  51.         if state[0] == 2 and len(state) % 2 == 1:
  52.             return 1
  53.         elif state[0] == 2 and len(state) % 2 == 0:
  54.             return 0
  55.  
  56.     return -1
  57.  
  58.  
  59.  
  60. def successors_of(state):
  61.     """
  62.    The successor seems to create the correct tree
  63.    """
  64.  
  65.     # Figure out whos' turn it is.
  66.  
  67.     min_turn = False
  68.     if len(state) % 2 == 1:
  69.         min_turn = True
  70.  
  71.     # Generate valid moves
  72.     valid_moves = []
  73.     print("State: " , state)
  74.     for i in range(len(state)):
  75.         if state[i] == 1 or state[i] == 2:
  76.             continue
  77.         count = 1
  78.         while count < state[i]/2:
  79.             new_state = state.copy()
  80.             new_state[i] = new_state[i]-count
  81.             new_state.append(count)
  82.             new_state.sort(reverse=True)
  83.             valid_moves.append((count - 1 ,new_state))
  84.             count += 1
  85.     print(valid_moves)
  86.     return valid_moves
  87.  
  88.  
  89.  
  90. def display(state):
  91.     print("-----")
  92.     print(state)
  93.     '''
  94.    for c in [0, 3, 6]:
  95.        print(state[c + 0], state[c + 1], state[c + 2])
  96.    '''
  97.  
  98. def main():
  99.     board = [7]
  100.     while not is_terminal(board):
  101.         board[minmax_decision(board)] = 'X'
  102.         if not is_terminal(board):
  103.             display(board)
  104.             board[int(input('Your move? '))] = 'O'
  105.     display(board)
  106.  
  107.  
  108. def argmax(iterable, func):
  109.     return max(iterable, key=func)
  110.  
  111.  
  112. if __name__ == '__main__':
  113.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement