Skip to content

Added TIC_TAC_TOE #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions TIC_TAC_TOE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TIC_TAC_TOE
TIC_TAC_TOE using Python and Tkinter
This is a simple Tic-Tac-Toe game implemented in Python using the Tkinter library for the graphical user interface. Players can enter their names in the provided text entry fields, and then take turns playing the classic Tic-Tac-Toe game on a 3x3 grid. The game logic checks for wins, ties, and provides an option to reset the game.

Here is a brief description of the key components and functionality of this code:
1. **Player Names**: Players can enter their names in the text entry fields provided.
2. **Game Grid**: The game grid is displayed as a 3x3 matrix of buttons, where players can click to make their moves.
3. **Winning Logic**: The code checks for winning conditions in rows, columns, and diagonals. When a player wins, the winning combination is highlighted in green, and a message box displays the winner's name.
4. **Tie**: If the game results in a tie, a message box announces it.
5. **Reset Game**: There is a "Reset Game" button to start a new game.
6. **Responsive UI**: The UI responds to player actions and updates the button text and color appropriately.
This code can be a simple and fun addition to your GitHub repository, allowing others to play Tic-Tac-Toe and learn from or contribute to the code. It provides a practical example of creating a basic game using Tkinter in Python.

## OUTPUT

<div align="center">
<img src="assets/img/Output1.png">
<img src="assets/img/Output2.png">
<img src="assets/img/Output3.png">
</div>
93 changes: 93 additions & 0 deletions TIC_TAC_TOE/TIC_TAC_TOE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from tkinter import *
import tkinter.messagebox
tk = Tk()
tk.title("Tic Tac Toe")
tk.configure(bg='yellow')
p1 = StringVar()
p2 = StringVar()
player1_name = Entry(textvariable=p1, bd=5, bg='white', width=40)
player1_name.grid(row=1, column=1, columnspan=8)
player2_name = Entry(tk, textvariable=p2, bd=5, bg='white', width=40)
player2_name.grid(row=2, column=1, columnspan=8)
bclick = True
flag = 0
current_player_name = p1.get() if p1.get() else 'X'
def disableButton():
for i in range(3):
for j in range(3):
buttons[i][j].configure(state=DISABLED)
def checkForWin():
for i in range(3):
if buttons[i][0]["text"] == buttons[i][1]["text"] == buttons[i][2]["text"] != " ":
buttons[i][0].config(bg="green")
buttons[i][1].config(bg="green")
buttons[i][2].config(bg="green")
disableButton()
winner_name = p1.get() if buttons[i][0]['text'] == 'X' else p2.get()
if not winner_name:
winner_name = 'Player 1' if buttons[i][0]['text'] == 'X' else 'Player 2'
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
return
if buttons[0][i]["text"] == buttons[1][i]["text"] == buttons[2][i]["text"] != " ":
buttons[0][i].config(bg="green")
buttons[1][i].config(bg="green")
buttons[2][i].config(bg="green")
disableButton()
winner_name = p1.get() if buttons[0][i]['text'] == 'X' else p2.get()
if not winner_name:
winner_name = 'Player 1' if buttons[0][i]['text'] == 'X' else 'Player 2'
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
return
if buttons[0][0]["text"] == buttons[1][1]["text"] == buttons[2][2]["text"] != " ":
buttons[0][0].config(bg="green")
buttons[1][1].config(bg="green")
buttons[2][2].config(bg="green")
disableButton()
winner_name = p1.get() if buttons[0][0]['text'] == 'X' else p2.get()
if not winner_name:
winner_name = 'Player 1' if buttons[0][0]['text'] == 'X' else 'Player 2'
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
return
if buttons[0][2]["text"] == buttons[1][1]["text"] == buttons[2][0]["text"] != " ":
buttons[0][2].config(bg="green")
buttons[1][1].config(bg="green")
buttons[2][0].config(bg="green")
disableButton()
winner_name = p1.get() if buttons[0][2]['text'] == 'X' else p2.get()
if not winner_name:
winner_name = 'Player 1' if buttons[0][2]['text'] == 'X' else 'Player 2'
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
return
if flag == 8:
tkinter.messagebox.showinfo("Tic-Tac-Toe", "It's a Tie")
def resetGame():
global bclick, flag, current_player_name
for i in range(3):
for j in range(3):
buttons[i][j]["text"] = " "
buttons[i][j].config(bg='black', state=NORMAL)
bclick = True
flag = 0
current_player_name = p1.get() if p1.get() else 'X'
Label(tk, text="Player 1:", font='Times 20 bold', bg='yellow', fg='black', height=1, width=8).grid(row=1, column=0)
Label(tk, text="Player 2:", font='Times 20 bold', bg='yellow', fg='black', height=1, width=8).grid(row=2, column=0)
buttons = [[Button(tk, text=' ', font='Times 20 bold', bg='black', fg='white', height=4, width=8) for _ in range(3)] for _ in range(3)]
def btnClick(button):
global bclick, flag
if button["text"] == " ":
if bclick:
button["text"] = "X"
else:
button["text"] = "O"
bclick = not bclick
flag += 1
checkForWin()
else:
tkinter.messagebox.showinfo("Tic-Tac-Toe", "Button already Clicked!")
for i in range(3):
for j in range(3):
buttons[i][j].configure(command=lambda row=i, col=j: btnClick(buttons[row][col]))
buttons[i][j].grid(row=i + 3, column=j)
reset_button = Button(tk, text="Reset Game", font='Times 16 bold', bg='white', fg='black', command=resetGame)
reset_button.grid(row=6, column=0, columnspan=3)
tk.mainloop()
Binary file added TIC_TAC_TOE/assets/img/Output1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TIC_TAC_TOE/assets/img/Output2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TIC_TAC_TOE/assets/img/Output3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.