Advertisement
Guest User

Game

a guest
May 29th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | Source Code | 0 0
  1. from itertools import permutations
  2.  
  3. # Constants
  4. GRID_SIZE = 3
  5. DIGITS = list(range(1, 10))
  6.  
  7. # Score functions
  8. def score_rows(grid):
  9. return sum(row[0] * row[1] * row[2] for row in grid)
  10.  
  11. def score_cols(grid):
  12. return sum(grid[0][j] * grid[1][j] * grid[2][j] for j in range(3))
  13.  
  14. # Recursive Minimax with memoization
  15. memo = {}
  16.  
  17. def serialize_state(grid, remaining_digits, is_rows_turn):
  18. flat_grid = tuple(cell for row in grid for cell in row)
  19. return (flat_grid, tuple(remaining_digits), is_rows_turn)
  20.  
  21. def minimax(grid, remaining_digits, is_rows_turn):
  22. key = serialize_state(grid, remaining_digits, is_rows_turn)
  23. if key in memo:
  24. return memo[key]
  25.  
  26. if not remaining_digits:
  27. # Game over
  28. r_score = score_rows(grid)
  29. c_score = score_cols(grid)
  30. result = 1 if r_score > c_score else -1 if c_score > r_score else 0
  31. memo[key] = result
  32. return result
  33.  
  34. best = -2 if is_rows_turn else 2
  35. for i in range(3):
  36. for j in range(3):
  37. if grid[i][j] == 0:
  38. for d in remaining_digits:
  39. new_grid = [row[:] for row in grid]
  40. new_grid[i][j] = d
  41. new_remaining = remaining_digits[:]
  42. new_remaining.remove(d)
  43. result = minimax(new_grid, new_remaining, not is_rows_turn)
  44. if is_rows_turn:
  45. best = max(best, result)
  46. if best == 1:
  47. memo[key] = best
  48. return best
  49. else:
  50. best = min(best, result)
  51. if best == -1:
  52. memo[key] = best
  53. return best
  54. memo[key] = best
  55. return best
  56.  
  57. # Generalized function to evaluate possible moves
  58. def evaluate_moves(current_grid, current_digits, is_rows_turn):
  59. possible_moves = []
  60. for i in range(3):
  61. for j in range(3):
  62. if current_grid[i][j] == 0:
  63. for d in current_digits:
  64. new_grid = [row[:] for row in current_grid]
  65. new_grid[i][j] = d
  66. new_remaining = current_digits[:]
  67. new_remaining.remove(d)
  68. result = minimax(new_grid, new_remaining, not is_rows_turn)
  69. possible_moves.append(((i, j, d), result))
  70. return possible_moves
  71.  
  72. # Example usage
  73. if __name__ == "__main__":
  74. # Change this section to represent any current game state
  75. grid = [
  76. [0, 0, 0],
  77. [0, 0, 0],
  78. [0, 0, 0]
  79. ]
  80. used_digits = [cell for row in grid for cell in row if cell != 0]
  81. remaining = [d for d in DIGITS if d not in used_digits]
  82. is_rows_turn = True # Toggle depending on the turn
  83.  
  84. possible_moves = evaluate_moves(grid, remaining, is_rows_turn)
  85. print("Possible moves from current state:")
  86. for (i, j, d), result in sorted(possible_moves, key=lambda x: -x[1] if is_rows_turn else x[1]):
  87. result_str = "Win" if result == 1 else "Loss" if result == -1 else "Draw"
  88. print(f"Place {d} at ({i},{j}): {result_str}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement