Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 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 e in successors_of(state):
  7.             v = max(v, min_value(e))
  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 e in successors_of(state):
  16.             v = min(v, max_value(e))
  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 i in range(len(state)):
  27.         if state[i] > 2:
  28.             no_options = False
  29.             break
  30.  
  31.     if no_options:
  32.         return True
  33.  
  34.     utility = utility_of(state)
  35.     if utility == 1 or utility == -1:
  36.         return True
  37.     else:
  38.         return False
  39.  
  40.  
  41. def utility_of(state):
  42.     if state[0] == [2, 1, 1, 1, 1, 1]:
  43.         if state[0] == 'computer':
  44.             return 1
  45.         else:
  46.             return -1
  47.  
  48.  
  49. def successors_of(pile):
  50.     total_pile = []
  51.     pile_index = 0
  52.     count = 0
  53.     pile.sort()  # by sorting beforehand it makes it easy to clean up
  54.     while pile_index < len(pile):
  55.         single_pile = pile[pile_index]
  56.         remainder = pile[:pile_index] + pile[pile_index + 1:]
  57.         splitted_pile = single_split(single_pile)
  58.         pile_index += 1
  59.         for item in splitted_pile:
  60.             for remains in remainder:
  61.                 item.append(remains)
  62.             item.sort()
  63.             total_pile.append((count, item))
  64.             count += 1
  65.     while pile in total_pile:
  66.         total_pile.remove(pile)  # prevents duplicates of the pile in the new pile
  67.     return total_pile
  68.  
  69.  
  70. def single_split(single_pile):
  71.     # 6 returns [[1,3],[2,4]]
  72.     # takes an int and returns possible states in list of lists form
  73.     split_pile = []
  74.     if single_pile < 3:
  75.         split_pile.append([single_pile])
  76.     else:
  77.         first = 1
  78.         last = single_pile - 1
  79.         if single_pile % 2 == 0:
  80.             mid = single_pile // 2
  81.         else:
  82.             mid = (single_pile // 2) + 1
  83.         while first < mid:
  84.             split_pile.append([first, last])
  85.             first += 1
  86.             last -= 1
  87.     return split_pile
  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.     starting_pile = [7]
  100.     while not is_terminal(starting_pile):
  101.         starting_pile[minmax_decision(starting_pile)] = 'computer'
  102.         if not is_terminal(starting_pile):
  103.             display(starting_pile)
  104.             starting_pile[int(input('Your move? '))] = 'player'
  105.     display(starting_pile)
  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