Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 63506e9

Browse files
authored
Merge pull request #191 from madhavmehndiratta/ticTacToe
GUI-Based Tic Tac Toe Game with Python
2 parents 9da4711 + ecd8313 commit 63506e9

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# TIC TAC TOE #
2+
3+
A simple tic tac toe game made in python.
4+
5+
## Introduction ##
6+
7+
A tic tac toe is a game in which two players seek in alternate turns to complete a row, a column, or a diagonal with either three O's or three X's drawn in the spaces of a grid of nine squares; noughts and crosses. I've tried making this in python using `tkinter`
8+
9+
## Usage ##
10+
```
11+
$ python3 main.py
12+
```
13+
14+
## Examples ##
15+
16+
![sample1](resources/sample1.png)
17+
18+
![sample2](resources/sample2.png)
19+
20+
## Author name
21+
<a href="https://github.com/madhavmehndiratta">Madhav Mehndiratta</a>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from tkinter import *
2+
import tkinter.messagebox
3+
import sys
4+
import os
5+
6+
tk = Tk()
7+
tk.title("Tic Tac Toe")
8+
tk.resizable(False, False)
9+
10+
bclick = True
11+
count = 0
12+
13+
# This function prevents the user from clicking the same button again.
14+
def button(buttons):
15+
global bclick, count
16+
if buttons["text"] == "" and bclick == True:
17+
buttons["text"] = "X"
18+
bclick = False
19+
winner()
20+
count += 1
21+
22+
elif buttons["text"] == "" and bclick == False:
23+
buttons["text"] = "O"
24+
bclick = True
25+
winner()
26+
count += 1
27+
else:
28+
tkinter.messagebox.showinfo("Tic Tac Toe", "Button already Clicked!")
29+
30+
def playagain():
31+
python = sys.executable
32+
os.execl(python, python, * sys.argv)
33+
34+
def winner():
35+
if (button1["text"] == "X" and button2["text"] == "X" and button3["text"] == "X" or
36+
button4["text"] == "X" and button5["text"] == "X" and button6["text"] == "X" or
37+
button7["text"] == "X" and button8["text"] == "X" and button9["text"] == "X" or
38+
button1["text"] == "X" and button5["text"] == "X" and button9["text"] == "X" or
39+
button3["text"] == "X" and button5["text"] == "X" and button7["text"] == "X" or
40+
button1["text"] == "X" and button4["text"] == "X" and button7["text"] == "X" or
41+
button2["text"] == "X" and button5["text"] == "X" and button8["text"] == "X" or
42+
button3["text"] == "X" and button6["text"] == "X" and button9["text"] == "X"):
43+
msgbox = tkinter.messagebox.askquestion("Tic Tac Toe", "X wins! \n\nDo you want to play again?")
44+
if msgbox == "yes":
45+
playagain()
46+
else:
47+
tk.destroy()
48+
49+
elif (button1["text"] == "O" and button2["text"] == "O" and button3["text"] == "O" or
50+
button4["text"] == "O" and button5["text"] == "O" and button6["text"] == "O" or
51+
button7["text"] == "O" and button8["text"] == "O" and button9["text"] == "O" or
52+
button1["text"] == "O" and button5["text"] == "O" and button9["text"] == "O" or
53+
button3["text"] == "O" and button5["text"] == "O" and button7["text"] == "O" or
54+
button1["text"] == "O" and button4["text"] == "O" and button7["text"] == "O" or
55+
button2["text"] == "O" and button5["text"] == "O" and button8["text"] == "O" or
56+
button3["text"] == "O" and button6["text"] == "O" and button9["text"] == "O"):
57+
msgbox = tkinter.messagebox.askquestion("Tic Tac Toe", "O wins! \n\nDo you want to play again?")
58+
if msgbox == "yes":
59+
playagain()
60+
else:
61+
tk.destroy()
62+
63+
elif(count == 8):
64+
msgbox = tkinter.messagebox.askquestion("Tic Tac Toe", "It's a tie! \n\nDo you want to play again?")
65+
if msgbox == "yes":
66+
playagain()
67+
else:
68+
tk.destroy()
69+
70+
# Game Board
71+
72+
restart = Button(tk, text = "RESTART", font = "Arial", bg = "white", fg = "black", height = 3, width = 8, command = lambda: playagain())
73+
restart.grid(row = 2, column = 0)
74+
75+
quit = Button(tk, text = "EXIT", font = "Arial", bg = "white", fg = "black", height = 3, width = 8, command = lambda: tk.destroy())
76+
quit.grid(row = 2, column = 2)
77+
78+
button1 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button1))
79+
button1.grid(row = 3, column = 0)
80+
81+
button2 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button2))
82+
button2.grid(row = 3, column = 1)
83+
84+
button3 = Button(tk, text = "",font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button3))
85+
button3.grid(row = 3, column = 2)
86+
87+
button4 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button4))
88+
button4.grid(row = 4, column = 0)
89+
90+
button5 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button5))
91+
button5.grid(row = 4, column = 1)
92+
93+
button6 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button6))
94+
button6.grid(row = 4, column = 2)
95+
96+
button7 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button7))
97+
button7.grid(row = 5, column = 0)
98+
99+
button8 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button8))
100+
button8.grid(row = 5, column = 1)
101+
102+
button9 = Button(tk, text = "", font = "Arial", bg = "Black", fg = "white", height = 4, width=8, command=lambda: button(button9))
103+
button9.grid(row = 5, column = 2)
104+
105+
tk.mainloop()
Loading
Loading

0 commit comments

Comments
 (0)