Python Tic Tac Toe game ⭕
ฝัง
- เผยแพร่เมื่อ 11 ก.ค. 2024
- python tic tac toe game tutorial example explained
#python #tictactoe #game
********************************************************
Python Tic Tac Toe game
********************************************************
from tkinter import *
import random
def next_turn(row, column):
global player
if buttons[row][column]['text'] == "" and check_winner() is False:
if player == players[0]:
buttons[row][column]['text'] = player
if check_winner() is False:
player = players[1]
label.config(text=(players[1]+" turn"))
elif check_winner() is True:
label.config(text=(players[0]+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
else:
buttons[row][column]['text'] = player
if check_winner() is False:
player = players[0]
label.config(text=(players[0]+" turn"))
elif check_winner() is True:
label.config(text=(players[1]+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
def check_winner():
for row in range(3):
if buttons[row][0]['text'] == buttons[row][1]['text'] == buttons[row][2]['text'] != "":
buttons[row][0].config(bg="green")
buttons[row][1].config(bg="green")
buttons[row][2].config(bg="green")
return True
for column in range(3):
if buttons[0][column]['text'] == buttons[1][column]['text'] == buttons[2][column]['text'] != "":
buttons[0][column].config(bg="green")
buttons[1][column].config(bg="green")
buttons[2][column].config(bg="green")
return True
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")
return True
elif 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")
return True
elif empty_spaces() is False:
for row in range(3):
for column in range(3):
buttons[row][column].config(bg="yellow")
return "Tie"
else:
return False
def empty_spaces():
spaces = 9
for row in range(3):
for column in range(3):
if buttons[row][column]['text'] != "":
spaces -= 1
if spaces == 0:
return False
else:
return True
def new_game():
global player
player = random.choice(players)
label.config(text=player+" turn")
for row in range(3):
for column in range(3):
buttons[row][column].config(text="",bg="#F0F0F0")
window = Tk()
window.title("Tic-Tac-Toe")
players = ["x","o"]
player = random.choice(players)
buttons = [[0,0,0],
[0,0,0],
[0,0,0]]
label = Label(text=player + " turn", font=('consolas',40))
label.pack(side="top")
reset_button = Button(text="restart", font=('consolas',20), command=new_game)
reset_button.pack(side="top")
frame = Frame(window)
frame.pack()
for row in range(3):
for column in range(3):
buttons[row][column] = Button(frame, text="",font=('consolas',40), width=5, height=2,
command= lambda row=row, column=column: next_turn(row,column))
buttons[row][column].grid(row=row,column=column)
window.mainloop()
********************************************************
Bro Code merch store 👟 :
===========================================================
teespring.com/stores/bro-code-5
===========================================================
music credits 🎼 :
===========================================================
Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
Free Download / Stream: bit.ly/2JnDfCE
Music promoted by Audio Library • Up In My Jam (All Of A...
=========================================================== - วิทยาศาสตร์และเทคโนโลยี
# ********************************************************
# Python Tic Tac Toe game
# ********************************************************
from tkinter import *
import random
def next_turn(row, column):
global player
if buttons[row][column]['text'] == "" and check_winner() is False:
if player == players[0]:
buttons[row][column]['text'] = player
if check_winner() is False:
player = players[1]
label.config(text=(players[1]+" turn"))
elif check_winner() is True:
label.config(text=(players[0]+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
else:
buttons[row][column]['text'] = player
if check_winner() is False:
player = players[0]
label.config(text=(players[0]+" turn"))
elif check_winner() is True:
label.config(text=(players[1]+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
def check_winner():
for row in range(3):
if buttons[row][0]['text'] == buttons[row][1]['text'] == buttons[row][2]['text'] != "":
buttons[row][0].config(bg="green")
buttons[row][1].config(bg="green")
buttons[row][2].config(bg="green")
return True
for column in range(3):
if buttons[0][column]['text'] == buttons[1][column]['text'] == buttons[2][column]['text'] != "":
buttons[0][column].config(bg="green")
buttons[1][column].config(bg="green")
buttons[2][column].config(bg="green")
return True
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")
return True
elif 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")
return True
elif empty_spaces() is False:
for row in range(3):
for column in range(3):
buttons[row][column].config(bg="yellow")
return "Tie"
else:
return False
def empty_spaces():
spaces = 9
for row in range(3):
for column in range(3):
if buttons[row][column]['text'] != "":
spaces -= 1
if spaces == 0:
return False
else:
return True
def new_game():
global player
player = random.choice(players)
label.config(text=player+" turn")
for row in range(3):
for column in range(3):
buttons[row][column].config(text="",bg="#F0F0F0")
window = Tk()
window.title("Tic-Tac-Toe")
players = ["x","o"]
player = random.choice(players)
buttons = [[0,0,0],
[0,0,0],
[0,0,0]]
label = Label(text=player + " turn", font=('consolas',40))
label.pack(side="top")
reset_button = Button(text="restart", font=('consolas',20), command=new_game)
reset_button.pack(side="top")
frame = Frame(window)
frame.pack()
for row in range(3):
for column in range(3):
buttons[row][column] = Button(frame, text="",font=('consolas',40), width=5, height=2,
command= lambda row=row, column=column: next_turn(row,column))
buttons[row][column].grid(row=row,column=column)
window.mainloop()
# ********************************************************
epic video bro
Love you
@Bro code how do we save the data of the wins
@@justairman hey man not sure if you are still in need of this but you can use the command "global"
Op video bro code
Bro you just explained tic tac toe in 21 min to a complete beginner. Hats off! mate
dude you have the same profile pic like me
Outstanding explanation given by you bro, easy to understand. And have successfully made this TicTacToe game. Thanks to you again... (HOPE TO SEE MORE AMAZING VIDEOS FROM YOU)
Very entertaining and also quite understandable. Quality content! Keep it up!
nice work. Truly inspirational, keep up the good work
I really like the way you explained : Simple, clean and beautiful.
Wow! You actually encouraged me to do python even when I was not that into it❤ Also got a lot of videos left to go!
Rad vid bro! Keep up the cool work. :)
Bro Code thanks You are a great man I know I am late but you just made my carrier! I am really happy to get this course for free and not paying that stupid 199$ for python course now I can really apply for jobs but of-course after some practice
Hyo Bro ❤️💯. We do appreciate your hard work. Young programmers are building their mental Database thanks to you 👏👏👏👏
wow very interesting and educative thank you
Thank you so much Bro Code! I learned so much new information from this tutorial!
Excelent explanation, thank you.
Thank you so much for the video
I love this channel it helps me so much learning python and other stuff
This is Awsome!
great video
Fantastic video!
you are best!!!
you re very good teacher thanks a lot 👏👏😘
Thank you bro about this good video
(:
Big fan 🖤
very nice video!!
Thanks my dear Bro 💯👑 Could you also do the minesweeper?
thx for the game Bro Code always the best ;)
Thank You! Explained well
Thanks a bunch you're great 👏
Great video, very easy to go with and I understood most of what went over, and just searched whatever I didn't know. But 1 question, I am currently using pycharm on my macbook and everything works except the part where it could show the winner, meaning the background does not change to green, or yellow when a tie. Is there a reason to why this is happening, and if there is a work around to this?
Bro, you're a such great and benevolent programmer!!! God good you are!!!
Thanks for making this video!
Thank you Bro!
This is an awesome tutorial. Thanks!
only missing the computer player AI trying to win against us. or alternatively adding 2 different controller inputs like mouse and keyboard for each player. example one key to chose which button position and Enter to push it, the the mouse thing will remain the same. I will try to implement this myself.
How did it go? You figured it out?
Good video taught me alot!
Hi, thanks for the video, it really helped so thank you so much for that and also I have a doubt, what platform should I use if I want to convert this script into a mobile application.
Thank you for the video bro!
a real great tutorial ty bro
Thank you so much sir.
thank you for the vid
Thanks sir
Woah! That’s good video😂😂😂
Thank you very much !
this helped me thank you :)
great
nice and gg
Cool
awesome he's a great teacher
nice
My first Python project , Thanks a lot.
Thank you bro
😍 you
bro very excellent video, but just one question if I wanted to play against the machine, what function can I use
thank you very much
thank you bro
THX SO MUCH
I am a fellow bro from now on ❤
Hello bro
thank you
winner winner chicken dinner
learn new thing thank you
great job, bro, thank you!
underrated af
thx man true gigachad
bro you are genius
Hello, What can I do to make the squares stay painted when I hover the mouse over them?
im on mac using sublime but i cant seem to figure out why the bg colors arent appearing for me. i get no errors however i get no colors froms the inning or ties
from tkmacosx import Button as BT
use this and then replace Button with BT
Very nice tutorial, but is there a way to add CSS to this project? I'm more familiar with that language for styling of the buttons, background color etc. compared to (bg = "orange")
You're a true CHAD!
thank you it work correctly
I see some guys around trying to implement AIs already and here I come with the question to pass the exam lol:
How can I add a score to this? I've managed to add a label for the score and I assume I need some variables where we would need to add the wins.
Can someone drop a comment if you figured it out?
genius
Op video bro code please relply
I couldn't get this to work. Very curious, I wonder what went wrong?
good old days when bro was 21 years old
my brain... ahhhh
I like this video, a very good tutorial, thanks.😄
why in this line "reset_button = Button(text="restart",font=('Aharoni',20),command=new_game)" you didn't put () after new_game?
You dont need too
I used () as in video, and function "new game" didn't work.... but when removed it, all is perfect!
What if i want it to be solo and multiplayer (so you can press a button to choose if you want to play against someone or against the comuter) how would you do that?
You would need an AI for that, which would be even harder to code than the game itself...
1 more video to finish
If it took you 100 lines of code , i will do it in 300 lines and still have bugs in it and finish it in a month
thanku for the source code
tHANKS aLOT
what code editor do you use?
Pycharm
is it different for mac? colours are not changing after win
11:46
😅
Can someone explain the for loops in this. I understood everything except the for loops😅😅
could you please show how to the scoring? i’ve tried since two days ago and still wrong.
when this :
buttons[row][column]["text"]
i know [row][column] get row and column but what is ["text"] does?
calls all the text from the button.
I have dropped a comment down below
Great video! but I have a question, I don't know why when is time for "o" to play and I select a button to put it and is not working only with "x". Does anyone know why?
what is your coding app
PyCharm community
@@user-fd4tv4kf4i thx
hi ive copied the code from the comments but the buttons don't change colour when I run it. its worth noting I'm using a Mac. anyone know why this is?
Didn't understand what the lambda command did, can you explain it quickly?
bro can you explain why buttons is declared this way
buttons=[ [0,0,0],
[0,0,0],
[0,0,0] ]
So, that it is easy to read for US I think
step 1 to defeating the algorithm complete.
i am a hater of you
i defeated the algortithm first
Don’t care + didn’t ask + cry about it + stay mad + get real + L + mald seethe cope harder + ho mad + basic + skill issue + ratio + you fell off + the audacity + triggered + any askers + repelled + get a life + ok + and? + cringe + touch grass + donowalled + not based + your a (insert stereotype) + not funny didn’t laugh + you “re” + grammar issues + go outside + get good + reported + ad hominem + GG! + ask deez + ez clap + straight cash + ratio agian + final ratio + problematic
No offense but, whenever I tried coding the game, I ran it to test and the top where it says like, “x turn”, it does not have a space and it is not changing to, “o”. Which is why I had to delete the whole program.😔😔😔
comment
can use [0,0] instead of [0,0,0]
Is it possible to get a copy of your tic-tac-toe-python-code?
check description and pinned comment
omg 888 likes