13 Video Views·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 3 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 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 last 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 you code and thanks for watching!
what's going on you guys welcome back to
the channel
let's redo that
what's going on you guys welcome back to
the channel today's video is going to be
a continuation in our building graphical
user interfaces in python with kinter or
t kinter and if you haven't been
following along so far what we've built
out thus far is just kind of the shell
of what will be a tic-tac-toe game
we built it to where the user can select
whether they're going to be x's or o's
and they can take their turn
but we haven't built out any sort of ai
computer taking a turn back at you yet
and obviously we haven't spent a lot of
time making it aesthetically pleasing
that's because we want it to be a
functional game before we worry too much
about it looking pretty
so today we're going to get into how to
create that ai opponent but before we
get too into it if you have been finding
this or anything else on the channel
useful i really appreciate it subscribe
to the channel and a like on the video
helps me out a ton as i get started here
and without any further ado let's go
ahead and take a look at how we're going
to do this
so first thing is we have to build a
turn-based system right something that's
going to track whether it's your turn or
the computer's turn so i'm gonna create
a new variable and i'm gonna call it
turn and i'll start it at one you could
start at zero i'm gonna kind of consider
the player one in the computer zero and
so when the game starts i want the
player to move first so i'm gonna set it
equal to one and then i also want a
variable called turns that's going to be
the total number of turns that have been
taken and that's how i'm going to track
whether the game is over or not because
obviously in tic-tac-toe you go until
the game board has been filled
so when that gets to nine um we're going
to stop letting the player or the ai
make a mega turn so then just like we
had player character we're going to want
to create another variable and we'll
call it a i character and
obviously anywhere we define
the player character we're going to want
to come in and just define the opposite
character as the ai character and if you
think about how we're drawing the
buttons
um it should make sense that we need
both of these because we have to when a
a the player or the computer take a turn
we have to be able to update the button
with whatever text
that player had so we need to track both
of these it's not enough to just track
one
okay
so there we go we are saying when we
select x
for our player the ai is going to get o
and when we select o the ai is going to
get x
and we define those as global so we can
track them outside of the game
and then what we want to do is come down
and where it actually updates our table
and draws the new button we actually
want to make this conditional
on the turn now being equal to zero
and the total number of turns being less
than nine right
because when turn is equal to one it's
the player's turn to go and when turns
is less than nine total that means the
game's not over yet so if both of those
things are true
then the player can do this but actually
we want to
um
we want to add a conditional to
this as well
and only say if
positions
at position
is equal to
position
is equal to this dash right that's our
like default blank character that's the
only time we want to actually let the
player take a turn because now we have
to deal with player trying to like
overwrite where the computer played or
where they already played
so with both those conditions we should
be handling the only time we actually
want to let
them update the screen
and then what we also need to do here is
we also need to say that's when the turn
variable goes to zero so we're saying i
just went and now it's your turn to go
and because we are referencing a
variable that's outside the function we
need that to be a global
and we'll also want to increment our
turns counter so we'll want to get that
in here as well
so we're saying turns is now equal to
zero
or turns you add one to and turn is now
equal to zero and this is when we'll
this is when we'll call a function that
we'll call a i turn
okay
so we're saying okay the player has just
taken his turn now it's time for the
computer to take their turn so let's go
ahead
and take a look at how we are going to
define ai turn
so just like in a player's turn i think
we need to track
turns
and just turn as well
so let's start there
and let's see what do we want to do when
the ai takes a turn well i think to
start we could make a smart ai that's
kind of checking for two in a row and
looking to block that's going to take
probably like a 40 minute video in its
own right so today we'll just make it um
random it's going to randomly select
integers and as long as it's a blank
space it'll take its turn there so it's
kind of like playing a little kid in
tic-tac-toe where they're just filling
in spaces they don't really know why but
we're going to build a simple ai for
this one
so now we're going to say while turn is
equal to zero
and turns is less than nine so this is
pretty similar to what we just did
for the player
and while the turn is equal to zero and
turns is less than nine let's make a
local variable called ai select and
we'll set it equal to um the function
rand int and it's going to take that
from zero to eight
because those are the total positions
right
and rand int if you don't have the
random module in your python file go
ahead and say from random import rand
int that's just letting us give it a
range of integers to select a random
variable from
so
now it's going to say
if positions at that ai select variable
we just grabbed
if it's equal to
just like in the player turn if it's
equal to a dash that means it's blank
and now we can go ahead and actually
build out the logic to track
um
to track whether or not we can
update it
so
we just said
pick a random position in that table of
positions and then as long as it's blank
what are we going to do now we have to
check what the position was because row
and column
needs two variables and that position is
just one variable
so we're going to go ahead and say if a
i select and i'm actually going to grab
all of our positional tracking code from
here
to tell us where what the rows and
columns are because we already did this
and all we have to do is say instead of
if position at position
we say if the value of a i select and so
i'm going to just anywhere that we had
position for the playerupdate function
i'm going to replace with ai select
because it was just the same it was
passing
one integer 0 through 8 into that
function and then it was converting it
to row and column information
okay and so what we're doing now is
we're just doing the same thing with an
automatically generated computerized
turn
so this little ditty right here is going
to convert all of our
computers turns that are just coming in
as one number it's going to convert into
a row and column coordinate system
and then once we've got our row and
column data we're also going to steal
the new button logic from
uh from from down here and so we'll say
okay ai turn
now we've got our rows and columns and
let's draw a new button again instead of
position we're gonna pass in ai select
but since we used the r and c
same
same syntax we can keep that but instead
we're going to set turn equal to one now
so we're passing it back to the player
and then we're going to increment turns
as well
so that might do it the only thing is um
we want to build in some reset code so
once like the game is over
um you should be able to just hit the
start button and have everything clear
out and have it reset so let's go ahead
down into our draw board function
because both of the start buttons
call this draw board function
and what we're going to do
right when you call draw board
we are going to reset it so that
regardless of
um
what happened in the previous game turn
is going to be equal to zero
and that or turns is gonna be equal to
zero and let's say turn is going to be
equal to one and we'll put it back in
the player's hands
and then the positions make sure you're
calling global positions but the
positions table is going to reset to all
blanks as well so once a game is done
and the board is all filled out you just
hit the start button and it should clear
out
so this was pretty quick i imagine we
might run into some bugs when we run
this but let's go ahead and see if we're
playing against a computer now okay
let's play as x start
so i took my turn and it's letting me
keep taking turns
huh all right let's take a look at
what's going on here
okay sorry about the confusion i did
some uh troubleshooting it took a few
minutes here but uh what i realized just
a stupid line uh that i forgot to do we
checked to see if it's blank but we
never actually update our positions
table with the ai's character and so it
thinks it's taking a turn but we never
told it that it actually has to uh
that it actually has to update our table
of um who's gone where with the ai
character so hopefully that clears
things up i was printing out um like the
what turn had been taken in the
positions table and trying to figure out
um
why nothing was working but let's go
ahead and run it now x start
okay we see the ai's playing against us
it is just random so you can see uh like
they're they obviously weren't blocking
us um when we got two in a row
but uh we fill in the table it doesn't
uh it doesn't crap out even though like
right now the ai would see that it could
be continuously running that um loop
so let's see yeah and we can clear it
out and we can restart it um again
we'll handle wind conditions in the next
video and we'll handle actually making
it pretty once the game is functional
and handles everything from start to
winter to to reset and everything like
that flawlessly
but for this video we learned how to
make an ai that we can actually play
against and that's pretty cool so if you
have any questions about what you saw in
this video today or anything in
particular you want to see in future
videos be sure to let me know about in
the comments below again if you've been
finding this or anything on the channel
useful i really appreciate a subscribe
and a like on the video it helps me out
a ton and as always good luck with your
code and thanks for watching thanks bye