Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 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.  
  26.    board_is_full = True  # starter med at sætte et fuldt bræt
  27.    for i in range(len(state)):  # looper igennem længden af state og hvis det ikke er fyldt med X eller O er det ikke fyldt
  28.        if state[i] != 'O' and state[i] != 'X':
  29.            board_is_full = False
  30.            break
  31.  
  32.    if board_is_full:  # returnerer hvis brættet er fyldt
  33.        return True
  34.  
  35.    #bruger resultatet af utility_of(state) til at se om der er vundet eller tabt, ellers returneres false.
  36.    utility = utility_of(state)
  37.    if utility == 1 or utility == -1:
  38.        return True
  39.    else:
  40.        return False
  41. '''
  42.  
  43. def is_terminal(state):
  44.     no_options = True
  45.     for entry in state:
  46.         if entry > 2:    # hvis bunken er større end 2, kan man stadig dele bunken.
  47.             no_options = False
  48.             break
  49.  
  50.     if no_options:
  51.         return True
  52.  
  53.     utility = utility_of(state)
  54.     if utility == 0 or utility == 1:
  55.         return True
  56.     else:
  57.         return False
  58.  
  59.  
  60. def utility_of(state):
  61.     """
  62.    returns +1 if winner is X (MAX player), -1 if winner is O (MIN player), or 0 otherwise
  63.    :param state: State of the checkerboard. Ex: [0; 1; 2; 3; X; 5; 6; 7; 8]
  64.    :return:
  65.    """
  66.     for entry in state:
  67.         if entry == 2 and len(state) % 2 == 1:
  68.             return 1
  69.         elif entry == 2 and len(state) % 2 == 0:
  70.             return 0
  71.  
  72.     return -1
  73.  
  74.  
  75.  
  76. def successors_of(state):
  77.     """
  78.    returns a list of tuples (move, state) as shown in the exercise slides
  79.    :param state: State of the checkerboard. Ex: [0; 1; 2; 3; X; 5; 6; 7; 8]
  80.    :return:
  81.    """
  82.  
  83.     # Figure out whos' turn it is.
  84.  
  85.     min_turn = False
  86.     if len(state) % 2 == 1:
  87.         min_turn = True
  88.  
  89.     # Generate valid moves
  90.     valid_moves = []
  91.     for i in range(len(state)):
  92.         if state[i] == 1 or state[i] == 2:
  93.             continue
  94.         count = 1
  95.         while count < state[i]/2:
  96.             new_state = state.copy()
  97.             new_state[i] = new_state[i]-count
  98.             new_state.append(count)
  99.             new_state.sort(reverse=True)
  100.             valid_moves.append((count,new_state))
  101.             count += 1
  102.     return valid_moves
  103.  
  104.  
  105.  
  106. def display(state):
  107.     print("-----")
  108.     print(state)
  109.     '''
  110.    for c in [0, 3, 6]:
  111.        print(state[c + 0], state[c + 1], state[c + 2])
  112.    '''
  113.  
  114. def main():
  115.     board = [7]
  116.     while not is_terminal(board):
  117.         board[minmax_decision(board)] = 'X'
  118.         if not is_terminal(board):
  119.             display(board)
  120.             board[int(input('Your move? '))] = 'O'
  121.     display(board)
  122.  
  123.  
  124. def argmax(iterable, func):
  125.     return max(iterable, key=func)
  126.  
  127.  
  128. if __name__ == '__main__':
  129.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement