ArfIsAToe

Python project TicTacToe

May 3rd, 2021 (edited)
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | None | 0 0
  1. from tkinter import *
  2. import tkinter as tk
  3. from tkinter import messagebox
  4.  
  5.  
  6.  
  7. # checking if one of the players won
  8. def checkForWin(mat,l,c,xo):
  9.     # checking main diagonal
  10.        
  11.     prevl=l-1 if l>0 else 2
  12.     prevc=c-1 if c>0 else 2
  13.     nextl=l+1 if l<2 else 0
  14.     nextc=c+1 if c<2 else 0
  15.  
  16.     if (l==c):
  17.         if (mat[prevl][prevc]+mat[nextl][nextc]+mat[l][c]==xo*3):
  18.             return TRUE
  19.     # checking the opposite diagonal
  20.     elif (l+c==2):
  21.         if (mat[prevl][nextc]+mat[nextl][prevc]+mat[l][c]==xo*3):
  22.             return TRUE  
  23.     #checking lines and columns
  24.     if(mat[prevl][c]+mat[nextl][c]+mat[l][c]==xo*3 or mat[l][prevc]+mat[l][nextc]+mat[l][c]==xo*3):
  25.         return TRUE
  26.     else:
  27.         return FALSE
  28.    
  29.    
  30.  
  31. # Defining the functions for the buttons
  32. def buttonClicked(text,button,l,c):
  33.     global x, numOfButtonsDisabled, stopPlaying, firstChar, secondChar
  34.     x+=1
  35.     choiceButton["state"]= DISABLED
  36.     choiceButton["bg"]="#404040"
  37.     choiceButton["fg"]="#000000"
  38.  
  39.     button["state"]= DISABLED
  40.     #button["relief"]= "sunken"
  41.     if (x%2==0):        
  42.         button["bg"]="#dbf9db"
  43.  
  44.         text.set(firstChar)
  45.         mat[l-1][c]=firstChar
  46.         #print(checkForWin(mat,l-1,c,"X"))
  47.         if checkForWin(mat,l-1,c,firstChar):
  48.             box = messagebox.askquestion("Player 2 Won", "Would You Like To Keep Playing?")
  49.             if (box !='yes'):
  50.                 #here i want to call RESTART
  51.                 #print("stoppppppppppppppppp playing  ",stopPlaying)
  52.                 stopPlaying=TRUE
  53.                 #print("saha :)")            
  54.             root.destroy()      
  55.     else:
  56.         button["bg"]="#f2ffc2"
  57.         text.set(secondChar)
  58.         mat[l-1][c]=secondChar
  59.         #print(checkForWin(mat,l-1,c,"O"))  
  60.         if checkForWin(mat,l-1,c,secondChar):
  61.             box=messagebox.askquestion("Player 1 Won", "Would You Like To Keep Playing?")
  62.             if (box!='yes'):
  63.                 #here i want to call RESTART
  64.                 stopPlaying=TRUE
  65.                 #print("saha :)")                
  66.             root.destroy()  
  67.  
  68.     numOfButtonsDisabled+=1
  69.     if numOfButtonsDisabled==9:
  70.         box=messagebox.askquestion("It's a TIE ", "Would You Like To Keep Playing?")
  71.         if (box!='yes'):
  72.             stopPlaying=TRUE
  73.         root.destroy()
  74.     #print(mat)
  75.  
  76. #picking what to fill the buttons with
  77. def changeText(text,button):
  78.     global firstChar, secondChar, charList, charNumber
  79.     charNumber= charNumber+1 if charNumber<2 else 0
  80.     firstChar=charList[charNumber][0]
  81.     secondChar=charList[charNumber][1]
  82.     text.set(charList[charNumber][2])  
  83.  
  84.  
  85. """--------------------------Main--------------------------"""    
  86. global stopPlaying
  87. stopPlaying=FALSE
  88. while (stopPlaying==FALSE):
  89.     #print("stop playing  ",stopPlaying)
  90.    
  91.     charNumber=0
  92.     charList=[["X","O","X O"],["-","+","- +"],[""," ","Only Color"]]
  93.     mat=[["."]*3,["."]*3,["."]*3]
  94.     numOfButtonsDisabled=0
  95.        
  96.     root = tk.Tk()
  97.     root.title("Tic Tac toe")
  98.     #root.geometry("1000x1000")
  99.  
  100.     # defining buttons                   and                      putting them on a grid
  101.  
  102.     global x, firstChar, secondChar
  103.     firstChar="X"
  104.     secondChar="O"    
  105.     x=0
  106.     text0  = tk.StringVar()
  107.     text0.set("Click To Pick Variant")
  108.  
  109.     text1  = tk.StringVar()
  110.     text2  = tk.StringVar()
  111.     text3  = tk.StringVar()
  112.  
  113.     text4  = tk.StringVar()
  114.     text5  = tk.StringVar()
  115.     text6  = tk.StringVar()
  116.  
  117.     text7  = tk.StringVar()
  118.     text8  = tk.StringVar()
  119.     text9  = tk.StringVar()
  120.     #text1.set("")
  121.     choiceButton  = Button(root,textvariable=text0,width=39,height=5,bg="#bbcab7", command= lambda: changeText(text0,choiceButton))
  122.     choiceButton.grid(row=0 ,column=0, columnspan=3)
  123.  
  124.     button1  = Button(root,textvariable=text1,text="",width=12,height=5,bg="#e1f1dd", command= lambda: buttonClicked(text1,button1,3,0) )
  125.     button1.grid(row=3 ,column=0)
  126.     button2  = Button(root,textvariable=text2,text="",width=12,height=5,bg="#bbcab7", command= lambda: buttonClicked(text2,button2,3,1) )
  127.     button2.grid(row=3 ,column=1)
  128.     button3  = Button(root,textvariable=text3,text="",width=12,height=5,bg="#e1f1dd", command= lambda: buttonClicked(text3,button3,3,2) )
  129.     button3.grid(row=3 ,column=2)
  130.  
  131.     button4  = Button(root,textvariable=text4,text="",width=12,height=5,bg="#bbcab7", command= lambda: buttonClicked(text4,button4,2,0) )
  132.     button4.grid(row=2 ,column=0)
  133.     button5  = Button(root,textvariable=text5,text="",width=12,height=5,bg="#e1f1dd", command= lambda: buttonClicked(text5,button5,2,1) )
  134.     button5.grid(row=2 ,column=1)
  135.     button6  = Button(root,textvariable=text6,text="",width=12,height=5,bg="#bbcab7", command= lambda: buttonClicked(text6,button6,2,2) )
  136.     button6.grid(row=2 ,column=2)
  137.  
  138.     button7  = Button(root,textvariable=text7,text="",width=12,height=5,bg="#e1f1dd", command= lambda: buttonClicked(text7,button7,1,0) )
  139.     button7.grid(row=1 ,column=0)
  140.     button8  = Button(root,textvariable=text8,text="",width=12,height=5,bg="#bbcab7", command= lambda: buttonClicked(text8,button8,1,1) )
  141.     button8.grid(row=1 ,column=1)
  142.     button9  = Button(root,textvariable=text9,text="",width=12,height=5,bg="#e1f1dd", command= lambda: buttonClicked(text9,button9,1,2) )
  143.     button9.grid(row=1 ,column=2)
  144.  
  145.     root.mainloop()
  146.  
  147.  
  148.  
Add Comment
Please, Sign In to add comment