Advertisement
Guest User

Tic-Tac-Toe by Leo

a guest
Oct 28th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.53 KB | None | 0 0
  1. #leoleosuper's Tic-Tac-Toe
  2. import turtle
  3. import random
  4. import time
  5.  
  6. end=0
  7. TL="N"
  8. ML="N"
  9. BL="N"
  10. TM="N"
  11. MM="N"
  12. BM="N"
  13. TR="N"
  14. MR="N"
  15. BR="N"
  16. game="play"
  17. ff="Verdana",88,"bold"
  18. #Makes board
  19. def write(color):
  20.     global wn
  21.     global Leo
  22.     wn=turtle.Screen()
  23.     wn.setup(500,500)
  24.     wn.bgcolor("DeepSkyBlue")
  25.     Leo=turtle.Turtle()
  26.     Leo.pensize(15)
  27.     Leo.speed(100)
  28.     Leo.color(color)
  29.     moveTo(Leo,0,-240)
  30.     Leo.write("Tic-Tac-Toe",False,align="center",font=("Verdana",20,"bold"))
  31.     line(0,300,-175,100)
  32.     line(0,300,-175,0)
  33.     line(-90,300,-75,200)
  34.     line(0,300,25,200)
  35. #Moves turtle to (x,y)
  36. def moveTo(tur,x,y):
  37.     tur.penup()
  38.     tur.goto(x,y)
  39.     tur.pendown()
  40. #Rematch
  41. def again(re):
  42.     global game
  43.     global TL
  44.     global ML
  45.     global BL
  46.     global TM
  47.     global MM
  48.     global BM
  49.     global TR
  50.     global MR
  51.     global BR
  52.     global turn
  53.     global xyz
  54.     global end
  55.     global player
  56.     if(re=="1" or re=="y" or re=="yes" or re=="Y" or re=="Yes" or re=="one" or re=="YES"
  57.          or re=="One" or re=="ONE"):
  58.         write("forestGreen")
  59.         TL="N"
  60.         ML="N"
  61.         BL="N"
  62.         TM="N"
  63.         MM="N"
  64.         BM="N"
  65.         TR="N"
  66.         MR="N"
  67.         BR="N"
  68.         game="play"
  69.         turn = xyz
  70.         end=0
  71.     else:
  72.         player="end"
  73. #Checks if game is won by ether side, then displays so
  74. def chess(p):
  75.     global game
  76.     if (game == "win" + p):
  77.         time.sleep(4)
  78.         wn.bye()
  79.         re=input(p + " Wins! Rematch? 1/y/yes: ")
  80.         again(re)
  81. #Draws a line
  82. def line(n,m,x,y):
  83.     Leo.color("forestGreen")
  84.     Leo.left(n)
  85.     moveTo(Leo,x,y)
  86.     Leo.forward(m)
  87. #Helps the check command
  88. def helpch(K):
  89.     global TL
  90.     global ML
  91.     global BL
  92.     global TM
  93.     global MM
  94.     global BM
  95.     global TR
  96.     global MR
  97.     global BR
  98.     global game
  99.     if(TL==K):
  100.         if(ML==K and BL==K):
  101.             game="win" + K
  102.             line(0,300,-125,200)
  103.             return 1
  104.         if(MM==K and BR==K):
  105.             line(45,425,-175,200)
  106.             game="win" + K
  107.             return 1
  108.         if(TM==K and TR==K):
  109.             game="win" + K
  110.             line(90,300,-175,150)
  111.             return 1
  112.     if(TM==K):
  113.         if(MM==K and BM==K):
  114.             game="win" + K
  115.             line( 0,300,-25,200)
  116.             return 1
  117.     if(TR==K):
  118.         if(MR==K and BR==K):
  119.             game="win" + K
  120.             line(0,300,75,200)
  121.             return 1
  122.         if(MM==K and BL==K):
  123.             game="win" + K
  124.             line(-45,425,125,200)
  125.             return 1
  126.     if(ML==K):
  127.         if(MM==K and MR==K):
  128.             game="win" + K
  129.             line(90,300,-175,50)
  130.             return 1
  131.     if(BL==K):
  132.         if(BM==K and BR==K):
  133.             game="win" + K
  134.             line(90,300,-175,-50)
  135.             return 1
  136.     return 0
  137. #Checks if ether team has won
  138. def check():
  139.     global TL
  140.     global ML
  141.     global BL
  142.     global TM
  143.     global MM
  144.     global BM
  145.     global TR
  146.     global MR
  147.     global BR
  148.     global game
  149.     E = 0
  150.     E = helpch("X")
  151.     if (E==0):
  152.         E = helpch("O")
  153.     if (E==1):
  154.         return 1
  155.     if(TL is not "N" and ML is not "N" and BL is not "N" and TM is not "N" and MM is not "N"
  156.        and BM is not "N" and TR is not "N" and MR is not "N" and BR is not "N"):
  157.         game="Tie"
  158.         print("endw")
  159.         return 1
  160.     return 0
  161. #Uses Bot's turn to block enemy from winning
  162. def smartMove(y,p,e):
  163.     global TL
  164.     global ML
  165.     global BL
  166.     global TM
  167.     global MM
  168.     global BM
  169.     global TR
  170.     global MR
  171.     global BR
  172.     global itWorks
  173.     global turn
  174.     if((TL=="N" and TR==p and TM==p) or (MM==p and BR==p and TL=="N") or
  175.        (ML==p and BL==p and TL=="N")):
  176.         moveTo(Leo,-127,82)
  177.         Leo.write(p,False,align="center",font=ff)
  178.         TL=p
  179.         itWorks=1
  180.         turn=y
  181.         return 1
  182.     if((ML=="N" and TL==p and BL==p) or
  183.        (MM==p and MR==p and ML=="N")):
  184.         moveTo(Leo,-127,-18)
  185.         Leo.write(p,False,align="center",font=ff)
  186.         ML=p
  187.         itWorks=1
  188.         turn=y
  189.         return 1
  190.     if((BL=="N" and TL==p and ML==p) or (BM==p and BR==p and BL=="N") or
  191.        (MM==p and TR==p and BL=="N")):
  192.         moveTo(Leo,-127,-120)
  193.         Leo.write(p,False,align="center",font=ff)
  194.         BL=p
  195.         itWorks=1
  196.         turn=y
  197.         return 1
  198.     if((TM=="N" and TR==p and TL==p) or
  199.        (MM==p and BM==p and TM=="N")):
  200.         moveTo(Leo,-24,82)
  201.         Leo.write(p,False,align="center",font=ff)
  202.         TM=p
  203.         itWorks=1
  204.         turn=y
  205.         return 1
  206.     if((MM=="N" and TL==p and BR==p) or (MM=="N" and BL==p and TR==p) or
  207.        (MM=="N" and TM==p and BM==p) or (MM=="N" and ML==p and MR==p)):
  208.         moveTo(Leo,-24,-18)
  209.         Leo.write(p,False,align="center",font=ff)
  210.         MM=p
  211.         itWorks=1
  212.         turn=y
  213.         return 1
  214.     if((BM=="N" and BR==p and BL==p) or (MM==p and TM==p and BM=="N")):
  215.         moveTo(Leo,-24,-120)
  216.         Leo.write(p,False,align="center",font=ff)
  217.         BM=p
  218.         itWorks=1
  219.         turn=y
  220.         return 1
  221.     if((TR=="N" and TM==p and TL==p) or (MM==p and BL==p and TR=="N") or
  222.        (MR==p and BR==p and TR=="N")):
  223.         moveTo(Leo,77,82)
  224.         Leo.write(p,False,align="center",font=ff)
  225.         TR=p
  226.         itWorks=1  
  227.         turn=y
  228.         return 1
  229.     if((MR=="N" and TR==p and BR==p) or (MM==p and ML==p and MR=="N")):
  230.         moveTo(Leo,77,-18)
  231.         Leo.write(p,False,align="center",font=ff)
  232.         MR=p
  233.         itWorks=1  
  234.         turn=y
  235.         return 1
  236.     if((BR=="N" and BM==p and BL==p) or (MM==p and TL==p and BR=="N") or
  237.        (MR==p and TR==p and BR=="N")):
  238.         moveTo(Leo,77,-120)
  239.         Leo.write(p,False,align="center",font=ff)
  240.         BR=p
  241.         itWorks=1  
  242.         turn=y
  243.         return 1
  244.     if((TL=="N" and TR==e and TM==e) or (MM==e and BR==e and TL=="N") or
  245.        (ML==e and BL==e and TL=="N")):
  246.         moveTo(Leo,-127,82)
  247.         Leo.write(p,False,align="center",font=ff)
  248.         TL=p
  249.         itWorks=1
  250.         turn=y
  251.         return 1
  252.     if((ML=="N" and TL==e and BL==e) or (MM==e and MR==e and ML=="N")):
  253.         moveTo(Leo,-127,-18)
  254.         Leo.write(p,False,align="center",font=ff)
  255.         ML=p
  256.         itWorks=1
  257.         turn=y
  258.         return 1
  259.     if((BL=="N" and TL==e and ML==e) or (BM==e and BR==e and BL=="N") or
  260.        (MM==e and TR==e and BL=="N")):
  261.         moveTo(Leo,-127,-120)
  262.         Leo.write(p,False,align="center",font=ff)
  263.         BL=p
  264.         itWorks=1
  265.         turn=y
  266.         return 1
  267.     if((TM=="N" and TR==e and TL==e) or (MM==e and BM==e and TM=="N")):
  268.         moveTo(Leo,-24,82)
  269.         Leo.write(p,False,align="center",font=ff)
  270.         TM=p
  271.         itWorks=1
  272.         turn=y
  273.         return 1
  274.     if((MM=="N" and TL==e and BR==e) or (MM=="N" and BL==e and TR==e) or
  275.        (MM=="N" and TM==e and BM==e) or (MM=="N" and ML==e and MR==e)):
  276.         moveTo(Leo,-24,-18)
  277.         Leo.write(p,False,align="center",font=ff)
  278.         MM=p
  279.         itWorks=1
  280.         turn=y
  281.         return 1
  282.     if((BM=="N" and BR==e and BL==e) or (MM==e and TM==e and BM=="N")):
  283.         moveTo(Leo,-24,-120)
  284.         Leo.write(p,False,align="center",font=ff)
  285.         BM=p
  286.         itWorks=1
  287.         turn=y
  288.         return 1
  289.     if((TR=="N" and TM==e and TL==e) or (MM==e and BL==e and TR=="N") or
  290.        (MR==e and BR==e and TR=="N")):
  291.         moveTo(Leo,77,82)
  292.         Leo.write(p,False,align="center",font=ff)
  293.         TR=p
  294.         itWorks=1  
  295.         turn=y
  296.         return 1
  297.     if((MR=="N" and TR==e and BR==e) or (MM==e and ML==e and MR=="N")):
  298.         moveTo(Leo,77,-18)
  299.         Leo.write(p,False,align="center",font=ff)
  300.         MR=p
  301.         itWorks=1  
  302.         turn=y
  303.         return 1
  304.     if((BR=="N" and BM==e and BL==e) or (MM==e and TL==e and BR=="N") or
  305.        (MR==e and TR==e and BR=="N")):
  306.         moveTo(Leo,77,-120)
  307.         Leo.write(p,False,align="center",font=ff)
  308.         BR=p
  309.         itWorks=1  
  310.         turn=y
  311.         return 1
  312. #Bot's turn
  313. def CPUTurn(pla,z,p,e):
  314.     plac=pla
  315.     global itWorks
  316.     global turn
  317.     global TL
  318.     global ML
  319.     global BL
  320.     global TM
  321.     global MM
  322.     global BM
  323.     global TR
  324.     global MR
  325.     global BR
  326.     de=smartMove(z,p,e)
  327.     if (de==1):
  328.         turn = z
  329.         return z
  330.     if(plac==1 and TL == "N"):
  331.         moveTo(Leo,-127,82)
  332.         Leo.write(p,False,align="center",font=ff)
  333.         TL=p
  334.         itWorks=1
  335.         turn = z
  336.         return z
  337.     if(plac==2 and ML == "N"):
  338.         moveTo(Leo,-127,-18)
  339.         Leo.write(p,False,align="center",font=ff)
  340.         ML=p
  341.         itWorks=1
  342.         turn = z
  343.         return z
  344.     if(plac==3 and BL == "N"):
  345.         moveTo(Leo,-127,-120)
  346.         Leo.write(p,False,align="center",font=ff)
  347.         BL=p
  348.         itWorks=1
  349.         turn = z
  350.         return z
  351.     if(plac==4 and TM == "N"):
  352.         moveTo(Leo,-24,82)
  353.         Leo.write(p,False,align="center",font=ff)
  354.         TM=p
  355.         itWorks=1
  356.         turn = z
  357.         return z
  358.     if(plac==5 and MM == "N"):
  359.         moveTo(Leo,-24,-18)
  360.         Leo.write(p,False,align="center",font=ff)
  361.         MM=p
  362.         itWorks=1
  363.         turn = z
  364.         return z
  365.     if(plac==6 and BM == "N"):
  366.         moveTo(Leo,-24,-120)
  367.         Leo.write(p,False,align="center",font=ff)
  368.         BM=p
  369.         itWorks=1
  370.         turn = z
  371.         return z
  372.     if(plac==7 and TR == "N"):
  373.         moveTo(Leo,77,82)
  374.         Leo.write(p,False,align="center",font=ff)
  375.         TR=p
  376.         itWorks=1
  377.         turn = z
  378.         return z
  379.     if(plac==8 and MR == "N"):
  380.         moveTo(Leo,77,-18)
  381.         Leo.write(p,False,align="center",font=ff)
  382.         MR=p
  383.         itWorks=1
  384.         turn = z
  385.         return z
  386.     if(plac==9 and BR == "N"):
  387.         moveTo(Leo,77,-120)
  388.         Leo.write(p,False,align="center",font=ff)
  389.         BR=p
  390.         itWorks=1
  391.         turn = z
  392.         return z
  393. #Player's turn
  394. def Xmove(x,y,p,z):
  395.             global turn
  396.             global TL
  397.             global ML
  398.             global BL
  399.             global TM
  400.             global MM
  401.             global BM
  402.             global TR
  403.             global MR
  404.             global BR
  405.             if (-176<x<-75):
  406.                 if (100<y<201 and TL=="N"):
  407.                     moveTo(Leo,-127,82)
  408.                     Leo.write(p,False,align="center",font=ff)
  409.                     TL=p
  410.                     turn = z
  411.                 if (-1<y<101 and ML=="N"):
  412.                     moveTo(Leo,-127,-18)
  413.                     Leo.write(p,False,align="center",font=ff)
  414.                     ML=p
  415.                     turn = z
  416.                 if (-100<y<0 and BL=="N"):
  417.                     moveTo(Leo,-127,-118)
  418.                     Leo.write(p,False,align="center",font=ff)
  419.                     BL=p
  420.                     turn = z
  421.             elif (-76<x<25):
  422.                 if (100<y<201 and TM=="N"):
  423.                     moveTo(Leo,-24,82)
  424.                     Leo.write(p,False,align="center",font=ff)
  425.                     TM=p
  426.                     turn = z
  427.                 if (-1<y<101 and MM=="N"):
  428.                     moveTo(Leo,-24,-18)
  429.                     Leo.write(p,False,align="center",font=ff)
  430.                     MM=p
  431.                     turn = z
  432.                 if (-100<y<0 and BM=="N"):
  433.                     moveTo(Leo,-24,-118)
  434.                     Leo.write(p,False,align="center",font=ff)
  435.                     BM=p
  436.                     turn = z
  437.             elif (24<x<125):
  438.                 if (100<y<201 and TR=="N"):
  439.                     moveTo(Leo,77,82)
  440.                     Leo.write(p,False,align="center",font=ff)
  441.                     TR=p
  442.                     turn = z
  443.                 if (-1<y<101 and MR=="N"):
  444.                     moveTo(Leo,77,-18)
  445.                     Leo.write(p,False,align="center",font=ff)
  446.                     MR=p
  447.                     turn = z
  448.                 if (-100<y<0 and BR=="N"):
  449.                     moveTo(Leo,77,-118)
  450.                     Leo.write(p,False,align="center",font=ff)
  451.                     BR=p
  452.                     turn = z
  453.             else:
  454.                 print("Not in bounds.")
  455. #Main function
  456. def display(x,y):
  457.     wn.onclick(None)
  458.     global turn
  459.     global place
  460.     global game
  461.     global TL
  462.     global ML
  463.     global BL
  464.     global TM
  465.     global MM
  466.     global BM
  467.     global TR
  468.     global MR
  469.     global BR
  470.     global player
  471.     global itWorks
  472.     global end
  473.     if(player=="2"):
  474.         if (turn==1):
  475.             Leo.color("Fuchsia")
  476.             Xmove(x,y,"X",0)
  477.             check()
  478.         else:
  479.             Leo.color("SteelBlue")
  480.             Xmove(x,y,"O",1)
  481.             check()
  482.     if (player=="1"):
  483.         if (turn==1):
  484.             Leo.color("Fuchsia")
  485.             Xmove(x,y,"X",0)
  486.             end=check()
  487.         if (turn==0 and end==0):
  488.             Leo.color("SteelBlue")
  489.             itWorks=0
  490.             while (itWorks==0):
  491.                 pla=random.randint(1,9)
  492.                 turn=CPUTurn(pla,1,"O","X")
  493.             end=check()
  494.     if (player=="0"):
  495.         if (turn==1 and end==0):
  496.             Leo.color("Fuchsia")
  497.             itWorks=0
  498.             while (itWorks==0):
  499.                 pla=random.randint(1,9)
  500.                 CPUTurn(pla,0,"X","O")
  501.             end=check()
  502.         if (turn==0 and end==0):
  503.             Leo.color("SteelBlue")
  504.             itWorks=0
  505.             while (itWorks==0):
  506.                 pla=random.randint(1,9)
  507.                 CPUTurn(pla,1,"O","X")
  508.             end=check()
  509.     chess("O")
  510.     chess("X")
  511.     if (game == "Tie"):
  512.         time.sleep(3)
  513.         wn.bye()
  514.         re=input("Nobody wins. Rematch? 1/y/yes: ")
  515.         again(re)
  516.     if (player=="end"):
  517.         print("No rematch. GGnoRe")
  518.         player="end"
  519.     if (end == 0):
  520.         wn.onclick(display)
  521. #Main lines for function, starts it up.
  522. print("Single or Multiplayer?")
  523. ad=0
  524. player="2"
  525. turn=1
  526. game=0
  527. xyz=1
  528. derp=input("Everything but 1, single, singleplayer, variants thereof, or 0 will be multiplayer: ")
  529. if(derp=="0"):
  530.     player="0"
  531. if(derp=="single" or derp=="1" or derp=="one" or derp=="singleplayer" or derp=="Single" or
  532.    derp=="One" or derp=="Singleplayer" or derp=="SinglePlayer" or derp=="singlePlayer"):
  533.     player="1"
  534.     ad=input("Cpu or you first? Cpu and variants thereof will have CPU as X: ")
  535.     if(ad=="Cpu" or ad=="CPU" or ad=="cpu" or ad=="Computer" or ad=="computer"):
  536.         turn=0
  537.         xyz=0
  538. wn=turtle.Screen()
  539. Leo=turtle.Turtle()
  540. write("ForestGreen")
  541. if(ad=="Cpu" or ad=="CPU" or ad=="cpu" or ad=="Computer" or ad=="computer"):
  542.     display(-300,-300)
  543. wn.onclick(display)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement