├── .DS_Store ├── Gui.PNG ├── README.md ├── Winning_message.PNG ├── draw_messge.PNG ├── resources └── base.png └── ttgame.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python019/python-game-projects/076ff9081af7119421ebb1eaec9c0156a7c862de/.DS_Store -------------------------------------------------------------------------------- /Gui.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python019/python-game-projects/076ff9081af7119421ebb1eaec9c0156a7c862de/Gui.PNG -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tic Tac Toe Game In Python 2 | 3 | -------------------------------------------------------------------------------- /Winning_message.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python019/python-game-projects/076ff9081af7119421ebb1eaec9c0156a7c862de/Winning_message.PNG -------------------------------------------------------------------------------- /draw_messge.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python019/python-game-projects/076ff9081af7119421ebb1eaec9c0156a7c862de/draw_messge.PNG -------------------------------------------------------------------------------- /resources/base.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python019/python-game-projects/076ff9081af7119421ebb1eaec9c0156a7c862de/resources/base.png -------------------------------------------------------------------------------- /ttgame.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import ttk 3 | import tkinter.messagebox 4 | 5 | root=Tk() 6 | root.title("Tic Tac Toe") 7 | #add Buttons 8 | bu1=ttk.Button(root,text=' ') 9 | bu1.grid(row=0,column=0,sticky='snew',ipadx=40,ipady=40) 10 | bu1.config(command=lambda: ButtonClick(1)) 11 | 12 | bu2=ttk.Button(root,text=' ') 13 | bu2.grid(row=0,column=1,sticky='snew',ipadx=40,ipady=40) 14 | bu2.config(command=lambda: ButtonClick(2)) 15 | 16 | bu3=ttk.Button(root,text=' ') 17 | bu3.grid(row=0,column=2,sticky='snew',ipadx=40,ipady=40) 18 | bu3.config(command=lambda: ButtonClick(3)) 19 | 20 | bu4=ttk.Button(root,text=' ') 21 | bu4.grid(row=1,column=0,sticky='snew',ipadx=40,ipady=40) 22 | bu4.config(command=lambda: ButtonClick(4)) 23 | 24 | bu5=ttk.Button(root,text=' ') 25 | bu5.grid(row=1,column=1,sticky='snew',ipadx=40,ipady=40) 26 | bu5.config(command=lambda: ButtonClick(5)) 27 | 28 | bu6=ttk.Button(root,text=' ') 29 | bu6.grid(row=1,column=2,sticky='snew',ipadx=40,ipady=40) 30 | bu6.config(command=lambda: ButtonClick(6)) 31 | 32 | bu7=ttk.Button(root,text=' ') 33 | bu7.grid(row=2,column=0,sticky='snew',ipadx=40,ipady=40) 34 | bu7.config(command=lambda: ButtonClick(7)) 35 | 36 | bu8=ttk.Button(root,text=' ') 37 | bu8.grid(row=2,column=1,sticky='snew',ipadx=40,ipady=40) 38 | bu8.config(command=lambda: ButtonClick(8)) 39 | 40 | bu9=ttk.Button(root,text=' ') 41 | bu9.grid(row=2,column=2,sticky='snew',ipadx=40,ipady=40) 42 | bu9.config(command=lambda: ButtonClick(9)) 43 | 44 | playerturn=ttk.Label(root,text=" Player 1 turn! ") 45 | playerturn.grid(row=3,column=0,sticky='snew',ipadx=40,ipady=40) 46 | 47 | playerdetails=ttk.Label(root,text=" Player 1 is X\n\n Player 2 is O") 48 | playerdetails.grid(row=3,column=2,sticky='snew',ipadx=40,ipady=40) 49 | 50 | res=ttk.Button(root,text='Restart') 51 | res.grid(row=3,column=1,sticky='snew',ipadx=40,ipady=40) 52 | res.config(command=lambda: restartbutton()) 53 | 54 | a=1 55 | b=0 56 | c=0 57 | def restartbutton(): 58 | global a,b,c 59 | a=1 60 | b=0 61 | c=0 62 | playerturn['text']=" Player 1 turn! " 63 | bu1['text']=' ' 64 | bu2['text']=' ' 65 | bu3['text']=' ' 66 | bu4['text']=' ' 67 | bu5['text']=' ' 68 | bu6['text']=' ' 69 | bu7['text']=' ' 70 | bu8['text']=' ' 71 | bu9['text']=' ' 72 | bu1.state(['!disabled']) 73 | bu2.state(['!disabled']) 74 | bu3.state(['!disabled']) 75 | bu4.state(['!disabled']) 76 | bu5.state(['!disabled']) 77 | bu6.state(['!disabled']) 78 | bu7.state(['!disabled']) 79 | bu8.state(['!disabled']) 80 | bu9.state(['!disabled']) 81 | 82 | #after getting result(win or loss or draw) disable button 83 | def disableButton(): 84 | bu1.state(['disabled']) 85 | bu2.state(['disabled']) 86 | bu3.state(['disabled']) 87 | bu4.state(['disabled']) 88 | bu5.state(['disabled']) 89 | bu6.state(['disabled']) 90 | bu7.state(['disabled']) 91 | bu8.state(['disabled']) 92 | bu9.state(['disabled']) 93 | 94 | 95 | def ButtonClick(id): 96 | global a,b,c 97 | print("ID:{}".format(id)) 98 | 99 | #for player 1 turn 100 | if id==1 and bu1['text']==' ' and a==1: 101 | bu1['text']="X" 102 | a=0 103 | b+=1 104 | if id==2 and bu2['text']==' ' and a==1: 105 | bu2['text']="X" 106 | a=0 107 | b+=1 108 | if id==3 and bu3['text']==' ' and a==1: 109 | bu3['text']="X" 110 | a=0 111 | b+=1 112 | if id==4 and bu4['text']==' ' and a==1: 113 | bu4['text']="X" 114 | a=0 115 | b+=1 116 | if id==5 and bu5['text']==' ' and a==1: 117 | bu5['text']="X" 118 | a=0 119 | b+=1 120 | if id==6 and bu6['text']==' ' and a==1: 121 | bu6['text']="X" 122 | a=0 123 | b+=1 124 | if id==7 and bu7['text']==' ' and a==1: 125 | bu7['text']="X" 126 | a=0 127 | b+=1 128 | if id==8 and bu8['text']==' ' and a==1: 129 | bu8['text']="X" 130 | a=0 131 | b+=1 132 | if id==9 and bu9['text']==' ' and a==1: 133 | bu9['text']="X" 134 | a=0 135 | b+=1 136 | #for player 2 turn 137 | if id==1 and bu1['text']==' ' and a==0: 138 | bu1['text']="O" 139 | a=1 140 | b+=1 141 | if id==2 and bu2['text']==' ' and a==0: 142 | bu2['text']="O" 143 | a=1 144 | b+=1 145 | if id==3 and bu3['text']==' ' and a==0: 146 | bu3['text']="O" 147 | a=1 148 | b+=1 149 | if id==4 and bu4['text']==' ' and a==0: 150 | bu4['text']="O" 151 | a=1 152 | b+=1 153 | if id==5 and bu5['text']==' ' and a==0: 154 | bu5['text']="O" 155 | a=1 156 | b+=1 157 | if id==6 and bu6['text']==' ' and a==0: 158 | bu6['text']="O" 159 | a=1 160 | b+=1 161 | if id==7 and bu7['text']==' ' and a==0: 162 | bu7['text']="O" 163 | a=1 164 | b+=1 165 | if id==8 and bu8['text']==' ' and a==0: 166 | bu8['text']="O" 167 | a=1 168 | b+=1 169 | if id==9 and bu9['text']==' ' and a==0: 170 | bu9['text']="O" 171 | a=1 172 | b+=1 173 | 174 | #checking for winner 175 | if( bu1['text']=='X' and bu2['text']=='X' and bu3['text']=='X' or 176 | bu4['text']=='X' and bu5['text']=='X' and bu6['text']=='X' or 177 | bu7['text']=='X' and bu8['text']=='X' and bu9['text']=='X' or 178 | bu1['text']=='X' and bu4['text']=='X' and bu7['text']=='X' or 179 | bu2['text']=='X' and bu5['text']=='X' and bu8['text']=='X' or 180 | bu3['text']=='X' and bu6['text']=='X' and bu9['text']=='X' or 181 | bu1['text']=='X' and bu5['text']=='X' and bu9['text']=='X' or 182 | bu3['text']=='X' and bu5['text']=='X' and bu7['text']=='X'): 183 | disableButton() 184 | c=1 185 | tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 1") 186 | elif( bu1['text']=='O' and bu2['text']=='O' and bu3['text']=='O' or 187 | bu4['text']=='O' and bu5['text']=='O' and bu6['text']=='O' or 188 | bu7['text']=='O' and bu8['text']=='O' and bu9['text']=='O' or 189 | bu1['text']=='O' and bu4['text']=='O' and bu7['text']=='O' or 190 | bu2['text']=='O' and bu5['text']=='O' and bu8['text']=='O' or 191 | bu3['text']=='O' and bu6['text']=='O' and bu9['text']=='O' or 192 | bu1['text']=='O' and bu5['text']=='O' and bu9['text']=='O' or 193 | bu3['text']=='O' and bu5['text']=='O' and bu7['text']=='O'): 194 | disableButton() 195 | c=1 196 | tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 2") 197 | elif b==9: 198 | disableButton() 199 | c=1 200 | tkinter.messagebox.showinfo("Tic Tac Toe","Match is Draw.") 201 | 202 | if a==1 and c==0: 203 | playerturn['text']=" Player 1 turn! " 204 | elif a==0 and c==0: 205 | playerturn['text']=" Player 2 turn! " 206 | 207 | root.mainloop() 208 | 209 | --------------------------------------------------------------------------------