ArfIsAToe

tictactoe to baha

May 8th, 2021
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. import tkinter as tk
  3. from tkinter import messagebox
  4. import tkinter.font as font
  5. """ GREAT COLOR PALETTES :https://imgur.com/Jmk6LEH """
  6.  
  7. def insertWinButtons(xo,l,c,nextl,nextc,prevl,prevc):
  8.     winButtonOne= Button(root,bd=0,text=xo,width=12,height=5,fg="#323831",bg="#a6e22e")
  9.     winButtonOne.grid(row=l+1,column=c)
  10.     winButtonOne["font"]= myFont
  11.        
  12.     winButtonTwo= Button(root,bd=0,text=xo,width=12,height=5,fg="#323831",bg="#a6e22e")
  13.     winButtonTwo.grid(row=nextl+1,column=nextc)
  14.     winButtonTwo["font"]= myFont
  15.        
  16.     winButtonThree= Button(root,bd=0,text=xo,width=12,height=5,fg="#323831",bg="#a6e22e")
  17.     winButtonThree.grid(row=prevl+1,column=prevc)
  18.     winButtonThree["font"]= myFont
  19. # checking if one of the players won
  20. def checkForWin(mat,l,c,xo):
  21.      
  22.     prevl=l-1 if l>0 else 2
  23.     prevc=c-1 if c>0 else 2
  24.     nextl=l+1 if l<2 else 0
  25.     nextc=c+1 if c<2 else 0
  26.    
  27.    
  28.     # checking main diagonal
  29.     if (l==c):
  30.         if (mat[prevl][prevc]+mat[nextl][nextc]+mat[l][c]==xo*3):
  31.             insertWinButtons(xo,l,c,nextl,nextc,prevl,prevc)
  32.             return TRUE
  33.     # checking the opposite diagonal
  34.     elif (l+c==2):
  35.         if (mat[prevl][nextc]+mat[nextl][prevc]+mat[l][c]==xo*3):
  36.             insertWinButtons(xo,l,c,prevl,nextc,nextl,prevc)
  37.             return TRUE  
  38.     #checking lines and columns
  39.     if(mat[prevl][c]+mat[nextl][c]+mat[l][c]==xo*3 ):
  40.         insertWinButtons(xo,l,c,nextl,c,prevl,c)
  41.         return TRUE
  42.     elif mat[l][prevc]+mat[l][nextc]+mat[l][c]==xo*3:
  43.         insertWinButtons(xo,l,c,l,nextc,l,prevc)
  44.         return TRUE
  45.     else:
  46.         return FALSE
  47.        
  48. # Defining the functions for the buttons
  49. def buttonClicked(text,button,l,c):
  50.     global x, numOfButtonsDisabled, stopPlaying, firstChar, secondChar, playerOneWins, playerTwoWins
  51.     x+=1
  52.     atLeastOnePlayerWon=FALSE
  53.  
  54.     choiceButton["state"]= DISABLED
  55.     choiceButton["bg"]="#404040"
  56.     choiceButton["fg"]="#000000"
  57.  
  58.     button["state"]= DISABLED
  59.     if (x%2==0):  
  60.         #only changes color if the player chooses the "only color" option before he starts playing
  61.         if (firstChar==""):    
  62.             button["bg"]="#feecaa"
  63.             #button["relief"]= "sunken"        
  64.  
  65.         text.set(firstChar)
  66.         mat[l-1][c]=firstChar
  67.         if checkForWin(mat,l-1,c,firstChar):
  68.             playerTwoWins+=1
  69.             atLeastOnePlayerWon= TRUE
  70.             box = messagebox.askquestion("Player 2 Won", "\tSCORE:  "+str(playerOneWins)+" - "+str(playerTwoWins)+"\nWould You Like To Keep Playing?",icon = 'question')
  71.             if (box !='yes'):
  72.                 stopPlaying=TRUE        
  73.             root.destroy()      
  74.     else:
  75.         #only changes color if the player chooses the "only color" option before he starts playing
  76.         if (firstChar ==""):
  77.             #Color Combinations pink and pastel(bb8082,f6dfeb)  white and gray(ffffff,353535) brown and gray(897853,353535) pastel purple and pink(f6dfeb,dbd0ff) red and black(630000,1b1717)
  78.             button["bg"]="#decc8c"
  79.             #button["relief"]= "sunken"
  80.  
  81.         text.set(secondChar)
  82.         mat[l-1][c]=secondChar
  83.         if checkForWin(mat,l-1,c,secondChar):
  84.             playerOneWins+=1
  85.             atLeastOnePlayerWon= TRUE
  86.             box=messagebox.askquestion("Player 1 Won","\tSCORE:  "+str(playerOneWins)+" - "+str(playerTwoWins)+"\nWould You Like To Keep Playing?",icon = 'question')
  87.             if (box!='yes'):
  88.                 stopPlaying=TRUE          
  89.             root.destroy()  
  90.  
  91.     #in case of numOfButtonsDisabled==9 then we know it's a draw unless someone won with the last button click
  92.     numOfButtonsDisabled+=1
  93.     if numOfButtonsDisabled==9 and atLeastOnePlayerWon == FALSE:
  94.         box=messagebox.askquestion("It's a TIE ", "\tSCORE:  "+str(playerOneWins)+" - "+str(playerTwoWins)+"\nWould You Like To Keep Playing?",icon = 'question')
  95.         if (box!='yes'):
  96.             stopPlaying=TRUE
  97.         root.destroy()
  98.  
  99. #picking what to fill the buttons with
  100. def changeText(text,button):
  101.     global firstChar, secondChar, charList, charNumber
  102.     charNumber= charNumber+1 if charNumber<4 else 0 #charNumber is the variables traversing the charList[][]
  103.     firstChar=charList[charNumber][0]
  104.     secondChar=charList[charNumber][1]
  105.     text.set(charList[charNumber][2])  
  106.  
  107. def onClose():
  108.     global stopPlaying
  109.     stopPlaying=TRUE
  110.     root.destroy()
  111.  
  112. """--------------------------Main--------------------------"""    
  113. global stopPlaying, playerOneWins, playerTwoWins
  114. stopPlaying=FALSE #boolean deciding when to stop the while loop: only becomes true if the player clicks no on the popup
  115. playerTwoWins=0 #tracking how many times player 1 won
  116. playerOneWins=0#tracking how many times player 2 won
  117. while (stopPlaying==FALSE):
  118.    
  119.     charNumber=0#variable selecing the subarrays containing the preferred symbols
  120.     charList=[["O","X","X O"],["+","-","- +"],["B","A","A B"],["Y","X","X Y"],[""," ","Only Color"]] # list of preferred symbols ("X O","A B", etc...) the user has to pick from
  121.     mat=[["."]*3,["."]*3,["."]*3] # matrix keeping count of who marked where
  122.     numOfButtonsDisabled=0 # tracking if there's a draw or not
  123.        
  124.     root = tk.Tk()
  125.     root.title("Tic Tac toe")
  126.     root.iconbitmap(r'images/TicTacToe.ico')
  127.     root.configure(bg="#c7dabf")
  128.     root.protocol('WM_DELETE_WINDOW', onClose)
  129.    
  130.     # define font
  131.     myFontChoice = font.Font(family='Times',weight="bold",slant="italic")
  132.     myFont = font.Font(family='Times',weight="bold")
  133.  
  134.  
  135.     global x, firstChar, secondChar
  136.     firstChar="O"
  137.     secondChar="X"    
  138.     x=0
  139.  
  140.     """------------------------------------Defining each button and putting it on the grid------------------------------------"""
  141.     #choiceButton is the button the user has to click on to pick preferred symbols "X O","A B", etc...
  142.    
  143.     text0  = tk.StringVar()
  144.     text0.set("Click To Pick Variant")
  145.     choiceButton  = Button(root,bd=0,textvariable=text0,width=38,height=5,fg="#323831",bg="#a3b996", command= lambda: changeText(text0,choiceButton))
  146.     choiceButton.grid(padx=1,pady=1,row=0 ,column=0, columnspan=3)
  147.     choiceButton["font"]= myFontChoice
  148.  
  149.     #Row of [1,2,3]
  150.     text1  = tk.StringVar()
  151.     button1  = Button(root,bd=0,textvariable=text1,text="",width=12,height=5,bg="#bbca95", command= lambda: buttonClicked(text1,button1,3,0) )
  152.     button1.grid(padx=2,pady=1,row=3 ,column=0)
  153.     button1["font"]= myFont
  154.  
  155.     text2  = tk.StringVar()
  156.     button2  = Button(root,bd=0,textvariable=text2,text="",width=12,height=5,bg="#a3b996", command= lambda: buttonClicked(text2,button2,3,1) )
  157.     button2.grid(padx=1,pady=1,row=3 ,column=1)
  158.     button2["font"]= myFont
  159.  
  160.     text3  = tk.StringVar()
  161.     button3  = Button(root,bd=0,textvariable=text3,text="",width=12,height=5,bg="#bbca95", command= lambda: buttonClicked(text3,button3,3,2) )
  162.     button3.grid(padx=2,pady=1,row=3 ,column=2)
  163.     button3["font"]= myFont
  164.  
  165.     #Row of [4,5,6]
  166.     text4  = tk.StringVar()
  167.     button4  = Button(root,bd=0,textvariable=text4,text="",width=12,height=5,bg="#a3b996", command= lambda: buttonClicked(text4,button4,2,0) )
  168.     button4.grid(padx=2,pady=1,row=2 ,column=0)
  169.     button4["font"]= myFont
  170.  
  171.     text5  = tk.StringVar()
  172.     button5  = Button(root,bd=0,textvariable=text5,text="",width=12,height=5,bg="#bbca95", command= lambda: buttonClicked(text5,button5,2,1) )
  173.     button5.grid(padx=1,pady=1,row=2 ,column=1)
  174.     button5["font"]= myFont
  175.  
  176.     text6  = tk.StringVar()
  177.     button6  = Button(root,bd=0,textvariable=text6,text="",width=12,height=5,bg="#a3b996", command= lambda: buttonClicked(text6,button6,2,2) )
  178.     button6.grid(padx=2,pady=1,row=2 ,column=2)
  179.     button6["font"]= myFont
  180.  
  181.     #Row of [7,8,9]
  182.     text7  = tk.StringVar()
  183.     button7  = Button(root,bd=0,textvariable=text7,text="",width=12,height=5,bg="#bbca95", command= lambda: buttonClicked(text7,button7,1,0) )
  184.     button7.grid(padx=2,pady=1,row=1 ,column=0)
  185.     button7["font"]= myFont
  186.  
  187.     text8  = tk.StringVar()
  188.     button8  = Button(root,bd=0,textvariable=text8,text="",width=12,height=5,bg="#a3b996", command= lambda: buttonClicked(text8,button8,1,1) )
  189.     button8.grid(padx=1,pady=1,row=1 ,column=1)
  190.     button8["font"]= myFont
  191.  
  192.     text9  = tk.StringVar()
  193.     button9  = Button(root,bd=0,textvariable=text9,text="",width=12,height=5,bg="#bbca95", command= lambda: buttonClicked(text9,button9,1,2) )
  194.     button9.grid(padx=2,pady=1,row=1 ,column=2)
  195.     button9["font"]= myFont
  196.  
  197.  
  198.     root.mainloop()
  199. """
  200.  
  201.  
  202. █████╗ ██████╗ ███████╗██╗███████╗ █████╗ ████████╗ ██████╗
  203. ██╔══██╗██╔══██╗██╔════╝██║╚══███╔╝██╔══██╗╚══██╔══╝██╔═══██╗
  204. ███████║██████╔╝█████╗  ██║  ███╔╝ ███████║   ██║   ██║   ██║
  205. ██╔══██║██╔══██╗██╔══╝  ██║ ███╔╝  ██╔══██║   ██║   ██║   ██║
  206. ██║  ██║██║  ██║██║     ██║███████╗██║  ██║   ██║   ╚██████╔╝
  207. ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝╚══════╝╚═╝  ╚═╝   ╚═╝    ╚═════╝
  208.                                                            
  209.  
  210. """
RAW Paste Data