Advertisement
SigmaBoy456

Python MiniMax AI (Easy Mode AI)

Feb 21st, 2025
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. import math
  2. from random import randint
  3. #Setup Variable
  4. Turn = None
  5. AI = None
  6. Player = None
  7. PrevPlayed = None
  8. Board = [
  9. [1, 2, 3],
  10. [4, 5, 6],
  11. [7, 8, 9]
  12. ]
  13. VaildTeam = ["O", "X"]
  14. # Game Function
  15. def DrawBoard():
  16. global Board
  17. print("-"*13)
  18. for i in range(3):
  19. print(f"| {Board[i][0]} | {Board[i][1]} | {Board[i][2]} |")
  20. print("-"*13)
  21. def ChooseTeam():
  22. global Player, AI, VaildTeam
  23. while True:
  24. PlayerInput = input("Choose You Team O or X : ").upper()
  25. if PlayerInput in VaildTeam:
  26. Player = PlayerInput
  27. AI = "O" if Player=="X" else "O"
  28. break
  29. else:
  30. print("Invaild Team Choose Again!")
  31. continue
  32.  
  33. def StartTurn():
  34. global Turn
  35. n=randint(1,2)
  36. Turn = Player if n==1 else AI
  37.  
  38. def IsAbleToFill(row:int, col:int)-> bool:
  39. global Board
  40. return type(Board[row][col]) == int
  41.  
  42. def IsTie() -> bool:
  43. global Board
  44. return all([ type(Board[i][j]) != int for i in range(3) for j in range(3)])
  45.  
  46. def IsGameOver() -> bool:
  47. global Board
  48. for i in range(3):
  49. # Left or Right
  50. if Board[i][0] == Board[i][1] and Board[i][1] == Board[i][2]:
  51. return True
  52. #Up and Down
  53. elif Board[0][i] == Board[1][i] and Board[1][i] == Board[2][i]:
  54. return True
  55. # More
  56. if Board[0][0] == Board[1][1] and Board[1][1] == Board[2][2]:
  57. return True
  58. elif Board[0][2] == Board[1][1] and Board[1][1] == Board[2][0]:
  59. return True
  60. return False
  61.  
  62. def PlayerFill(row : int, col : int):
  63. global Player, Board
  64. if IsAbleToFill(row, col):
  65. Board[row][col] = Player
  66. else:
  67. return
  68.  
  69. def AutoPlayRound():
  70. global Turn, Player, AI, Board, PrevPlayed
  71. # AutoPlay Function
  72. def PlayerPlay():
  73. global Board, AI, Player, PrevPlayed, Turn
  74. row, col= None, None
  75. while True:
  76. PlayerInput = int(input("Choose Board to Fill in 1-9 : "))
  77. if PlayerInput > 9:
  78. print("Invaild Input Try Again")
  79. continue
  80. if PlayerInput <= 3:
  81. row, col = 0, PlayerInput-1
  82. if not IsAbleToFill(row, col):
  83. print("Board Already Filled Choose Another")
  84. continue
  85. PlayerFill(row, col)
  86. break
  87. elif PlayerInput <= 6:
  88. row, col = 1, PlayerInput-4
  89. if not IsAbleToFill(row, col):
  90. print("Board Already Filled Choose Another")
  91. continue
  92. PlayerFill(row, col)
  93. break
  94. elif PlayerInput <= 9:
  95. row, col = 2, PlayerInput-7
  96. if not IsAbleToFill(row, col):
  97. print("Board Already Filled Choose Another")
  98. continue
  99. PlayerFill(row, col)
  100. break
  101. PrevPlayed = Player
  102. Turn = AI
  103.  
  104. def AIPlay():
  105. global Board, AI, Player, PrevPlayed, Turn
  106. print("AI is Thinking and Choosing...")
  107. AIFill()
  108. PrevPlayed = AI
  109. Turn = Player
  110. if Turn == Player:
  111. PlayerPlay()
  112. else:
  113. AIPlay()
  114. #AI MiniMax Function
  115. def MiniMaxCheckWinner():
  116. global Board
  117. for i in range(3):
  118. # Left or Right
  119. if Board[i][0] == Board[i][1] and Board[i][1] == Board[i][2]:
  120. return Board[i][0]
  121. #Up and Down
  122. elif Board[0][i] == Board[1][i] and Board[1][i] == Board[2][i]:
  123. return Board[0][i]
  124. # More
  125. if Board[0][0] == Board[1][1] and Board[1][1] == Board[2][2]:
  126. return Board[0][0]
  127. elif Board[0][2] == Board[1][1] and Board[1][1] == Board[2][0]:
  128. return Board[0][2]
  129. return None
  130.  
  131. def MiniMax(ismaxing : bool) -> int:
  132. global AI, Player, Board
  133. if MiniMaxCheckWinner() == AI: return 1
  134. if MiniMaxCheckWinner() == Player: return -1
  135. if IsTie(): return 0
  136. # Minimax Main Block Code
  137. if ismaxing:
  138. maxscore = -math.inf
  139. for i in range(3):
  140. for j in range(3):
  141. if IsAbleToFill(i, j):
  142. temp = Board[i][j]
  143. Board[i][j] = AI
  144. score = MiniMax(False)
  145. if score > maxscore: maxscore = score
  146. Board[i][j] = temp
  147. return maxscore
  148. else:
  149. minscore = math.inf
  150. for i in range(3):
  151. for j in range(3):
  152. if IsAbleToFill(i, j):
  153. temp = Board[i][j]
  154. Board[i][j] = AI
  155. score = MiniMax(True)
  156. if score < minscore: minscore = score
  157. Board[i][j] = temp
  158. return minscore
  159.  
  160. def AIFill():
  161. global AI, Player
  162. #Setup Variable for AI
  163. bestscore = -math.inf
  164. bestfill = None
  165. for i in range(3):
  166. for j in range(3):
  167. if IsAbleToFill(i, j):
  168. score = MiniMax(False)
  169. if score > bestscore:
  170. bestscore = score
  171. bestfill = (i, j)
  172. # AI Fill Here
  173. Board[bestfill[0]][bestfill[1]] = AI
  174. # Main Game Code
  175.  
  176. ChooseTeam()
  177. StartTurn()
  178. while True:
  179. DrawBoard()
  180. if IsGameOver():
  181. print(f"Game Over Winner : {PrevPlayed}")
  182. break
  183. if IsTie():
  184. print("Game Tie!")
  185. break
  186. AutoPlayRound()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement