This repository was archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New TicTacToe GUI base Game using TkInter #501
Open
hi-meral
wants to merge
2
commits into
Python-World:master
Choose a base branch
from
hi-meral:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Tic Tac Toe | ||
|
||
## Description | ||
|
||
A python tkinter based 2-player Tic Tac Toe game. | ||
It takes input of the two players. | ||
The two players are named as X and O | ||
and will click alternating moves in attempt to win the game. | ||
|
||
## Prerequisites | ||
|
||
Install the tk in python | ||
|
||
**pip install tk** | ||
|
||
## How to run | ||
|
||
Just run | ||
|
||
```sh | ||
python tic-toc-toe-tkinter.py | ||
``` | ||
|
||
<!-- ## Screenshots/Demo --> | ||
|
||
https://tinyurl.com/yjn28mn9 | ||
|
||
|
||
## Authors | ||
- [Miral Maradia] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
from tkinter import * | ||
from tkinter import messagebox | ||
from typing import Counter | ||
|
||
|
||
root = Tk() | ||
root.title("My Tic Toc Toe") | ||
|
||
|
||
my_menu = Menu(root) | ||
root.config(menu=my_menu) | ||
option_menu = Menu(my_menu, tearoff=FALSE) | ||
|
||
my_menu.add_cascade(label="Options", menu=option_menu) | ||
|
||
|
||
def whoseTurn(): | ||
global clicked | ||
return "X" if clicked is True else "O" | ||
|
||
|
||
def disable_all_button(): | ||
b1.config(state=DISABLED) | ||
b2.config(state=DISABLED) | ||
b3.config(state=DISABLED) | ||
b4.config(state=DISABLED) | ||
b5.config(state=DISABLED) | ||
b6.config(state=DISABLED) | ||
b7.config(state=DISABLED) | ||
b8.config(state=DISABLED) | ||
b9.config(state=DISABLED) | ||
|
||
|
||
def is_win(): | ||
global b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 | ||
global clicked, count | ||
|
||
if(b1["text"] != " " and (b1["text"] == b4["text"] == b7["text"])): | ||
b1.config(bg="red") | ||
b4.config(bg="red") | ||
b7.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b2["text"] != " " and (b2["text"] == b5["text"] == b8["text"])): | ||
b2.config(bg="red") | ||
b5.config(bg="red") | ||
b8.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b3["text"] != " " and (b3["text"] == b6["text"] == b9["text"])): | ||
b3.config(bg="red") | ||
b6.config(bg="red") | ||
b9.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b1["text"] != " " and (b1["text"] == b2["text"] == b3["text"])): | ||
b1.config(bg="red") | ||
b2.config(bg="red") | ||
b3.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b4["text"] != " " and (b4["text"] == b5["text"] == b6["text"])): | ||
b4.config(bg="red") | ||
b5.config(bg="red") | ||
b6.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b7["text"] != " " and (b7["text"] == b8["text"] == b9["text"])): | ||
b7.config(bg="red") | ||
b8.config(bg="red") | ||
b9.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b1["text"] != " " and (b1["text"] == b5["text"] == b9["text"])): | ||
b1.config(bg="red") | ||
b5.config(bg="red") | ||
b9.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif(b3["text"] != " " and (b3["text"] == b5["text"] == b7["text"])): | ||
b3.config(bg="red") | ||
b5.config(bg="red") | ||
b7.config(bg="red") | ||
b10["text"] = f"{whoseTurn()} Won" | ||
disable_all_button() | ||
|
||
elif (count == 9): | ||
b10["text"] = "Its tie, No one Won!!" | ||
disable_all_button() | ||
|
||
|
||
def b_click(b): | ||
global clicked, count, b10 | ||
|
||
if b["text"] == " ": | ||
b["text"] = whoseTurn() | ||
count += 1 | ||
b10["text"] = "X Turn" if clicked is False else "O Turn" | ||
is_win() | ||
clicked = not clicked | ||
|
||
|
||
def reset(): | ||
global b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 | ||
global clicked, count | ||
|
||
clicked = True | ||
count = 0 | ||
|
||
b1 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b1)) | ||
b2 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b2)) | ||
b3 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b3)) | ||
|
||
b4 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b4)) | ||
b5 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b5)) | ||
b6 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b6)) | ||
|
||
b7 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b7)) | ||
b8 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b8)) | ||
b9 = Button(root, text=" ", font=("Helvetica,20"), height=3, | ||
width=6, bg="SystemButtonFace", command=lambda: b_click(b9)) | ||
|
||
b10 = Button(root, text=f'{whoseTurn()} Turn', font=( | ||
"Helvetica,20"), height=2, width=20, bg="Black", fg="white") | ||
|
||
b1.grid(row=0, column=0) | ||
b2.grid(row=0, column=1) | ||
b3.grid(row=0, column=2) | ||
|
||
b4.grid(row=1, column=0) | ||
b5.grid(row=1, column=1) | ||
b6.grid(row=1, column=2) | ||
|
||
b7.grid(row=2, column=0) | ||
b8.grid(row=2, column=1) | ||
b9.grid(row=2, column=2) | ||
|
||
b10.grid(row=3, columnspan=3) | ||
|
||
|
||
option_menu.add_command(label="Reset Game", command=reset) | ||
reset() | ||
root.mainloop() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it modular by doing all manipulation inside functions and call them with some parameter