Advertisement
FSNWRMH

Untitled

Nov 28th, 2023
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import turtle
  2. import copy
  3. import random
  4.  
  5. screen = turtle.Screen()
  6. screen.setup(800,800)
  7. screen.title("Tic Tac Toe ")
  8. screen.setworldcoordinates(-5,-5,5,5)
  9. screen.bgcolor('light gray')
  10. screen.tracer(0,0)
  11. turtle.hideturtle()
  12.  
  13. def draw_board():
  14. turtle.pencolor('green')
  15. turtle.pensize(10)
  16. turtle.up()
  17. turtle.goto(-3,-1)
  18. turtle.seth(0)
  19. turtle.down()
  20. turtle.fd(6)
  21. turtle.up()
  22. turtle.goto(-3,1)
  23. turtle.seth(0)
  24. turtle.down()
  25. turtle.fd(6)
  26. turtle.up()
  27. turtle.goto(-1,-3)
  28. turtle.seth(90)
  29. turtle.down()
  30. turtle.fd(6)
  31. turtle.up()
  32. turtle.goto(1,-3)
  33. turtle.seth(90)
  34. turtle.down()
  35. turtle.fd(6)
  36.  
  37. def draw_circle(x,y):
  38. turtle.up()
  39. turtle.goto(x,y-0.75)
  40. turtle.seth(0)
  41. turtle.color('red')
  42. turtle.down()
  43. turtle.circle(0.75, steps=100)
  44.  
  45. def draw_x(x,y):
  46. turtle.color('blue')
  47. turtle.up()
  48. turtle.goto(x-0.75,y-0.75)
  49. turtle.down()
  50. turtle.goto(x+0.75,y+0.75)
  51. turtle.up()
  52. turtle.goto(x-0.75,y+0.75)
  53. turtle.down()
  54. turtle.goto(x+0.75,y-0.75)
  55.  
  56. def draw_piece(i,j,p):
  57. if p==0: return
  58. x,y = 2*(j-1), -2*(i-1)
  59. if p==1:
  60. draw_x(x,y)
  61. else:
  62. draw_circle(x,y)
  63.  
  64. def draw(b):
  65. draw_board()
  66. for i in range(3):
  67. for j in range(3):
  68. draw_piece(i,j,b[i][j])
  69. screen.update()
  70.  
  71. # return 1 if player 1 wins, 2 if player 2 wins, 3 if tie, 0 if game is not over
  72. def gameover(b):
  73. if b[0][0]>0 and b[0][0] == b[0][1] and b[0][1] == b[0][2]: return b[0][0]
  74. if b[1][0]>0 and b[1][0] == b[1][1] and b[1][1] == b[1][2]: return b[1][0]
  75. if b[2][0]>0 and b[2][0] == b[2][1] and b[2][1] == b[2][2]: return b[2][0]
  76. if b[0][0]>0 and b[0][0] == b[1][0] and b[1][0] == b[2][0]: return b[0][0]
  77. if b[0][1]>0 and b[0][1] == b[1][1] and b[1][1] == b[2][1]: return b[0][1]
  78. if b[0][2]>0 and b[0][2] == b[1][2] and b[1][2] == b[2][2]: return b[0][2]
  79. if b[0][0]>0 and b[0][0] == b[1][1] and b[1][1] == b[2][2]: return b[0][0]
  80. if b[2][0]>0 and b[2][0] == b[1][1] and b[1][1] == b[0][2]: return b[2][0]
  81. p = 0
  82. for i in range(3):
  83. for j in range(3):
  84. p += (1 if b[i][j] > 0 else 0)
  85. if p==9: return 3
  86. else: return 0
  87.  
  88. def play(x,y):
  89. global turn
  90. if turn=='x': return
  91.  
  92. i = 3-int(y+5)//2
  93. j = int(x+5)//2 - 1
  94. if i>2 or j>2 or i<0 or j<0 or b[i][j]!=0: return
  95. if turn == 'x': b[i][j], turn = 1, 'o'
  96. else: b[i][j], turn = 2, 'x'
  97. draw(b)
  98. r = gameover(b)
  99. if r==1:
  100. screen.textinput("Game over!","X won!")
  101. elif r==2:
  102. screen.textinput("Game over!","O won!")
  103. elif r==3:
  104. screen.textinput("Game over!", "Tie!")
  105. if r>0: turtle.bye()
  106. _,move = max_node(b,-2,2)
  107. b[move[0]][move[1]] = 1
  108. draw(b)
  109. r = gameover(b)
  110. if r==1:
  111. screen.textinput("Game over!","X won!")
  112. elif r==2:
  113. screen.textinput("Game over!","O won!")
  114. elif r==3:
  115. screen.textinput("Game over!", "Tie!")
  116. if r>0: turtle.bye()
  117. turn = 'o'
  118.  
  119. b = [ [ 0,0,0 ], [ 0,0,0 ], [ 0,0,0 ] ]
  120. draw(b)
  121. turn = 'x'
  122. screen.onclick(play)
  123. #turtle.mainloop()
  124.  
  125. def max_node(b,alpha,beta):
  126. r = gameover(b)
  127. if r==1: return 1,None
  128. elif r==2: return -1,None
  129. elif r==3: return 0,None
  130.  
  131. score = -2
  132. # find all possible next moves
  133. pm = list()
  134. for i in range(3):
  135. for j in range(3):
  136. if b[i][j] == 0: pm.append((i,j))
  137. random.shuffle(pm)
  138. for (i,j) in pm:
  139. if b[i][j] == 0:
  140. nb = copy.deepcopy(b)
  141. nb[i][j] = 1
  142. cs,_ = min_node(nb,alpha,beta)
  143. if score<cs:
  144. score=cs
  145. move = (i,j)
  146. alpha = max(alpha,cs)
  147. if alpha>=beta: return score,move
  148. return score,move
  149.  
  150. def min_node(b,alpha,beta):
  151. r = gameover(b)
  152. if r==1: return 1,None
  153. elif r==2: return -1,None
  154. elif r==3: return 0,None
  155.  
  156. score = 2
  157. # find all possible next moves
  158. pm = list()
  159. random.shuffle(pm)
  160. for i in range(3):
  161. for j in range(3):
  162. if b[i][j] == 0: pm.append((i,j))
  163. for (i,j) in pm:
  164. if b[i][j] == 0:
  165. nb = copy.deepcopy(b)
  166. nb[i][j] = 2
  167. cs,_ = max_node(nb,alpha,beta)
  168. if score>cs:
  169. score=cs
  170. move = (i,j)
  171. beta = min(beta,cs)
  172. if alpha>=beta: return score,move
  173. return score,move
  174.  
  175. _,move = max_node(b,-2,2)
  176. b[move[0]][move[1]] = 1
  177. draw(b)
  178. turn = 1
  179. screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement