Advertisement
Guest User

Untitled

a guest
May 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.16 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import ttk
  3.  
  4.  
  5.  
  6. root = Tk()
  7. #root.geometry('495x480')
  8. root.configure(bg="black")
  9.  
  10. style = ttk.Style()
  11. s = ttk.Style()
  12. s.configure('TButton',background = 'blue', foreground = "red"
  13. , font = ('Sans','20','bold'), relief = "blue")
  14.  
  15. s.map('TButton',foreground = [('active','black')] )
  16. s.map('TButton', background = [('active',"red")])
  17.  
  18.  
  19. userXO=" _ "
  20.  
  21. AI_XO=" _ "
  22.  
  23.  
  24.  
  25. def board(event=None):
  26.  
  27. global labelframe9
  28. global button9
  29. labelframe9 = LabelFrame(m1,bg=AI_XO_COLOR)
  30. labelframe9.pack(side=BOTTOM,fill=BOTH, expand=YES)
  31. button9 = Label(labelframe9, text=userXO, font=fontSize, bg="blue")
  32. button9.bind("<Button-1>",setMove9)
  33. button9.bind("<Enter>",setDinamicBg9)
  34. button9.bind("<Leave>",setBg9)
  35.  
  36. button9.pack(fill=BOTH,expand=YES)
  37.  
  38. #this is Key
  39. if(list[0]==" "):
  40. setMove9(event=None)
  41. setDinamicBg9(event=None)
  42. setBg9(event=None)
  43.  
  44. #####################################
  45.  
  46. global labelframe6
  47. global button6
  48. labelframe6 = LabelFrame(m1,bg="red")
  49. labelframe6.pack(fill=BOTH, expand=YES)
  50. button6 = Label(labelframe6, text=userXO, font=fontSize, bg="blue")
  51. ##
  52. button6.bind("<Button-1>",setMove6)
  53. button6.bind("<Enter>",setDinamicBg6)
  54. button6.bind("<Leave>",setBg6)
  55. button6.pack(fill=BOTH,expand=YES)
  56.  
  57. ##################################
  58.  
  59.  
  60. global labelframe3
  61. global button3
  62. labelframe3 = LabelFrame(m1,bg="red")
  63. labelframe3.pack(fill=BOTH, expand=YES)
  64. button3 = Label(labelframe3, text=userXO, font=fontSize,bg="blue")
  65. button3.pack(fill=BOTH, expand=YES)
  66.  
  67. button3.bind("<Button-1>",setMove3)
  68. button3.bind("<Enter>",setDinamicBg3)
  69. button3.bind("<Leave>",setBg3)
  70.  
  71. ###################################
  72.  
  73. global labelframe1
  74. global button1
  75.  
  76. labelframe1 = LabelFrame(m2,bg="red")
  77. labelframe1.pack(fill=BOTH, expand=YES)
  78. button1 = Label(labelframe1, text=AI_XO,font=fontSize,bg="blue")
  79. button1.pack(side=BOTTOM,fill=BOTH,expand=YES)
  80.  
  81. button1.bind("<Button-1>",setMove1)
  82. button1.bind("<Enter>",setDinamicBg1)
  83. button1.bind("<Leave>",setBg1)
  84.  
  85.  
  86. ####################################
  87.  
  88. global labelframe4
  89. global button4
  90. labelframe4 = LabelFrame(m2,bg="red")
  91. labelframe4.pack(fill=BOTH, expand=YES)
  92. button4 = Label(labelframe4, text=AI_XO,font=fontSize,bg="blue")
  93. button4.pack(side=BOTTOM,fill=BOTH,expand=YES)
  94.  
  95. button4.bind("<Button-1>",setMove4)
  96. button4.bind("<Enter>",setDinamicBg4)
  97. button4.bind("<Leave>",setBg4)
  98.  
  99. ########################################
  100.  
  101. global labelframe7
  102. global button7
  103. labelframe7 = LabelFrame(m2,bg="red")
  104. labelframe7.pack(fill=BOTH, expand=YES)
  105. button7 = Label(labelframe7, text=AI_XO,font=fontSize,bg="blue")
  106. button7.pack(side=BOTTOM,fill=BOTH,expand=YES)
  107.  
  108. button7.bind("<Button-1>",setMove7)
  109. button7.bind("<Enter>",setDinamicBg7)
  110. button7.bind("<Leave>",setBg7)
  111.  
  112. #########################################
  113.  
  114. global labelframe2
  115. global button2
  116. labelframe2 = LabelFrame(m3,bg="red")
  117. labelframe2.pack(fill=BOTH, expand=YES)
  118. button2 = Label(labelframe2, text=AI_XO,font=fontSize,bg="blue")
  119. button2.pack(side=BOTTOM,fill=BOTH,expand=YES)
  120.  
  121. button2.bind("<Button-1>",setMove2)
  122. button2.bind("<Enter>",setDinamicBg2)
  123. button2.bind("<Leave>",setBg2)
  124. #########################################
  125. global labelframe5
  126. global button5
  127. labelframe5 = LabelFrame(m3,bg="red")
  128. labelframe5.pack(fill=BOTH, expand=YES)
  129. button5 = Label(labelframe5, text=AI_XO,font=fontSize,bg="blue")
  130. button5.pack(side=BOTTOM,fill=BOTH,expand=YES)
  131.  
  132. button5.bind("<Button-1>",setMove5)
  133. button5.bind("<Enter>",setDinamicBg5)
  134. button5.bind("<Leave>",setBg5)
  135.  
  136. #########################################
  137.  
  138. global labelframe8
  139. global button8
  140. labelframe8 = LabelFrame(m3,bg="red")
  141. labelframe8.pack(fill=BOTH, expand=YES)
  142. button8 = Label(labelframe8, text=AI_XO,font=fontSize,bg="blue")
  143. button8.pack(side=BOTTOM,fill=BOTH,expand=YES)
  144.  
  145. button8.bind("<Button-1>",setMove8)
  146. button8.bind("<Enter>",setDinamicBg8)
  147. button8.bind("<Leave>",setBg8)
  148.  
  149.  
  150.  
  151.  
  152. def setUserOX(XO):
  153. global userXO
  154. userXO=XO
  155.  
  156.  
  157. def setAI_OX(XO):
  158. global AI_XO
  159. AI_XO=XO
  160.  
  161. def remMainMenue():
  162. play.destroy()
  163. exitMenue.destroy()
  164. global selectXO
  165. selectXO()
  166. board()
  167.  
  168.  
  169.  
  170. def mainMenue():
  171. container=PanedWindow()
  172. container.pack(side=TOP,fill=BOTH,expand=YES)
  173.  
  174. global board
  175. global play
  176. global exitMenue
  177. play=ttk.Button(container,command=remMainMenue,text="play")
  178. play.pack(side=LEFT,fill=BOTH,expand=YES)
  179.  
  180. exitMenue=ttk.Button(container,command=root.destroy,text="exit")
  181. exitMenue.pack(side=LEFT,fill=BOTH,expand=YES)
  182.  
  183.  
  184.  
  185.  
  186.  
  187. def selectXO():
  188. global container
  189. global setO
  190. global setX
  191. global O
  192. global X
  193. container=PanedWindow()
  194. container.pack(side=TOP,fill=BOTH,expand=YES)
  195.  
  196. O=ttk.Button(container,text="O")
  197. O.pack(side=LEFT,fill=BOTH,expand=YES)
  198.  
  199. X=ttk.Button(container,text="X")
  200. X.pack(side=LEFT,fill=BOTH,expand=YES)
  201.  
  202. O.bind("<Button-1>",setO)
  203. X.bind("<Button-1>",setX)
  204.  
  205.  
  206.  
  207.  
  208. def restart1():
  209. global reset
  210. global restart
  211. global exit
  212. restart=ttk.Button(container,command=reset,text="restart")
  213. restart.pack(side=LEFT,fill=BOTH,expand=YES)
  214. exit=ttk.Button(container,command=root.destroy,text="quit")
  215. exit.pack(side=LEFT,fill=BOTH,expand=YES)
  216.  
  217.  
  218.  
  219. def exit():
  220. root.destroy()
  221.  
  222.  
  223. def setO(self):
  224.  
  225.  
  226. setAI_OX(" X ")
  227. setUserOX(" O ")
  228.  
  229. O.destroy()
  230. X.destroy()
  231.  
  232. global restart1
  233. restart1()
  234.  
  235. def setX(slef):
  236. setAI_OX(" O ")
  237. setUserOX(" X ")
  238.  
  239. O.destroy()
  240. X.destroy()
  241.  
  242.  
  243.  
  244. global restart1
  245. restart1()
  246.  
  247.  
  248. def reset(event=None):
  249. global userXO
  250. userXO=" _ "
  251. global AI_XO
  252. AI_XO=" _ "
  253. button9.config(text=userXO)
  254. button8.config(text=userXO)
  255. button7.config(text=userXO)
  256. button6.config(text=userXO)
  257. button5.config(text=userXO)
  258. button4.config(text=userXO)
  259. button3.config(text=userXO)
  260. button2.config(text=userXO)
  261. button1.config(text=userXO)
  262.  
  263.  
  264.  
  265.  
  266. global restart1
  267.  
  268. restart.destroy()
  269. exit.destroy()
  270. board()
  271.  
  272. X=ttk.Button(container,command=restart1,text="X")
  273. X.pack(side=LEFT,fill=BOTH,expand=YES)
  274. O=ttk.Button(container,command=root.destroy,text="O")
  275. O.pack(side=LEFT,fill=BOTH,expand=YES)
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. AI_XO_COLOR="black"
  293.  
  294. fontSize="size 80"
  295. dinamicColorBg="red"
  296.  
  297. list=[" "," "," "," "," "," "," "," "," "]
  298.  
  299.  
  300.  
  301. m1 = PanedWindow()
  302. m1.pack(side=RIGHT,fill=BOTH, expand=YES)
  303.  
  304. m2 = PanedWindow()
  305. m2.pack(side=LEFT,fill=BOTH, expand=YES)
  306.  
  307.  
  308. m3 = PanedWindow()
  309. m3.pack(side=LEFT,fill=BOTH, expand=YES)
  310.  
  311. from random import randrange,uniform
  312.  
  313. ai=randrange(0,9)
  314. while(list[ai]=="X" and list[ai]=="O"):
  315. ai=randrange(0,9)
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. ##########################################
  324.  
  325.  
  326. def setMove9(event=None):
  327. list[8]=userXO
  328. button9.config(text=userXO)
  329. button9.config(bg=dinamicColorBg)
  330. def setDinamicBg9(event=None):
  331. button9.config(fg="black")
  332. button9.config(bg="red")
  333. def setBg9(event=None):
  334. button9.config(bg="blue")
  335. ###########################################
  336.  
  337. def setMove7(self):
  338. list[6]=userXO
  339. button7.config(text=userXO)
  340. button7.config(bg="red")
  341. def setDinamicBg7(event=None):
  342. button7.config(fg="black")
  343. button7.config(bg="red")
  344. def setBg7(event=None):
  345. button7.config(bg="blue")
  346. ############################################
  347.  
  348. def setMove6(self):
  349. list[5]=userXO
  350. button6.config(text=userXO)
  351. button6.config(bg="red")
  352. def setDinamicBg6(event=None):
  353. button6.config(fg="black")
  354. button6.config(bg="red")
  355. def setBg6(event=None):
  356. button6.config(bg="blue")
  357.  
  358. ##################################
  359.  
  360. def setMove3(self):
  361. list[2]=userXO
  362. button3.config(text=userXO)
  363. button3.config(bg="red")
  364. def setDinamicBg3(event=None):
  365. button3.config(fg="black")
  366. button3.config(bg="red")
  367. def setBg3(event=None):
  368. button3.config(bg="blue")
  369.  
  370. ###################################
  371. def setMove1(self):
  372. list[0]=userXO
  373. button1.config(text=userXO)
  374. button1.config(bg="red")
  375. def setDinamicBg1(event=None):
  376. button1.config(fg="black")
  377. button1.config(bg="red")
  378. def setBg1(event=None):
  379. button1.config(bg="blue")
  380.  
  381. ####################################
  382. def setMove4(self):
  383. list[3]=userXO
  384. button4.config(text=userXO)
  385. button4.config(bg="red")
  386. def setDinamicBg4(event=None):
  387. button4.config(fg="black")
  388. button4.config(bg="red")
  389. def setBg4(event=None):
  390. button4.config(bg="blue")
  391.  
  392. ####################################
  393. def setMove2(self):
  394. list[1]=userXO
  395. button2.config(text=userXO)
  396. button2.config(bg="red")
  397. def setDinamicBg2(event=None):
  398. button2.config(fg="black")
  399. button2.config(bg="red")
  400. def setBg2(event=None):
  401. button2.config(bg="blue")
  402. ####################################
  403. def setMove5(self):
  404. list[4]=userXO
  405. button5.config(text=userXO)
  406. button5.config(bg="red")
  407. def setDinamicBg5(event=None):
  408. button5.config(fg="black")
  409. button5.config(bg="red")
  410. def setBg5(event=None):
  411. button5.config(bg="blue")
  412.  
  413. #########################################
  414.  
  415. def setMove8(self):
  416. list[7]=userXO
  417. button8.config(text=userXO)
  418. button8.config(bg="red")
  419. def setDinamicBg8(event=None):
  420. button8.config(fg="black")
  421. button8.config(bg="red")
  422. def setBg8(event=None):
  423. button8.config(bg="blue")
  424.  
  425.  
  426. mainMenue()
  427.  
  428. """""
  429.  
  430.  
  431.  
  432. #display instructions for the player
  433. print("The object of Tic Tac Toe is to get three in a row. \n"
  434. "You play on a three by three game board. \n"
  435. "The first player is known as X and the second is O. \n"
  436. "Players alternate placing Xs and Os on the game board until\n"
  437.  
  438. "either opponent has three in a row or all nine squares are filled.\n")
  439.  
  440. move=[0,1,2,3,4,5,6,7,8]
  441.  
  442.  
  443. #function that prints board
  444. def board():
  445.  
  446. print("::======:=======:======::")
  447. print("|| ",move[0]," | ",move[1]," | ",move[2]," ||")
  448. print("::------|-------|------::")
  449. print("|| ",move[3]," | ",move[4]," | ",move[5]," ||")
  450. print("::------|-------|------::")
  451. print("|| ",move[6]," | ",move[7]," | ",move[8]," ||")
  452. print("::======:=======:======::")
  453. print("\n\n")
  454. #function that clears the board
  455. def resetBoard():
  456. for i in range(0,9):
  457. move[i]=" "
  458.  
  459. userXO=" "
  460. AI_XO=" "
  461.  
  462. #function that makes a random selection of X or O for the user
  463. from random import randrange,uniform
  464.  
  465. print("Do you want to be X or O?:")
  466. playerXO=input()
  467.  
  468. playerXO=str(playerXO)
  469.  
  470. xrand=randrange(0,2)
  471. if(playerXO=="X" or playerXO=="x"):
  472. userXO="X"
  473. AI_XO="O"
  474. elif(playerXO=="O" or playerXO=="o"):
  475. userXO="O"
  476. AI_XO="X"
  477. else:
  478. while(playerXO!="X" or playerXO!="x" or playerXO!="O" or playerXO!="o"):
  479. print("Wrong input\nDo you want to be X or O?:")
  480. playerXO=input()
  481. playerXO=str(playerXO)
  482. if(playerXO=="X" or playerXO=="x"):
  483. userXO="X"
  484. AI_XO="O"
  485. break
  486. elif(playerXO=="O" or playerXO=="o"):
  487. userXO="O"
  488. AI_XO="X"
  489. break
  490.  
  491. # function that sets moves
  492. def set(setMove,XO):
  493. move[setMove]=XO
  494.  
  495. #function that sets user's move
  496. def userMove():
  497.  
  498.  
  499. print("Please select a move for "+userXO+":")
  500. user=input()
  501. user=int(user)
  502.  
  503. if(move[user]=="X" or move[user]=="O"):
  504.  
  505. while(move[user]=="X" or move[user]=="O"):
  506. print("Move not available please select another move for ",userXO,":")
  507. user=input()
  508. user=int(user)
  509. set(user,userXO)
  510.  
  511. else:
  512. set(user,userXO)
  513.  
  514.  
  515.  
  516. def checkForWinner():
  517.  
  518. if(move[0]==AI_XO and move[1]==AI_XO and move[2]==AI_XO):
  519. resetBoard()
  520. set(0,AI_XO)
  521. set(1,AI_XO)
  522. set(2,AI_XO)
  523. return 1
  524. elif(move[3]==AI_XO and move[4]==AI_XO and move[5]==AI_XO ):
  525. resetBoard()
  526. set(3,AI_XO)
  527. set(4,AI_XO)
  528. set(5,AI_XO)
  529. return 1
  530. elif(move[6]==AI_XO and move[7]==AI_XO and move[8]==AI_XO ):
  531. resetBoard()
  532. set(6,AI_XO)
  533. set(7,AI_XO)
  534. set(8,AI_XO)
  535. return 1
  536. elif(move[0]==AI_XO and move[3]==AI_XO and move[6]==AI_XO ):
  537.  
  538. resetBoard()
  539. set(0,AI_XO)
  540. set(3,AI_XO)
  541. set(6,AI_XO)
  542. return 1
  543. elif(move[1]==AI_XO and move[4]==AI_XO and move[7]==AI_XO ):
  544. resetBoard()
  545. set(1,AI_XO)
  546. set(4,AI_XO)
  547. set(7,AI_XO)
  548. return 1
  549. elif(move[2]==AI_XO and move[5]==AI_XO and move[8]==AI_XO ):
  550. resetBoard()
  551. set(2,AI_XO)
  552. set(5,AI_XO)
  553. set(8,AI_XO)
  554. return 1
  555. elif(move[0]==AI_XO and move[4]==AI_XO and move[8]==AI_XO ):
  556. resetBoard()
  557. set(0,AI_XO)
  558. set(4,AI_XO)
  559. set(8,AI_XO)
  560. return 1
  561. elif(move[6]==AI_XO and move[4]==AI_XO and move[2]==AI_XO ):
  562. resetBoard()
  563. set(6,AI_XO)
  564. set(4,AI_XO)
  565. set(2,AI_XO)
  566. return 1
  567.  
  568.  
  569. elif(move[0]==userXO and move[1]==userXO and move[2]==userXO):
  570. resetBoard()
  571. set(0,userXO)
  572. set(1,userXO)
  573. set(2,userXO)
  574. return -1
  575.  
  576. elif(move[3]==userXO and move[4]==userXO and move[5]==userXO ):
  577. resetBoard()
  578. set(3,userXO)
  579. set(4,userXO)
  580. set(5,userXO)
  581. return -1
  582. elif(move[6]==userXO and move[7]==userXO and move[8]==userXO ):
  583. resetBoard()
  584. set(6,userXO)
  585. set(7,userXO)
  586. set(8,userXO)
  587. return -1
  588. elif(move[0]==userXO and move[3]==userXO and move[6]==userXO ):
  589. resetBoard()
  590. set(0,userXO)
  591. set(3,userXO)
  592. set(6,userXO)
  593. return -1
  594. elif(move[1]==userXO and move[4]==userXO and move[7]==userXO ):
  595. resetBoard()
  596. set(1,userXO)
  597. set(4,userXO)
  598. set(7,userXO)
  599. return -1
  600. elif(move[2]==userXO and move[5]==userXO and move[8]==userXO ):
  601. resetBoard()
  602. set(2,userXO)
  603. set(5,userXO)
  604. set(8,userXO)
  605. return -1
  606. elif(move[0]==userXO and move[4]==userXO and move[8]==userXO ):
  607. resetBoard()
  608. set(0,userXO)
  609. set(4,userXO)
  610. set(8,userXO)
  611. return -1
  612. elif(move[6]==userXO and move[4]==userXO and move[2]==userXO ):
  613. resetBoard()
  614. set(6,userXO)
  615. set(4,userXO)
  616. set(2,userXO)
  617. return -1
  618.  
  619. else:
  620. return 0
  621.  
  622.  
  623. def chechIfBoardIsFull():
  624.  
  625. checkBoard=1
  626. for i in range(0,9):
  627. if(move[i]==" "):
  628. checkBoard=0
  629.  
  630. return checkBoard
  631.  
  632.  
  633.  
  634.  
  635. #function that sets AI's move
  636. def AI_Move():
  637.  
  638.  
  639.  
  640. ai=randrange(0,9)
  641. winner=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[6,4,2]]
  642.  
  643. ### best move across the top
  644.  
  645. if(move[0]==AI_XO and move[1]==AI_XO and move[2]!=userXO):
  646.  
  647.  
  648. set(2,AI_XO)
  649. elif(move[2]==AI_XO and move[1]==AI_XO and move[0]!=userXO):
  650. set(0,AI_XO)
  651. elif(move[0]==AI_XO and move[2]==AI_XO and move[1]!=userXO):
  652. set(1,AI_XO)
  653.  
  654. ## best move across the middle
  655. elif(move[3]==AI_XO and move[4]==AI_XO and move[5]!=userXO):
  656. set(5,AI_XO)
  657.  
  658. elif(move[5]==AI_XO and move[4]==AI_XO and move[3]!=userXO):
  659. set(3,AI_XO)
  660.  
  661. elif(move[3]==AI_XO and move[5]==AI_XO and move[4]!=userXO):
  662. set(4,AI_XO)
  663.  
  664. ## best move across the bottom
  665.  
  666. elif(move[6]==AI_XO and move[7]==AI_XO and move[8]!=userXO):
  667. set(7,AI_XO)
  668.  
  669. elif(move[8]==AI_XO and move[7]==AI_XO and move[6]!=userXO):
  670. set(6,AI_XO)
  671.  
  672. elif(move[6]==AI_XO and move[8]==AI_XO and move[7]!=userXO):
  673. set(7,AI_XO)
  674.  
  675. ### best move down the right
  676. elif(move[0]==AI_XO and move[3]==AI_XO and move[6]!=userXO):
  677. set(6,AI_XO)
  678.  
  679. elif(move[6]==AI_XO and move[3]==AI_XO and move[0]!=userXO):
  680. set(0,AI_XO)
  681.  
  682. elif(move[0]==AI_XO and move[6]==AI_XO and move[3]!=userXO):
  683. set(3,AI_XO)
  684.  
  685. ### best move down the middle
  686. elif(move[1]==AI_XO and move[4]==AI_XO and move[7]!=userXO):
  687. set(7,AI_XO)
  688.  
  689. elif(move[7]==AI_XO and move[4]==AI_XO and move[1]!=userXO):
  690. set(1,AI_XO)
  691.  
  692. elif(move[1]==AI_XO and move[7]==AI_XO and move[4]!=userXO):
  693. set(4,AI_XO)
  694.  
  695.  
  696. ### best move down the left
  697. elif(move[2]==AI_XO and move[5]==AI_XO and move[8]!=userXO):
  698. set(8,AI_XO)
  699.  
  700. elif(move[8]==AI_XO and move[5]==AI_XO and move[2]!=userXO):
  701. set(2,AI_XO)
  702.  
  703. elif(move[2]==AI_XO and move[8]==AI_XO and move[5]!=userXO):
  704. set(5,AI_XO)
  705.  
  706.  
  707. ## best move diagonal
  708.  
  709. elif(move[0]==AI_XO and move[4]==AI_XO and move[8]!=userXO):
  710. set(8,AI_XO)
  711.  
  712. elif(move[8]==AI_XO and move[4]==AI_XO and move[0]!=userXO):
  713. set(0,AI_XO)
  714.  
  715. elif(move[0]==AI_XO and move[8]==AI_XO and move[4]!=userXO):
  716. set(4,AI_XO)
  717.  
  718.  
  719. ## best move diagonal
  720.  
  721. elif(move[2]==AI_XO and move[4]==AI_XO and move[6]!=userXO):
  722. set(6,AI_XO)
  723.  
  724. elif(move[6]==AI_XO and move[4]==AI_XO and move[2]!=userXO):
  725. set(2,AI_XO)
  726.  
  727. elif(move[6]==AI_XO and move[2]==AI_XO and move[4]!=userXO):
  728. set(4,AI_XO)
  729.  
  730.  
  731. ### blocks across the top
  732. elif(move[0]==userXO and move[1]==userXO and move[2]!=userXO and move[2]!=AI_XO):
  733. set(2,AI_XO)
  734.  
  735. elif(move[2]==userXO and move[1]==userXO and move[0]!=userXO and move[0]!=AI_XO):
  736. set(0,AI_XO)
  737.  
  738. elif(move[0]==userXO and move[2]==userXO and move[1]!=userXO and move[1]!=AI_XO):
  739. set(1,AI_XO)
  740.  
  741. ### blocks across the middle
  742. elif(move[3]==userXO and move[4]==userXO and move[5]!=userXO and move[5]!=AI_XO):
  743. set(5,AI_XO)
  744. ###
  745. elif(move[5]==userXO and move[4]==userXO and move[3]!=userXO and move[3]!=AI_XO):
  746. set(3,AI_XO)
  747.  
  748. elif(move[3]==userXO and move[5]==userXO and move[4]!=userXO and move[4]!=AI_XO):
  749. set(4,AI_XO)
  750.  
  751.  
  752. ### blocks across the bottom
  753. elif(move[6]==userXO and move[7]==userXO and move[8]!=userXO and move[8]!=AI_XO):
  754. set(8,AI_XO)
  755.  
  756.  
  757. elif(move[8]==userXO and move[7]==userXO and move[6]!=userXO and move[6]!=AI_XO):
  758. set(6,AI_XO)
  759.  
  760. elif(move[6]==userXO and move[8]==userXO and move[7]!=userXO and move[7]!=AI_XO):
  761. set(7,AI_XO)
  762.  
  763.  
  764. ### blocks down the right
  765.  
  766. elif(move[0]==userXO and move[3]==userXO and move[6]!=userXO and move[6]!=AI_XO):
  767. set(6,AI_XO)
  768.  
  769. elif(move[6]==userXO and move[3]==userXO and move[0]!=userXO and move[0]!=AI_XO):
  770. set(0,AI_XO)
  771.  
  772.  
  773. elif(move[0]==userXO and move[6]==userXO and move[3]!=userXO and move[3]!=AI_XO):
  774. set(3,AI_XO)
  775.  
  776.  
  777. ### blocks down the middle
  778.  
  779. elif(move[1]==userXO and move[4]==userXO and move[7]!=userXO and move[7]!=AI_XO):
  780. set(7,AI_XO)
  781.  
  782. elif(move[7]==userXO and move[4]==userXO and move[1]!=userXO and move[1]!=AI_XO):
  783. set(1,AI_XO)
  784.  
  785.  
  786. elif(move[1]==userXO and move[7]==userXO and move[4]!=userXO and move[4]!=AI_XO):
  787. set(4,AI_XO)
  788.  
  789. ### blocks down the left
  790.  
  791. elif(move[2]==userXO and move[5]==userXO and move[8]!=userXO and move[8]!=AI_XO):
  792. set(8,AI_XO)
  793.  
  794. elif(move[8]==userXO and move[5]==userXO and move[2]!=userXO and move[2]!=AI_XO):
  795. set(2,AI_XO)
  796.  
  797. elif(move[8]==userXO and move[2]==userXO and move[5]!=userXO and move[5]!=AI_XO):
  798. set(5,AI_XO)
  799.  
  800. ### blocks diagonal
  801.  
  802. elif(move[0]==userXO and move[4]==userXO and move[8]!=userXO and move[8]!=AI_XO):
  803. set(8,AI_XO)
  804.  
  805. elif(move[8]==userXO and move[4]==userXO and move[0]!=userXO and move[0]!=AI_XO):
  806. set(0,AI_XO)
  807.  
  808. elif(move[8]==userXO and move[0]==userXO and move[4]!=userXO and move[4]!=AI_XO):
  809. set(4,AI_XO)
  810.  
  811. ### blocks diagonal
  812. elif(move[2]==userXO and move[4]==userXO and move[6]!=userXO and move[6]!=AI_XO):
  813. set(6,AI_XO)
  814. elif(move[6]==userXO and move[4]==userXO and move[2]!=userXO and move[2]!=AI_XO):
  815. set(2,AI_XO)
  816. elif(move[6]==userXO and move[2]==userXO and move[4]!=userXO and move[4]!=AI_XO):
  817. set(4,AI_XO)
  818.  
  819. ### generates random move for AI
  820. elif(move[ai]=="X" or move[ai]=="O"):
  821. while(move[ai]=="X" or move[ai]=="O"):
  822. ai=randrange(0,9)
  823. if(chechIfBoardIsFull()==1):
  824. break
  825. set(ai,AI_XO)
  826. else:
  827. set(ai,AI_XO)
  828.  
  829.  
  830. ## function that class user and AI move and look for a winner or for a tie
  831.  
  832. def user_AI_move():
  833. board()
  834. resetBoard()
  835.  
  836. for i in range(0,8):
  837. if(userXO=="X"):
  838. userMove()
  839. AI_Move()
  840. board()
  841. if(checkForWinner()==1):
  842. print("\'"+AI_XO+"\'"+"is the winner")
  843. print("Computer won!")
  844. board()
  845. break
  846. elif(checkForWinner()==-1):
  847. print("\'"+userXO+"\'"+"is the winner!")
  848. print("You won!")
  849. board()
  850. break
  851. elif(chechIfBoardIsFull()==1):
  852. print("DRAW!")
  853. break
  854.  
  855. else:
  856.  
  857. AI_Move()
  858. board()
  859.  
  860. if(checkForWinner()==1):
  861. print("\'"+AI_XO+"\'"+"is the winner")
  862. print("Computer won!")
  863. board()
  864. break
  865. elif(checkForWinner()==-1):
  866. print("\'"+userXO+"\'"+"is the winner!")
  867. print("You won!")
  868. board()
  869. break
  870. elif(chechIfBoardIsFull()==1):
  871. print("DRAW!")
  872. break
  873.  
  874. userMove()
  875.  
  876. while True:
  877. try:
  878. user_AI_move()
  879. break
  880. except ValueError:
  881. print("Oops! Wrong input. Try again...")
  882. print("Board will be reset. Start over..." )
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894. """
  895.  
  896. root.wait_window()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement