Apr 22, 2025
In this video we continue with the Python graphical user interface module known as Tkinter, and building an application for the classic Tic Tac Toe game!
This is episode 4 in this series for developing an in depth fully functioning practical game using the tkinter and python modules with pycharm as our IDE. This episode covers handling game over situations when the player wins or loses and reports the winner onto the screen! This completes the necessary functionality for a fully playable tic tac toe game!
Episode 3 covers writing the code for a computer opponent, or AI, to play against. This includes randomly selecting a position, checking to see if it is available, and writing the computer's symbol onto the screen when possible. This also covers tracking in a turn based system, and resetting the game when the max turns is met!
The 2nd episode covers creating a game board with the nine move squares available and creating a universal function that can handle inputs from any button to update with the symbol of whichever player just moved! The first episode covered widgets, labels, buttons, grid layouts, the pack method, and updating formatting and style for the app!
If you have any questions let us know about it in the comments and thanks for watching! If you found this or anything else on the channel helpful please subscribe to our channel and leave us a like on the video!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
Good luck with your code and thanks for watching!
what's going on you guys welcome back to
the channel and today's video is going
to be a continuation in our building
tic-tac-toe in uh python using kinter um
so this is an introduction to graphical
user interfaces
or guis and this is part four in this
series so if you haven't checked out the
first three be sure to go back and do
that if this is something you want to
follow along with line for line the game
we've built so far is uh you select a
character to play as and then once you
hit start it draws this game board
you go ahead and select characters the
computer plays against you
and then um
ultimately it just fills in the board
you can reset it but we haven't created
the code to track when the game is
actually over and if you win or lose yet
so that's what we'll be tackling in this
video before i do get into it if you do
find this useful
then please go ahead and leave a
subscribe to the channel and a like on
the video it helps me out a ton and if
you have any questions or comments just
be sure to let me know about in the
comments below and i'll get back to you
as soon as possible
all right so for this video we're going
to be
handling game over scenarios so i'm
going to start by
creating a new variable and i'm going to
call it game over and we're just going
to set equal to false when the game
starts
and then
when the board is drawn as well because
this is kind of where we handle
this is where we handle uh reset logic
so like after the game is over you'd hit
the button again if you wanted to
play another round so we're gonna do the
same thing we're gonna set equal to
false when you enter there
but then both places that uh
both places that turn can happen so
whether the player is taking a turn or
the ai is taking a turn we're gonna call
a function that i'm gonna call uh check
game over and we're actually gonna use
that to check game over status so
we need to pass in the positions table
because we're going to send it all of
the moves that have been made in the
game as a variable for it and we're
going to use this thing to actually
check the status of game over so let's
go ahead and copy this
line here
and another thing you know how before we
were saying well if it's the player's
turn and there's less than nine turns
total we want to let them take a move
now we're going to say also and game
over
is false because there's no sense
letting the player or the computer take
a turn if game over
is true so uh we'll include that in our
player function and our ai function as
well
we'll go ahead and do the same thing
here
and game over is false which means we're
going to need to call in the game over
variable you could absolutely all these
things i called in as globals you could
absolutely pass minutes parameters
that's totally valid too some people
would even argue it's better i just do
it this way because it works for me and
then we're going to use the exact same
check game over here which means we have
to obviously come up and make
the game over checker
so let's call it define check game over
right and both of them are going to be
passing in the positions table
so we will do the same and actually
since we're going to be typing it quite
a lot i'm just going to call it pos
because as you'll see once we get into
it there's going to be a lot of
different conditions that could make you
win and so it's going to save us a lot
of typing time to just be able to say
pos
so we'll pull in game over as well
because that's what we're actually
checking and setting
but now let's take a look at scenarios
that could actually end the game
so the first one i'm going to take a
look at is actually just the first row
across so that's
zero one and two in your positions table
and if you add those three together and
you get xxx then uh that's one scenario
where the game is one right and then a
second one would be the second row so
three
four
and five and so if you get what's going
on here um you might wanna skip a couple
minutes into the video because i am just
going to go through each scenario that
could end the game and type in
that scenario so like next is the third
row
six
seven
and eight
equals xxx and i'm doing an or statement
and then a new line at the end of each
of these it's just a lot cleaner than
getting like run-on lines of code here
so we've handled the three horizontal
conditions let's go ahead and add now
the three
vertical ones so this is going to be
column one which is going to be zero
three and six
and you can see with how many
freaking
times i would be typing in positions
calling it pos saves actually a lot of
characters over the course of this
conditioning
so then column two is going to be one
four and seven
and then
equals okay xxx
and then the third
vertical the third column is going to be
two
five
and eight
okay
and now there's only two more conditions
right the diagonals and so the first one
we're gonna say you start in the top
left you go through the middle which is
four and then down to the bottom right
which is eight
and
then
the other one would be let's say i have
a space there the other one would be you
start in the top right which is two
you go through the middle which is four
and you stop in the bottom left which is
6. okay
so that was a lot of
typing just to kind of handle this but
it was really important because these
are all the different ways that x could
win
so it's one
big ass if statement to get into this
but if that happens what we're going to
do
is we're going to set our game over
variable equal to true
and then we are going to put a label on
the screen
which i'll call win label and that's
going to be equal to label and we're
going to put it on root and then we want
the text to read
x wins okay
and so uh we'll go ahead and
draw it onto the screen in our grid
right here as well just to cut down on
total
rungs of code
we'll put it in the middle there
okay but the good news is because we did
all this right here we don't have to
like think through all the conditions
again for the o's right we can just come
in and anywhere that we had xxx we can
change to ooo and i'll just go ahead and
copy this
and i'll make it an l if um
in theory whichever one of these happens
first should take care of it but it's
just good practice if you want two
things to be mutually exclusive
make it an l if instead of just a
regular if okay
so we'll go ahead and replace them all
with o's
and now let's go ahead and say
x wins instead that's going to say o
wins
and then if neither of those are true
then we will just
say game over is false right which it
probably was already false if we're
running this function but that's okay
because
that's okay because now we're handling
all three scenarios which is basically o
wins x wins or no one wins
and let's return game over because
that's what we were checking here right
okay so
let's see that might do it
let's go ahead and run it and see if we
made any mistakes here or if we've got
it
alright let's play as x let's try to win
here
i'll just go here
x wins okay very cool let's start it
again
let's see if we can make the computer
win
o wins already nice and you can't take
any moves after that and you have to hit
start to reset the game board
so i would say that's working pretty
good obviously we're not keeping track
of like total score we haven't made the
ai think or anything and probably most
importantly this game board is ugly as
sin so in the next video we're actually
going to finally improve the aesthetic
appearance of our app but you can see
we've created like a working
tic-tac-toe game
that handles wind conditions and
everything so hopefully you found that
useful if you did i really appreciate a
like on the video and a subscribe to the
channel and as always thanks for
watching and good luck with your code
thanks bye