User File #638723765819061460

Upload All User Files

#638723765819061460 - c64 flooder solver

blank.txt
3 downloads
Uploaded 4 days ago by Cephla (see all 24)

Syntax Error

Syntax Error on line 98
  1. from collections import deque
  2. import copy
  3. 
  4. class FlooderGameSolver:
  5.     def __init__(self, board):
  6.         self.initial_board = board
  7.         self.size_x = len(board[0])
  8.         self.size_y = len(board)
  9.         self.max_moves = 20  # Set the maximum number of moves
 10.     
 11.     def flood_fill(self, board, x, y, target_number, replacement_number):
 12.         if target_number == replacement_number:
 13.             return board
 14. 
 15.         if board[x][y] != target_number:
 16.             return board
 17. 
 18.         queue = deque([(x, y)])
 19.         while queue:
 20.             cx, cy = queue.popleft()
 21.             if board[cx][cy] == target_number:
 22.                 board[cx][cy] = replacement_number
 23.                 if cx > 0:
 24.                     queue.append((cx - 1, cy))
 25.                 if cy > 0:
 26.                     queue.append((cx, cy - 1))
 27.                 if cx < self.size_y - 1:
 28.                     queue.append((cx + 1, cy))
 29.                 if cy < self.size_x - 1:
 30.                     queue.append((cx, cy + 1))
 31.         return board
 32. 
 33.     def is_winner(self, board):
 34.         first_number = board[0][0]
 35.         for row in board:
 36.             for cell in row:
 37.                 if cell != first_number:
 38.                     return False
 39.         return True
 40. 
 41.     def find_solutions(self):
 42.         initial_board = self.initial_board
 43.         queue = deque([(initial_board, 0, [])])  # (current_board, steps, path)
 44. 
 45.         visited = set()
 46.         visited.add(tuple(map(tuple, initial_board)))
 47. 
 48.         solutions = []
 49.         min_steps = None
 50. 
 51.         while queue:
 52.             current_board, steps, path = queue.popleft()
 53.             
 54.             if steps > self.max_moves:
 55.                 continue  # Skip processing this path if it exceeds the move limit
 56. 
 57.             if self.is_winner(current_board):
 58.                 if min_steps is None:
 59.                     min_steps = steps
 60.                 if steps == min_steps:
 61.                     solutions.append((steps, path))
 62.                 continue
 63.             
 64.             if min_steps is not None and steps >= min_steps:
 65.                 continue
 66. 
 67.             top_left_number = current_board[0][0]
 68.             possible_numbers = set()
 69.             for row in current_board:
 70.                 possible_numbers.update(row)
 71. 
 72.             for number in possible_numbers:
 73.                 if number != top_left_number:
 74.                     new_board = copy.deepcopy(current_board)
 75.                     self.flood_fill(new_board, 0, 0, top_left_number, number)
 76.                     new_board_tuple = tuple(map(tuple, new_board))
 77.                     if new_board_tuple not in visited:
 78.                         visited.add(new_board_tuple)
 79.                         if steps + 1 <= self.max_moves:  # Add the new state only if it stays within the move limit
 80.                             queue.append((new_board, steps + 1, path + [number]))
 81. 
 82.         return solutions
 83. 
 84.     def validate_solution(self, solution_path):
 85.         board = copy.deepcopy(self.initial_board)
 86.         for step in solution_path:
 87.             top_left_number = board[0][0]
 88.             self.flood_fill(board, 0, 0, top_left_number, step)
 89.             self.display_board(board)
 90.         return self.is_winner(board)
 91. 
 92.     def display_board(self, board):
 93.         for row in board:
 94.             print(" ".join(map(str, row)))
 95.         print()
 96. 
 97. # Define the board
 98. board = [Unexpected EOL parsing text in []
 99.     [3, 5, 2, 7, 7],
100.     [1, 5, 8, 1, 7],
101.     [3, 2, 3, 4, 3],
102.     [1, 2, 4, 8, 2],
103.     [2, 3, 2, 5, 8],
104.     [3, 6, 7, 2, 4],
105.     [7, 5, 7, 3, 5],
106.     [3, 1, 2, 3, 1],
107.     [4, 8, 6, 7, 6],
108.     [7, 3, 8, 6, 4],
109. ]
110. 
111. solver = FlooderGameSolver(board)
112. solutions = solver.find_solutions()
113. if solutions:
114.     print(f"Found {len(solutions)} solutions with {solutions[0][0]} steps:")
115.     for steps, path in solutions:
116.         print(f"Steps: {steps}, Path: {path}")
117.         print("Validating solution...")
118.         if solver.validate_solution(path):
119.             print("Solution is valid.")
120.         else:
121.             print("Solution is invalid.")
122. else:
123.     print("No solution found")