Advertisement
Hinski2

Untitled

May 22nd, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. import random
  2. import copy
  3.  
  4. # Definicje stałych
  5. black, white, empty = range(3)
  6. max_player, min_player = black, white
  7. vector_moves = [(i, j) for i in [-1, 0, 1] for j in [-1, 0, 1] if (i, j) != (0, 0)]
  8.  
  9. # Klasa reprezentująca węzeł drzewa ruchów
  10. class MovesTree:
  11.     def __init__(self, turn, alpha, beta, val, board, poss):
  12.         self.turn = turn
  13.         self.alpha = alpha
  14.         self.beta = beta
  15.         self.val = val
  16.         self.board = copy.deepcopy(board)
  17.         self.poss = copy.deepcopy(poss)
  18.         self.children = []
  19.  
  20.     def valid(self, x, y):
  21.         return 0 <= x <= 7 and 0 <= y <= 7 and self.board[x][y] == empty
  22.    
  23.     def generate_moves(self):
  24.         poss_moves = []
  25.         for u in self.poss[self.turn]:
  26.             for i, j in vector_moves:
  27.                 pos = [u[0] + i, u[1] + j]
  28.                 zbity = False
  29.                 while 0 <= pos[0] <= 7 and 0 <= pos[1] <= 7 and self.board[pos[0]][pos[1]] == self.turn ^ 1:
  30.                     zbity = True
  31.                     pos[0] += i
  32.                     pos[1] += j
  33.                 if self.valid(pos[0], pos[1]) and zbity:
  34.                     poss_moves.append(pos)
  35.         return poss_moves
  36.    
  37.     def make_move(self, x, y):
  38.         player = self.turn
  39.         self.board[x][y] = player
  40.         self.poss[player].append([x, y])
  41.        
  42.         for i, j in vector_moves:
  43.             pos = [x + i, y + j]
  44.             to_flip = []
  45.             while 0 <= pos[0] <= 7 and 0 <= pos[1] <= 7 and self.board[pos[0]][pos[1]] == player ^ 1:
  46.                 to_flip.append([pos[0], pos[1]])
  47.                 pos[0] += i
  48.                 pos[1] += j
  49.             if 0 <= pos[0] <= 7 and 0 <= pos[1] <= 7 and self.board[pos[0]][pos[1]] == player:
  50.                 for fx, fy in to_flip:
  51.                     self.board[fx][fy] = player
  52.                     self.poss[player].append([fx, fy])
  53.                     self.poss[player ^ 1].remove([fx, fy])
  54.    
  55.     def eval(self):
  56.         return len(self.poss[self.turn]) - len(self.poss[self.turn ^ 1])
  57.    
  58.     def game_result(self):
  59.         black_count = sum(row.count(black) for row in self.board)
  60.         white_count = sum(row.count(white) for row in self.board)
  61.         return 0 if black_count > white_count else 1
  62.  
  63. def min_max(node, depth):
  64.     if depth == 8 or not node.generate_moves():
  65.         return node.eval()
  66.    
  67.     if node.turn == max_player:
  68.         value = float('-inf')
  69.         for move in node.generate_moves():
  70.             child = MovesTree(node.turn ^ 1, node.alpha, node.beta, float('inf'), node.board, node.poss)
  71.             child.make_move(*move)
  72.             node.children.append(child)
  73.             value = max(value, min_max(child, depth + 1))
  74.             node.alpha = max(node.alpha, value)
  75.             if node.alpha >= node.beta:
  76.                 break
  77.         return value
  78.     else:
  79.         value = float('inf')
  80.         for move in node.generate_moves():
  81.             child = MovesTree(node.turn ^ 1, node.alpha, node.beta, float('-inf'), node.board, node.poss)
  82.             child.make_move(*move)
  83.             node.children.append(child)
  84.             value = min(value, min_max(child, depth + 1))
  85.             node.beta = min(node.beta, value)
  86.             if node.beta <= node.alpha:
  87.                 break
  88.         return value
  89.  
  90. def choose_optimal(node):
  91.     if node.turn == max_player:
  92.         return max(node.children, key=lambda child: child.val, default=None)
  93.     else:
  94.         return min(node.children, key=lambda child: child.val, default=None)
  95.  
  96. def choose_random(node):
  97.     return random.choice(node.children) if node.children else None
  98.  
  99. if __name__ == "__main__":
  100.     result = [0, 0]
  101.     for _ in range(1000):
  102.         # Inicjalizacja planszy
  103.         board = [[empty] * 8 for _ in range(8)]
  104.         board[3][3] = board[4][4] = black
  105.         board[3][4] = board[4][3] = white
  106.        
  107.         # Inicjalizacja węzła początkowego
  108.         black_poss = [[3, 3], [4, 4]]
  109.         white_poss = [[3, 4], [4, 3]]
  110.         moves_tree = MovesTree(black, float('-inf'), float('inf'), float('-inf'), board, [black_poss, white_poss])
  111.        
  112.         end_game = False
  113.         turn = black
  114.         turn_no = 1
  115.        
  116.         while not end_game:
  117.             if turn == black:
  118.                 min_max(moves_tree, 0)
  119.                 new_moves_tree = choose_optimal(moves_tree)
  120.             else:
  121.                 if turn_no == 1:
  122.                     min_max(moves_tree, 0)
  123.                 new_moves_tree = choose_random(moves_tree)
  124.            
  125.             if new_moves_tree is None:
  126.                 end_game = True
  127.                 break
  128.            
  129.             moves_tree = new_moves_tree
  130.             turn ^= 1
  131.             turn_no += 1
  132.            
  133.             if not moves_tree.generate_moves():
  134.                 end_game = True
  135.  
  136.         result[moves_tree.game_result()] += 1
  137.         print(result)
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement