Advertisement
pierdziadek

si3

Apr 2nd, 2025 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. import copy
  2. import random
  3.  
  4. from exceptions import AgentException
  5.  
  6. class MinMaxAgent:
  7.     def __init__(self, my_token='o'):
  8.         self.my_token = my_token
  9.  
  10.     def decide(self, connect4):
  11.         if connect4.who_moves != self.my_token:
  12.             raise AgentException('not my round')
  13.        
  14.         score, best_drop = self.minimax(connect4, 5, connect4.who_moves)
  15.         if best_drop is None:
  16.             return random.choice(connect4.possible_drops())
  17.         return best_drop
  18.  
  19.     def minimax(self, connect4, depth, who_moves):
  20.         if depth == 0:
  21.             return 0, None
  22.         if who_moves != connect4.who_moves:
  23.             raise AgentException('not my round')
  24.            
  25.         score = -999
  26.         best_drop = None
  27.         for possible_drop in connect4.possible_drops():
  28.             plansza_testowa = copy.deepcopy(connect4)
  29.             plansza_testowa.drop_token(possible_drop)
  30.             if plansza_testowa.game_over:
  31.                 if plansza_testowa.wins == who_moves:
  32.                     return 1, possible_drop
  33.             new_score, new_best_drop = self.minimax(plansza_testowa, depth - 1, 'o' if who_moves == 'x' else 'x')
  34.             new_score *= -1
  35.             if new_score > score:
  36.                 score = new_score
  37.                 best_drop = possible_drop
  38.         return score, best_drop
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. import copy
  47. import random
  48.  
  49. from exceptions import AgentException
  50.  
  51.  
  52. class AlphaBetaAgent:
  53.     def __init__(self, my_token='o'):
  54.         self.my_token = my_token
  55.  
  56.     def decide(self, connect4):
  57.         if connect4.who_moves != self.my_token:
  58.             raise AgentException('not my round')
  59.        
  60.         score, best_drop = self.alphabeta(connect4, 5, connect4.who_moves, -999, 999)
  61.         if best_drop is None:
  62.             return random.choice(connect4.possible_drops())
  63.         return best_drop
  64.  
  65.     def alphabeta(self, connect4, depth, who_moves, alpha, beta):
  66.         if depth == 0:
  67.             return 0, None
  68.         if who_moves != connect4.who_moves:
  69.             raise AgentException('not my round')
  70.            
  71.         score = -999
  72.         best_drop = None
  73.         for possible_drop in connect4.possible_drops():
  74.             plansza_testowa = copy.deepcopy(connect4)
  75.             plansza_testowa.drop_token(possible_drop)
  76.             if plansza_testowa.game_over:
  77.                 if plansza_testowa.wins == who_moves:
  78.                     return 1, possible_drop
  79.             new_score, new_best_drop = self.alphabeta(plansza_testowa, depth - 1, 'o' if who_moves == 'x' else 'x', -beta, -alpha)
  80.             new_score *= -1
  81.  
  82.             if new_score > score:
  83.                 score = new_score
  84.                 best_drop = possible_drop
  85.  
  86.             alpha = max(alpha, score)
  87.  
  88.             if alpha >= beta:
  89.                 break
  90.         return score, best_drop
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement