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 2 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 creating a game board with the nine move square available and creating a universal function that can handle inputs from any button to update with the symbol of whichever player just moved! Last 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 you code and thanks for watching!
what's going on you guys welcome back to
the channel and today's video will be a
continuation in our building a uh gui
with kinter in python and building a
playable tic-tac-toe game so if you
missed video one in this series be sure
to check it out
we made a game where
right now it just has you select what
character you're gonna play as and then
draws a start button onto the screen so
we were learning how to create labels
and use the grid layout in kinter
as well as create buttons and how to use
the command instruction for buttons so
if you haven't checked that out be sure
to go check that out
if you need some basic python
introductions and tutorials on
object-oriented programming be sure to
check that series out on the channel as
well and as always if you find this or
any other content useful i really
appreciate a like and a subscribe it
helps the channel out a ton
so what we want to do today is we're
going to work on actually drawing the
game board and then handling a player's
moves as you select
what button you want to play as
so uh once you say you want to be x or o
then you have this option to hit start
we're gonna go ahead and the next thing
we're gonna do is draw the game board
which will just be uh nine buttons in a
three by three grid
um and let's just go ahead and start
doing that so i'm gonna create a
function for on the start button and i'm
going to call it uh just draw board
draw gourd nice
draw a board and we're going to put that
on both start buttons because whether
they select x or o um it's gonna still
draw
the start button and so we want it to
run this function either way but now we
have to actually go make the function
for draw board so let's go ahead and say
draw
board
and i'm thinking let's just uh do it as
nine buttons um if we could set up a for
loop that's gonna run through and do
everything for us
but uh i i'm just gonna define all the
buttons here um so let's go ahead and
create a table that i'll call
positions
and to start this is gonna be what
actually keeps track of like what has
been played in each
in each space and so as either the
player or the computer picks
a space for x's and o's
then we are going to update this table
so that it's not just dashes and will
actually overwrite it with either the
player or the computer's character
so we just want for now just a string of
nine dashes as a placeholder and that'll
mean that they haven't been played
and then let's go ahead and actually
create the buttons i'll call
i'm going to use t for top and then l
for left
and then
i'm not even going to say button i'm
just going to say top left and we'll
make that a button with the
text that is going to be equal to our
positions table that we just made
and then this will be spot zero right
um oh you gotta say root tell it where
to draw it first and since these aren't
super complex um i'm gonna put the grids
uh right here as well
um
as we talked about in the previous video
a lot of times it's a good idea to
separate out your grid and your button
definition
but i think that this is going to work
just fine so let's go ahead and start
with row five column
zero
and i'm gonna copy pretty much this
whole thing because next we're going to
have
top mid and you can give these
much longer instruction names if you
want to give them variable names that
say actually top middle button or
something longer you can that's a fine
idea
i'm gonna use shorthand so i can do less
typing and get through more in this
video
so again take those three buttons top
left mid and right
copy them down for
middle left
middle middle
and then
middle right
and we need to update these from zero
one two to three
four
five we'll keep the columns as zero one
two and we'll make the rows six
six and six so you can obviously see you
could do this with two for loops and do
like four i in range nine and then do um
you know 0 1 2 0 1 2 0 1 2 for each of
the rows i might do that in a future
video just to show both are valid
but
i'm going to do it this way so that we
can kind of show
the difference in each button and i also
want to do it this way because they're
each going to have a different command
right so they're all
going to
write to a different button
with either the players or the
computer's character
so we're going to say the
command is going to be equal to a
function that i'll call
player position because if the button's
getting pressed then a person is
pressing it
but the problem is i want to just create
one function that works for all nine of
these buttons and not have um not have
to create nine functions for nine
buttons that basically are clones of one
another they just write to a different
button so what we're going to do is
we're going to use something that we
haven't talked about yet called lambda
and that's how you actually
call a function that you then pass in
parameters to because if you we haven't
talked about this either but if you
do a command line
just like this command equals player
position
and try to pass in parameters right here
it won't let you
it's not
valid this way you have to use lambda
and then separate it with a colon if you
want to do something like that now it's
giving us a squiggly red line because we
haven't defined the function player
position yet so let's do that while
we're doing this as well and let's say
player position and it's going to take
one argument which we'll call position
and what we want this to do
is
we want to update
the positions table
at the position that we pass in
and we want to set that equal to the
player's character
right
and you see we're getting this red
squiggly line because we define that
table inside of the function initially
so let's just go ahead and create it in
the beginning as well
so that it can see it inside our
position table alrighty so we do the
player's character there
and then
let's just
overwrite whatever button was in that
space with uh with a new button
where now the
it's still on the root but now the text
is going to be equal
[Music]
well just like before it's going to be
positions at
position
that needs to be square brackets though
position
and let's see i like that uh but then
let's draw it and what we're going to do
for row and column so that this works
for all of them
um we're going to set up let's do it
this way let's say column is going to be
equal to c and rho is going to be equal
to r and let's create some code to
handle
what r and c are going to be equal to so
let's say if
position is greater than
position
it's greater than or equal to
zero and less than or equal to two right
so it's between zero and two then we
want r
which is the row to be equal to zero as
well
and then else if
um
and then let's copy this whole jazz
else if the position is between three
and five we want the row to be equal to
one right and then we could do another l
if but since there's only three rows
we'll just say row is equal to two
um and then we need to be a little bit
more precise in defining column because
we can't really use ranges right because
the three positions in the table that
are going to be in column 0
are going to be and i'm just adding
spaces for python syntax you can leave
it if you want
um but then let's do
if position
is
positon sounds cool
if position is equal to zero or and
let's do position equal to
zero
one
um or the third one would be
so it's zero one two and then it's three
four five and then
six seven eight and so that'll make the
column equal to zero but then we need to
do
two more of these right we need to do an
l if and then we'll do
an else
just like with the rows so what we're
saying now is every third position
so 0 3 6 1
4
7
and
this would be 2 5 8 but you can just do
it this way
that corresponds to the column so now we
can make every
um we can make every button call this
same function when it's clicked just
with their updated position
so that's what i started doing here
command equals lambda
and player
position at you know whatever zero one
two so i'm gonna go ahead and copy this
through
just to speed things up so you guys
don't have to see me
do this slowly eight times
um when we were making the buttons
actually didn't finish
defining these stops at five
so this should be
six seven eight obviously
six seven eight
this should be
seven
seven seven
and zero one two then we want three four
five
six
seven
eight gotta go back to five
there we go
all right so i think what we've done
here is we've said when the start
buttons are pressed it's gonna draw a
board that's just gonna be a bunch of
blank buttons to start um
with these dashes in them and then if we
go through and we click then it's going
to update that button with our clicked
character so let's see x let's go ahead
and start all right so we get and we'll
work on making it pretty in the future
but
with code you always want to make it
functional before you make it pretty so
we get this little uh 3x3 grid let's see
oh
looks like we maybe defined rows and
columns wrong let's take a look here
oh yeah it's because we're not doing
rows uh zero one two we're doing rows
five six and seven so let's try that
x start
um
it appears that we got the columns wrong
so the problem we're getting right now
is because i made this just a second if
statement it needs to be an l if
statement
that's why the first column wasn't
reading properly
it should be able to scan all three of
these now if we just have the l if so
let's go ahead and try that out
x start
okay thank goodness
now obviously you're probably noticing a
few of the
flaws so far you can still change your
character mid-game the computer's not
taking turns and the board is super ugly
but we've created a game where
you know you can
draw your characters on so what we'll
need to do in the next video is take a
look at creating like a computer's turn
a turn-based system
as well as blocking it out so you can't
change your character mid-game and then
ultimately we'll be taking a look at
win-loss scenarios as well so for today
we drew our game board and created
buttons that update when the player
clicks on
so that's pretty cool hopefully you
found this useful stay tuned for the
rest of this series
where we'll cover the rest of it uh
start to finish but again i hope you
found this useful thanks for checking
out the channel if you do want to see
anything in particular in the future or
have any questions about what you saw
today let me know about in the comments
below again i really appreciate a like
and a subscribe it helps the channel out
a ton and as always good luck with your
code and thanks for watching thanks bye