[Music]
hello good afternoon on a Wednesday it
is the cutting train here oh and why why
might you be asking do I have to train
whistles today I mean first of all why
should any grown person on the internet
have train whistles I'm not really so
sure but apparently this is what I'm
doing with my life and so there it is I
am a person on the internet with not
just one train whistle but a second
train whistle albeit a slightly sadder
train whistle let's give this one a try
well actually that's not so bad
so you might be asking why do you have
multiple train whistles well thanks for
asking I am going to embark on a new
adventure in working on learning how to
use a laser cutter in particular I want
to learn how to do laser etching and I
have this YouTube membership thing and
I'm about to relaunch all of the
membership rewards and one of which will
be your very own
laser-etched coding train trade whistle
which you can only get exclusive oh the
other thing that's quite exciting here
is I have some new tricks I'm gonna
let's see if this works I have a new
camera here you can see my stream deck
and iPad but mostly I have this here
because now I can show you this
train whistle so this is what I've been
using for all these years purchased off
Amazon and now I have about a hundred
and fifty of these in a box in my office
and I will be laser etching my own
coding train train onto here and mailing
them out with the help of psy who is the
coding trade community manager in 2020
so if you I never had a reason to join
as a member today before you can join
today I also thought you know I could do
some more cubing coding cubing with my
beginners method of solving a rubik's
cube maybe I could talk you think I
could talk and solve the cube at the
same time can you even see what I'm
doing here
I suppose this probably isn't what I
should be doing right now but hi I am a
an amateur I have a have a love a love
and affection for Rubik's cubes I'm I'm
not sure why exactly I just can't help
myself what am I doing here
I'm trying to solve it I cannot
apparently I cannot solve and talk at
the same time it's much too difficult
for me to do that and all of you who are
actual kuipers out there probably in so
much pain right now watching me do this
with my methodology that is but do you
know I've spent not too much time in my
life studying how to solve a Rubik's
Cube I wish I had more time because
honestly I would like to spend all of my
time doing that which might just be kind
of an escapist fantasy a reason to like
completely leave the world and universe
that we're in right now which feels like
I often want to do but I shouldn't I
want to stay here I want to be with you
the people the people of the code the
passengers if you will of the coding
train
and hmm you know I want to enjoy this
earth planet thing that we're on I can't
even talk when I'm not solving a Rubik's
Cube and apparently while I am solving a
Rubik's Cube is a much harder it is very
hard for me to put words together do you
think I could code and solve the Rubik's
Cube at the same time I feel like I just
have to finish this now but I can't stop
talking so good play music
[Music]
Oh cubing in the Cabana coming soon
no baby
[Music]
what now unless you think that's
exciting I will show you now what I've
been working on I will show you what
I've been working on which is my new 9
by 9 cube which I am attempting to turn
into a 3 by 3 you can see that so far
I've solved the white centre the yellow
centre the green centre and this morning
when I was having my tea in the morning
I was working on the red centre I have
solved this before I took me about a
week you know a little pockets at times
here and there so you know maybe maybe a
little later today we'll work on this a
little bit more all right now I just
wanted to show you my new overhead
camera in case we have more things like
this train whistle and stuff for me to
show you I have that I also have up here
in the top left oh wait how come that
didn't go away there we go I have up
here in the top left of the screen a
stream labs alert box so I don't know if
this is a useful interesting thing to do
but I'm learning about the world I'm
like the old man livestreamer learning
about all the young people's fancy
tricks and so supposedly if somebody
subscribes to my youtube channel called
the coding train right now their name
will appear right here if they join as a
member their name will appear right here
and I believe a super chat but I don't
know how I feel about the super chats
that's a little those make me
uncomfortable you know I'm very grateful
and thankful for everyone's support some
people are asking me about the mouse pad
how do I get this back up here overhead
there we go
so this mouse pad is a drawing of a
house made by my daughter who is now 8
years old I don't I think she might have
been younger when she made this it
doesn't say on the back but there one of
those things when your kids make an art
project at school and then there's a
fundraiser for the school you could buy
their art on like a mousepad and so I
did
okay overhead off overhead off so Kevin
writes I subscribed five minutes ago and
it did not show mmm well five minutes
ago it should show instantaneously so if
you subscribe five minutes ago was I
live-streaming was it up there I don't
know all right all right everybody relax
let me test my banner here I'm gonna
make a little test of it I've got a
little test going I'm gonna test
subscription oh you're right it's not
working let's fix this this is this is
the important thing that I must work on
today oh I turned off subscribers I
turned off subscribers because that
seemed like a lot of noise let me turn
that back on
copy oh whoa somebody just subscribed it
appeared I don't know what I just did
but it worked now I don't know what I
did but it happened
somebody subscribed and it appeared that
was very exciting so maybe I don't need
to fix it hold on let me look at this
here just everybody just just just
afraid sponsors superjet hold on hold on
there oh yeah so I think it's working
all right thank you Akshay R for
subscribing to my channel what's this
from I think it's maybe like is it a
Futurama quote I'm not sure I like your
ideas and I would like to subscribe to
your newsletter maybe that's just from
The Simpsons
I don't know I'm I'm getting a very loud
I'm hearing like a ding dong when
somebody subscribes do you hear that or
is it just coming out probably is coming
out of the monitor of this computer down
here yes so I think I agree I think I'm
probably going to turn off the
subscriber oh but new member let's see
if that happens
yetsko fall new member maybe you
depending on your membership level you
might get a train whistle it didn't show
up alright I'm gonna fix this everybody
hold on
[Music]
alright alright if I've done everything
correctly
um now it shouldn't show up anymore as
people subscribe but only if they join
as a member
we'll see we'll see if that works oh
technology okay let's see hmm okay so I
should really get to the business at
hand which is that I am here today to do
one thing and one thing only I am going
to attempt to implement the minimax
algorithm to create a a I don't know if
that's really sometimes you say the word
AI and it sounds like oh this like fancy
artificial intelligence robot thing
we're really it's just an algorithm to
pick the optimal move but AI you know
it's critically great so I put it in the
title an AI bought to play against me in
tic-tac-toe and as you saw for my
Rubik's Cube solving prowess I'm
probably very good at tic-tac-toe new
members are joining or are they are they
popping up did that did the alert pop-up
I don't actually see it no oh so no and
so far no overlay I I guess I'm just
destined to never have this overlay
thing work hold on ah thanks for showing
my email oh wait a second wait a second
I am not logged in as the coding train
[Music]
okay hold on everybody
I'll fix this it's showing you an
overlay for some other YouTube channel
associated no no associated with my
Google account I will fix this now I now
know what the issue is
I am the coding train that is me I am
logging in I'm going to my dashboard I'm
going to my alert box I do not want to
join stream Labs University thank you
very much
alert box dink dink dink dink dink copy
I'm going now to open broadcast studio
into the Alerts property pasting in a
new URL I'm hitting okay I'm going to
refresh automatically I'm going to press
this button there we go
coding screen is become a member coding
train is donated super chat and you
don't see a subscriber because I have
that turned off okay you so now now it
should really work I hope
thank you Humpty Dumpty for joining
where is my Bell I will ring the bell
for your join don't fall off the wall
it's in front of my face this is the
thing but I have a very special talent
which is to not see things that are
right in front of my face Thank You
Humpty Dumpty and thank you to a new
member
EMS Koval a welcome you have the nice
lovely little VIP this dot icon okay so
what else do I want to talk about so
before I do the minimax algorithm and
talk about what minimax is let me
revisit the most recent video tutorial
published on the channel which was the
ml 5 jazz train your own neural network
now if you watched the live stream where
I made this tutorial but did not watch
the actual tutorial video you might this
is one of the rare cases where you
actually want to watch this video
because after the livestream irie
recorded it the the edited versions of
things that are alive stream always have
not always but often have a little extra
bits or some you know light editing to
speed through and skip things but
sometimes I will actually come back and
rerecord an entire thing or an entire
section if I got it really wrong and
David Snyder's that's like big I did it
show up did it show up did it show up I
want to know cuz I didn't hear the ding
I think I want to heard the ding oh it
did it did it just was late it did it
showed up late so you haven't seen it
yet you mean you've seen it now if
you're hearing me talk but you have to
remember that I'm on a thirty third
there's like a big delay here okay so
check this out if you're interested I
also so in addition to doing these live
streams like you're watching right now I
will often livestream to members my
different recording sessions if I'm
doing extra material or some extra
tutorials not part of a live stream and
just yesterday in fact I had a members
livestream where I recorded three more
videos all follow ups of this one so
this in this particular example I did
the data collection training and
inference and then the videos that will
be coming out are the videos that will
be coming out are looking at how to save
the data that you've collected how to
save the model that you've trained and
then also how to implement regression so
how to change this into a regression
okay so um take a look at those let's
take a few minutes to look at some of
these community contributions I should
also mention speaking of things I have
this keyboard that I'm using today
because my butterfly I think it's called
the butterfly keyboard on this Mac is
just you know it's dead it like it just
the it's like completely unusable
so I'm also I'm attempting to use an
external mouse an external keyboard
today and hopefully that will improve
things in terms of my coding and all of
that
all right overhead off so let's take a
look at some of these so firstly I
haven't actually looked at these so this
is a surprise to me turn my screen
brightness up a little bit so I can see
it let's look at the self learning
neural network by Vlad okay oh whoa wait
a second so this is interesting because
this looks like it is based off of my
cars neuro evolution example which I
built during a different livestream and
only for Windows so I guess it's just
the executable and we could run it is
this using gamma 5 Vlad if you happen to
be in the chat I have a bunch of
questions about this for you that I'm
curious about but I will move on right
now to the next one
neural network visualization after
training wait awhile by Charlotte ok I'm
excited about this so let's take a look
at that ABCD so this looks like it's
just like my example so I'm gonna do
some training here can you hear that
are we hearing anything maybe there's no
sound in this one it's using color
instead of sound so let me do a bunch of
bees a bunch of C's I would love a
community contribution that thinks about
a more elegant interface for doing this
data collection but let me do this and
I'm going to now hit T for training and
we'll wait for the model of the tree
so Simon is letting me know that I
missed showing contributions for
interactive drawing and teachable
machine and I would be glad to look at
those maybe I'll look at those at the
end if I have time because I want to
make sure I get the minimax did this
train
okay so Charlotte's instructions say Oh
Charlotte did what I was so we see like
a heat map or decision map of all the
different colors based on the training
data and it's kind of amazing how it
actually ends up producing this like
Voronoi like tessellation I would I
should really try this again with a less
obviously clustered data set if you'll
oblige you'll humor me I would like to
do this one more time so I'm gonna put
the I'm gonna make the data much noisier
in terms of where it is like I'm still
going to kind of cluster it but I'm
gonna spread out some will make it a
little bit more chaotic you know
traditionally you would do something
like what we're looking at here with an
algorithm like k-means or clustering
kind of algorithm it's interesting that
we're using a neural network here to
ultimately kind of perform the same kind
of idea now I expect the loss won't be
as low because there's gonna be a lot
more errors it's gonna be a lot trickier
for it to figure out how to train this
model but let's give it a try
the loss is definitely going down that's
a good sign
[Music]
interesting oh wait how do i do my zoom
thing I still have to use this are we
seeing it's interesting it really is
very consistent still so I'd be curious
I you know I wish I had hours to unpack
it do a deep dive into every single one
of these examples I'm curious why this
took so long it's a very slow process if
every single pixel was passed into the
neural network and classified one at a
time ml5 will allow you to pass in a
batch of data and get the
classifications for all of them so
Charlotte if you're watching I don't
know that you are take a look at that
that might improve this speed but this
this is a tricky one or maybe using
promises and some kind of like
asynchronous something-or-other could
help too
alright let me close this what else we
got here we have now regression based on
note frequency and color predictor okay
so let's see what I'm not sure to be oh
I like this I can paint with it that's
really nice I can paint so this is a
nice improvement on the interface I
don't know how many options there are
let's try this and hit T drain this
model mmm there's a little weird and I
don't see a graph at all oh yeah so
Simon makes a good point
I said k-means in the sense that k-means
will find the clusters without the
labels so I think I was conflating some
concepts there and just sort of like
thinking about the visual relationship
between that example and k-means example
but it actually is quite a different
thing so thank you for correcting me
there
all right what's just happened I don't
know so something has gone wrong here at
this example so I I don't think I'm able
to effectively debug this right now but
thank you for the submission
darshan have you happen to be in the
chat let me know oh so much discussion
about minimax and Q learning and what
should I use what the Spade la so much
good stuff to come okay so thank you for
these submissions stay tuned hopefully
sometime next week
the follow-up videos for this will be
released and but now I'm going to move
on to the main tasks of today and before
I get started with that what I would
like to do and hello to Zul from
Indonesia thanks for watching and CJ is
here hello CJ CJ from well from the host
escape decoding train garden but that's
not it it's the coded garden wonderful
YouTube channel I would like to download
this code and get myself set up with it
I think I'm gonna work excuse me outside
of the p5 web editor because this is
gonna be a pretty complex problem for me
to solve so let me download this p5 code
[Music]
so for any reason you want to follow
along you should you can doubt you'd
start with this starter code that I'm
about to use go in here let me call this
whoops mmm we call this mouse it's like
my hand is all the way over here look at
this is crazy my hand is all the way
over here operating the computer look I
can type one character at a time and
they up here one character at a time
go away overhead desktop and then let's
open this with Visual Studio code okay
let's go over here and let me run my
terminal and I'm going to go to desktop
tic-tac-toe I'm gonna run a server and
load that up in the browser there we go
that's the example from before let's go
here and now okay all right so here's
the thing what I would like to do is I
want to make some adjustments to this
tic-tac-toe example that I won't include
in the final edit of the coding
challenge but I will do with you here
right now in a way I want to simplify
some things about it just to make I
think the but I really want to focus on
is the the core minimax algorithm and I
don't want to add a lot of complexity
related to the game mechanics themselves
so I'm going to assume that there's only
ever two players and I'm gonna say
there's going to be an AI player which
will be the NX and a human player which
will be an
and then I am going to what I'm gonna do
is let's get rid of this like available
stuff for a second and this like picking
a random current player that could be
useful let's keep the current player and
we'll say that current player is starts
with as the human for example I'm gonna
get rid of this idea of an available
array just to simplify this check winner
thing can be the same next turn so this
was picking a random turn so let me do
this I'm gonna leave let me make the eye
okay so what I'm gonna do is I'm gonna
add a mouse pressed function and if the
human player Oh No
if the current player is a human let's
get let's get the index into the board
let's get with the mouse X divided by
width Mouse Y divided by height I'm
gonna floor these so that their index
what I'm trying to do right now is just
create a quick interaction where as I
click the mouse if where I click the
mouse is an empty spot on the board then
I'm going to fill it in with my human
marking my human marketing which is an O
and otherwise if the human player is a
is it a if the current player is the AI
actually you know what I'm just gonna
I'm just going to make my turn
and then immediately oh this is silly so
every time I click the mouse I'm trying
to think through this what I'm actually
doing
I'll leave the extras are some extra
stuff traps in here so if it's a valid
spot then the next thing I want is for
the AI to make its turn so human
make turn then if it's a valid turn then
the AI should make its turn and right
now just have it be random so I
shouldn't have gotten rid of that
available thing but I think I can just
rewrite that by saying I can just look
through all of the spots this is silly
because I already used inj whoa-kay
ABCD efg hijk ll does anybody ever use L
huh X goes first what that is Thank You
Simon I will correct that because I will
have the AI go first
eventually and I'm here to break the
rules I'm gonna use an L by golly that's
my index and oh this is so now I'm
writing a whole extra algorithm that I
don't want to write available push I is
K j is L and want a little object no no
no no let's just do this okay L make a
little object and then I want to this is
so silly what I'm doing I should just do
it in order it's fine then I'm gonna say
move equals random available and then
bored Oh what only if it's available if
bored K L is blank this is so silly that
I'm doing that I did not intend to write
a whole new algorithm here as like a
random AI but it's what I'm doing and
then move is random and then bored move
dot K eat move L equals equals the AI
and then the current player is the human
I think this current player thing is
completely unnecessary but that's fine
and once the humans made a valid move
the current player is the AI and let's
see if this does what I want to do okay
what players is not defined sketch KS
109
if spa equals what I say human was Oh
even though I'm doing it wrong according
to Simon and AI is X available is not
defined sketch 49 because I got rid of
that concept and then added it back in
again oh I needed it for something else
let's just do this let open spots is
[Music]
weight is zero and then if the board I
can use IJ here if the board I didn't
really think I was going to change very
much about this example IJ if the board
is empty increase the number of open
spots I will explain what I'm doing a
second more in more detail then as long
as there are no open spots I can break
out in the loop there too so if there is
no winner and there no open spots
it's a tie otherwise there's a winner
boy this algorithm is ridiculous I
should also refactor that but I'm not
going to let's see next turn 139 I'm not
doing a turn in the draw loop I
shouldn't use the draw loop at all but
I'm gonna be lazy 139 I'm missing and
curly bracket W is not defined I made up
this variable called W which somewhere I
must use thought I created it because I
use it everywhere like in draw here it
is it's not a global variable so let me
make this a global variable but I'm
going to have to create it in setup
because the width and height don't exist
and also it's a square so I don't need
separate within Heights but what are you
gonna do oh oh
I win haha
I win again by the way the optimum move
I think is to go in the corner X wins
what no I won there's a bug fix that
another time haha I went again okay so
this is this is what I wanted except
there is an issue of okay so I have a
let me do this let me do this
ai ai move we make that a separate
function everything's gonna be fine
and then I'm gonna call this AI move
which was actually called like next turn
before so I don't know why I'm changing
it and then this ha ha I could have my
trusty I and J back thank goodness then
oh I mean I'm gonna do something even
better
by the way in case you're wondering the
whole point of this is I'm going to
change this to minimax so I want to
start the tutorial or the coding
challenge from an AI that just picks a
random move and then have it pick the
optimal move that's what I'm doing here
today so if that interests you stick
around if I can get this to work I'm
gonna be quite shocked right this was it
right here I already have this it was
called next turn so I could just call
this next turn that's what I had before
and you know I'm gonna do I'm gonna put
this in a separate javascript file I'm
gonna call this minimax j/s and I'm
gonna have it there I'm gonna add this
minimax
I don't like having these CSS files for
no reason it's just extra noise and so
now this should be next turn and then so
that X really goes first we'll have in
setup X go first now we will now have an
official competition Oh goober genius
[Music]
I just think all the good I just watched
into the spider-verse sorry this is like
totally non sequitur which I absolutely
loved one of my favorite I did not
expect to love it as much as I loved it
but isn't the thing called like a goob a
goober no goog there are 512 people
watching of me about to attempt to play
tic-tac-toe against the computer all
right I'm gonna get you X oh good move X
goes after me and wins then X is told it
wins oh let's try it again
here here in your ex ex AI man-ho new
member mwah thank you welcome thank you
for joining boy this it's uh this
overlay thing really really brings on
the new members or maybe it's just
you're excited about this train with all
I'll be mailing to you okay do you
already have a method to check of
someone one yes I do I will not be doing
no so I do love I do wonder if ticked if
I do this particular coding challenge
with minimax if that will lay the
groundwork for future videos as I look
at more learning styles and q-learning
being one of them for reinforcement
learning okay
could you check yes I certainly could
check the wig condition after YouTube I
don't feel like correcting that right
now that's just gonna be a bug that I'm
gonna leave in there
and but I am now going to get started
and I'm going to need heavy use of the
whiteboard so let me just test something
here cuz I also I think diagramming the
minimax algorithm I'm going to want to
use multiple colors so I have not coded
this before but I did do some reading on
it and thinking about it and I drew some
diagrams on a white board in my office
all right I'm I'm somewhat prepared for
this which I'm sure it will all go
horribly wrong anyway I'm just writing
on the white board up where you can't
see it to see if these markers work I
have a blue I have a black this is
probably gonna be somewhat of a long one
and I have a green okay though these all
work
I want a red also
the Reds okay let's try this red better
alright alright I'm gonna turn the
overlay off because I'm Deepak is
suggesting there's a number file video
about Tic Tac Toe I did not realize that
I will have to take a look at that cue
Sharad is asking won't you need to fix
the winning edition because that might
mess with the training I think it's
gonna be ok the issue is that I'm
allowing two moves to happen at once and
when I'm not actually there's no
training going on here I'm just doing a
search basically for the optimal move
and I assume I won't have that problem
add a set timeout to next turn all of
these things are very good ideas I like
that a little delay okay I'll think
about that but I'm not gonna do that
right now so here's the thing I want to
stick to the pieces of this that are
relevant for minimax
and in that sense I'm not gonna worry
about the interface and how the game is
actually played in that kind of thing
all right so give me a minute here I'm
about to start
but before I begin I'm going to tell you
about today's sponsor the random numbers
no that's something I don't have a
sponsor for today's live stream so nice
opportunity for me to just like goof off
and I'm gonna tell you about today's
sponsor numbers
today's coding train is brought to you
by ninety one thousand two hundred forty
to fifty three thousand four hundred
seventy four and thirty four thousand
nine hundred sixty three thank you
random numbers
so Armour writes in the chat I was
hoping to apply this algorithm for more
challenging games like chess and you can
and maybe we can have a little
discussion about that after I get
through this very basic scenario but I'm
gonna do hopefully what is a fairly
simple about simple but basic scenario
[Music]
1:45 I'm not really 45 into this oh my
god
45 minutes into this alright
okay
what's going on here there we go Oh
alpha beta pruning yeah you're asleep
but oh boy so many things to discuss
I'll come back at the end to talk about
those things all right hello and welcome
to a coding challenge
tic-tac-toe again but no wait oh no no
no I live streaming so I'm yeah I I've
kind of this thing has happened where
sometimes all of these recording
sessions were are which are member only
live streams where I really allow myself
to start over like 15 times and part of
my reason for doing that was so that I
would stop doing that so much during
what's a more public live stream
unfortunately I'm not able to switch
that muscle off and but I really want to
attempt to not stop start and stop so
many times but just go with it and much
a coding trained editor extraordinaire
will hopefully make this all wonderful
and easy to watch in about 30 to 45
minutes which i think is my target for
this ending up as usual coding just
wants me to see their comment I see you
I see you hello and welcome to coding
challenge tic-tac-toe the minimax
algorithm so you might remember me from
my codicology number 149 tic-tac-toe
where I made very basic somewhat what's
the word I'm looking for
messy and spaghetti like where I made a
very sort of like a big mess out of
tic-tac-toe but I made a working version
of the game which I have running here in
the browser now I made some very slight
adjustments to where I left off in that
last video so if you haven't watched it
you could go back and watch it but you
know it's a basic version of tic-tac-toe
I want to start over so badly rocket
landing stimulation I totally want to do
that d t6 no more starting over no more
no more starting over
just by
last time please please please
I will put my overhead camera on and I
will look at you from close up if you
let me do it well I guess got kind of
dizzy all right cuz I just want to code
this and I have to stop doing this
last time last time hello and welcome to
a Cody challenge tic-tac-toe
Oh with the minimax algorithm that's why
I'm here because I made this other
coding challenge tic-tac-toe where I
created a kind of a big it was kind of a
mess if I'm being perfectly honest but I
made a working version of the
tic-tac-toe game that just played with
two players picking random spots so
since then and you could check one of my
live streams if you want to find where I
did this I made some adjustments to it
so that I could as a human being play
the game so right now I'm gonna play the
random computer picker I'm gonna I'm
gonna go here and then I'm gonna block X
and I'm gonna go here I win Owens so
what I have the adjustment that I made
is that I added a mouse pressed function
where I find where did I click and I put
my human variable which is the letter O
onto the board and then I called next
turn where next turn picks a random spot
in the board and makes that the AI spot
or X's spot so the whole point of this
video is for me to implement something
called minimax
which is an algorithm a search algorithm
if you will to find the optimal next
move for the a I mean it could find it
for myself and then I could implement it
but the idea is I want this player the
computer player to beat me at the game
or at least tie to play a perfect game
and tic-tac-toe is admittedly an
incredible simple this will be an edit
tic-tac-toe tic-tac-toe is admittedly an
incredibly simple scenario and we don't
necessarily need minimax we probably
just memorize a few key principles of
how to play but I think
a good demonstration of the minimax
algorithm and if I can get it working
which I'm really good then I can at the
end of this video I can kind of discuss
how you might take that into more
complex scenarios even something like
chess potentially is a game that you
could use the minimax algorithm with
okay YouTube reminder fail what's that I
don't know what that means probably
something that I did okay oh that's just
TMC joining if the reminder probably
alright so let me go here you know here
Matthieu maybe we can we can replace
these later with clips but I'm just
going to let's look good no this is
geeks for geeks is the one is this a
reputable site that I should feel
happily yeah stop I'm not interested
okay let me go here one there we go okay
no go away dad stop go away all you ads
you're not paying me you need to show up
on my livestream okay actually the note
is technically not of today but as a
sponsor go to lynda.com slash coding
trade 20 for your $20 off coupon you
know not a sponsor of today's livestream
but a previous one all right
I just want to reference these if you
want to learn more about the minimax
algorithm I would suggest to resources
that you can look at that I actually
looked at before beginning this coding
challenge haven't programmed this before
but I watched this video by Sebastian
leg
that explains the minimax algorithm also
something called alpha beta pruning
which I'm not going to implement but
could be a good exercise next step for
you and I also found this article on the
geeks for geeks org website which has a
three part series about the minimax
algorithm and how to apply it to
tic-tac-toe
now those those resources if I'm being
honest are probably better than what I'm
about to attempt to do but I'm also
going to take about you know a good
portion of this video to try to explain
the algorithm in my own words and
diagram it out which hopefully will help
me figure out what code I need to write
so I think I want to try to do this
twice once within just a very general
scenario and I'm going to say that there
I'm going to draw what is wrong with
this marker I had such a good one before
I thought I'm so good better
the minimax algorithm is typically
expressed or visualized as a tree so we
can think of the root of the tree as the
current state of the game oh you can't
see that can you I usually put these
little things this is a little magnet to
tell me where the top of the board is
but they were a little bit too high I
don't know the camera position changed
okay oh is it till thank you for telling
me that it's tilted let me try to fix
that I thought maybe it wouldn't be
noticeable fortunately I do have a level
I need to tighten this camera let's see
how bad it is not as bad as you would
think oh it's bad it's way off okay
that's better
loose is the problem I need to tighten
it but I think I think that's better
looks better yeah it'll just be tilted
for that beginning explanation math you
can always probably I mean that that's
gonna be might be like a super annoying
thing to do but could fix it a little
bit all right
the minimax algorithm is typically
visualized as a tree and the root of the
tree is you could think of as the
current state of the game so in this
case it's tic-tac-toe and so this is the
state of tic-tac-toe now whose turn is
it let's say in this case it is the
computers turn wait okay wait I let's
think about this for so good hold on
better thank you
Oh give me one more try here at this
it's thrown off there for a second
the minimax algorithm is typically
visualized as a tree and the root of the
tree you could think of as the current
state of the game so let's say I'm just
gonna draw a box here this is the
current state of the game and maybe I'll
in a moment I'll fill this in with a
tic-tac-toe board but the idea here is
that there is a player the player that's
trying to pick the optimal score you can
call that the maximizing player so the
maximizing player is trying to maximize
their score - the game is going to make
the next turn so let's say that player
just has two options in tic-tac-toe it's
the start of the game there are nine
options but let's say there are just two
options that means we can have a tree
going to the next state now this minimax
algorithm is useful for a turn-based
game that's this minimax algorithm is
useful for a turn-based game so if this
was the players X's turn now it's the
players this in this stage it's the
player OHS turn and the player oh if if
X made this turn then oh now has two
options and if X made this turn then o
has two options so I said that X is the
maximizing player and I erased that and
I'm gonna put it back o is the
minimizing player this whole system is
designed to pick X's next turn but we
have to play out the future and the
future has Oh making turns as well then
X finally it's X's turn so once again we
have the maximizing player going and
then o's turn and so on and so forth now
this only works if the game well that
not only works with a game like
tic-tac-toe that you'll see is simple
enough we can actually have this tree
play out all the possible scenarios
until one of the players wins or there's
a tie in other scenarios you have to
just use some kind of heuristic to make
a guess as to like whether
No what am I saying here oh I'm wading
into territory I don't want to wade into
yet this is very important so let me let
me rephrase that so in the case of
tic-tac-toe so in the case of
tic-tac-toe it's a simple enough game
where at any given point we could
actually computationally play out every
possible scenario until one player wins
or there's a tie in other games like
chess for example there's too many
possible outcomes and we have to use
some other methodology to figure out
when do we stop this tree how far into
the future do we look but fortunately
for us right now that's not an issue we
have to deal with so eventually at some
point they're after one of these moves
the game is going to be over and so we
need to establish at any point when the
game is over when we want to stop while
going out through this tree what is the
score
so for us we could do this very easily
we can say if X wins we're gonna give it
a plus-one score if Y wins we're going
to give it a negative 1 score and if
there's a tie we're going to give it a 0
score so let's say in this scenario here
oh I probably should use like fine
explaining this right just use more like
generic scores this is fine I'm gonna
I'm gonna stick with what I'm doing yeah
that's a nice tip Simon to draw these as
different that's usually what's done to
draw these as different icons but I'm
not gonna worry about that Reyes was use
colors for okay so using these scores
let's say so
so using these scores let's say if X
makes this move o makes this move and
then X makes this move X wins so there's
a plus 1 but if X makes this move X
makes this move and oh sorry X makes
this move o makes this move and X makes
this move it's a tie so if that's the
case
so if that's the case I see what
to make this a bit more legible let me
actually make these blue so when it's
OHS turn I'm gonna draw these squares as
blue that will help us see and we don't
have too many levels here but this will
help us see so X this is X is turn it
needs to decide should it go should it
make this move or this move well it is a
X is the maximizing player X is the
maximizing player so it's going to want
to make this move so that means we could
take this score and put it here plus 1
and I'm gonna give this I'm gonna circle
this with green just so we kind of see
it as it's a winning state for X now
what if X makes this move o makes this
move and then X makes this move well
maybe in this case o wins we get a
negative 1 but if if X makes this move
it's a tie and and so we get a 0 so
which one would X pick it's going to
pick 0 right it wants X is the
maximizing player if we're going this
way it's going to want to pick the move
that yields it be hot the better outcome
in this case tying is better than losing
so this now gets passed up here and it
gets a 0 so it's interesting now is
remember o is the minimizing player so
which move is o going to want to pick Oh
doesn't want X to win so we're assuming
we're gonna we're assuming we're playing
against a player that's making optimal
choices so Oh
minimizing would pick this move so this
would now be have a score of 0 in it I
realize it's kind of confusing the zeros
and the O's so that's a 0 here okay now
let's just fill in some other numbers
just to do this more quickly
okay so so in this case X to maximize
wood sorry so X is the maximizing player
will want to pick the move that gives it
a plus 1 X as the maximizing player want
to pick the move that gives it a 0 oh
boy
oh this was a bad example let me redo
that what do I want I want I need
something that's gonna be different here
let's have it win so it's got to win
both of these basically all right so let
me feel we try filling this in with some
other values 1 0 negative 1 1 so X is
the maximizing player is going to want
to if it's here pick this move so that
it wins if it's here it's gonna want to
pick this move so it wins oh is the
minimizing player but doesn't have any
better options the minimum is still just
one out of these two picks this and now
X can make its decision we're done right
if every single possibility is explored
then X knows that this going this way
will produce a better outcome than going
this way so this is the move that is
selected and this is done over and over
again top right of the diagram shows why
instead of Oh for the yeah no well why
put a y there that should be an oh not a
why I don't know why I put up why there
I think I should try this explanation
again I think I should actually just do
it with tic-tac-toe I was thinking was
gonna do it twice
[Music]
how do people feel about that
explanation I feel like maybe I want to
try it again the thing is I was thinking
about describing it more generically and
then I was gonna actually do tic-tac-toe
but I think it would actually work just
to do it with a tic-tac-toe board and I
would give the tic-tac-toe board like
initial an initial state yeah
I mean I definitely want to get to
coding this
these do it again yeah do it again there
was a thing like inherently wrong with
that explanation I just I don't know how
helpful are useful it is I don't think
I'm gonna do a pruning I'm gonna erase
this though that's for sure you know
what I think I'm gonna
okay I think I'm gonna actually just do
the X
yeah it's confusing that they are both
one and one I think I'm actually going
to do the I'm going to explain it again
but I'm gonna use an actual tic-tac-toe
board because I think that'll be more
clear I think actually having that there
the theory of it is interesting but it's
not that helpful right now write it
lower okay yeah and then amy is
suggesting tried explaining with chess
let me try doing it just as I also think
like why do it twice I want this video
to not be super long and the live stream
will always exist so let me try doing it
again
so I'm going to act I'm going to act
acting thank you thank you acting I'm
going to act as if I just explained to
you the Sebastian leg is it leg or lag I
think he told me this I think it's lag
I'm just referenced those okay
so those two resources are probably your
best bet for learning about the theory
of the minimax algorithm I'm gonna
bypass that and just show you how the
minimax algorithm is applied
specifically to tic-tac-toe and I'll
link to those other resources in this
video's description and hopefully this
will give you a sense of what the
minimax algorithm is so the theory of
the minimax algorithm is that the
minimax algorithm is typically
visualized as a tree so there's the the
the root of the tree is the state of a
particular game I said I wasn't gonna
start over yeah yeah okay so those
resources are probably your best bet for
learning about the theory of the minimax
algorithm and how it can be applied to a
wide variety of scenarios but but I'm
gonna look at the specifics of
tic-tac-toe which is a great starting
example because it's a simple enough
game that I could actually diagram out
all of the possibilities which even if
that's computationally possible is very
hard to diagram and there's lots of
scenarios where you couldn't even
compute all of the possible outcomes all
the possible pop all the possible future
moves of a game and chess is an example
of that so so minimax is typically
visualized as a tree so the root of the
tree is the current state of the game
board
so let's see I'm trying to think of I
did this earlier today and what's gonna
be a good I want to leave three moves
open but I also want to leave it
possible for somebody to win so whose
turn is it
it's X's turn no this is bad
put o in the middle X X
I mean I can make it so that o is going
to win that's sort of the point I should
make it that X is going to win but not
on their first move
let's try this that's exes turn so ex o
ex ex o ex Oh can't win unless X goes
here then o can win alright this will do
I think this'll do I think this will do
as a scenario
can you see this yes it's very hard for
me to see what's visible being recorded
so I've drawn a configuration so I've
drawn a configuration here of the
tic-tac-toe board midway through the
game because I don't want to start at
the beginning where there's so many
possibilities that I couldn't diagram it
so right now it's X's turn and X has
three possible moves so I could express
that in a tree diagram as move one move
two and move three so let's take a look
at what I mean by that let's say let's
say move one is over here X would win oh
that's the point Audio is very low is
that really true or just one person
probably one person
I'm gonna use a blue marker so you can
see the new moves so let's say one
possible move is X going here let me
diagram out the next two possible moves
another possible move is X going here
and another possible move is X going
here so we need to look at eight each of
these and determine is the game over or
is it continuing so only in this case is
the game over and in this case the game
is over and X has won so I'm gonna mark
this green for the state of X having won
the game
now it's o's turn and for each of these
spots o could make as two possible
options
should do the next two all right
so Oh could go here ro could go here
Oh could go here RR oh could go here and
look at these in this case and this case
Oh has one remember we're trying to
solve for the optimal move for X X is
making the first turn so this means I
can mark these were Owens as red I was
like those are bad outcomes oh wow look
at this X there's no tie this this is an
area where there's no tie I kind of
meant to come up with a scenario where
there would at least be a tie but c'est
la vie the X is in the wrong place on
the second board says leo no no to do
this again aren't I why does anybody
watch me on YouTube I'm like the worst
at this stuff the X is in the wrong
place on where the X is in the wrong
place on the second board it should be
down one which is the second board
this is right this matches that this
matches that this matches that this
matches that X X X X s what I don't see
it
can't see the red marker very well
bottom right what I'm going crazy are
people trolling me here
I know Leo isn't the bottom row second
to the left
oh this is the left second to the left
I don't think so are you thinking that's
supposed to go like this this is a
terminal state it's correct okay it's
correct so Leo I think I'm assuming
maybe you made a mistake by thinking
this is somehow connected to this where
it's not this is a terminal State and
the red marker apparently is not visible
let's try this
yeah all right
so while these now are not terminal
states there's only one possible move
what's sorry too many markers and some
of them don't work look to 20 already so
while these are not terminal states so
while these are not terminal states
there's only one move possible for X so
I could draw an arrow and put that down
here but ultimately I could just
consider this as X has to go here so
these you could consider as terminal and
in which case x will win I think I might
want to do this again with a different
starting configuration
now you see that I visualized all the
possible outcomes of the tic-tac-toe
game based on this starting
configuration so how does X pick which
path to go down where we can see here
that the goal is for X to win the game
to get to here here or there how does it
do that and the way that it does by the
way spoiler alert this is the move it
should pick it should just win instantly
but the idea of the minimax algorithm is
if we can score every possible outcome
those scores can ripple back up the tree
and help X decide which way to go to
find the highest possible score the nice
thing about tic-tac-toe is there's not a
range of scores the endpoints are either
you won you lost are you tied so we
could say something like if X wins I
could have a score of +1 and typically
in most examples you'll actually see
people let me just I'll come back to
this later so I could say I can say if X
wins it's a score of +1 if oh wind it's
a score of negative 1 and if it's a tie
it's a score of 0 so in other words we
can give this a score of plus 1 this is
score of plus 1 this is score of
negative 1
this is score of negative 1 and this a
score of plus 1 but we've got an issue
I'm trying to pick between this option
this option in this option these two
don't have a score how do I pick their
score well I can pick their score based
on which one of these would be picked
but who's picking these this is why it
is called mini max oh that you can't see
that at all
this is why the algorithm is called mini
max because we have two players each
trying to make an optimal score X is
what's known as the maximizing player so
to go from here to here it's X's turn
and X is maximizing but here it's now
O's turns its O's turn and o wants to
win an X once is it going to X Kenneth o
wants to win X can assume that o is
going to play their optimal smooth now
you could have a situation where your
player doesn't play optimally and that's
something you might have to consider in
terms of game theory but in this case of
the algorithm we can assume that o is
going to make the best possible move I
mean if it makes a worse move that's
only better for us anyway so it's all
gonna work out so o is what's referred
to as the minimizing player the
minimizing player so o has the option of
picking negative 1 or plus 1 negative 1
or +1 which one is o going to picks pick
as the minimizing player the option to
win so we can then take the best outcome
for o the minimizing outcome for o and
pass that back up here's the score and
the same thing here now X is the
maximizing player which one of these is
gonna pick negative 1 plus 1 or negative
1 these are negative 1 because even
though it could win here if X goes this
way oh it's gonna win if X goes this way
oh it's gonna win if a plays optimally
so this is the way to go and this
scenario is you know albeit very this
scenario that I picked might not be the
best demonstration of the idea in fact
there's no ties here there's only two
turns being played so you may be what
you might want to do is pause this video
and do the same exact diagram but have X
and O with two starting positions and
see if you can diagram that whole thing
out yourself
if you do it again you could try this
configuration XO o o XX I have to say
I'm quite proud of his diet yeah even
though it didn't really work out the way
that I wanted to and I should have
planned this in advance and probably
whatever I did like I posted on Twitter
what did I do when I did this earlier I
go to my Twitter page yeah and look at
this image oh oh x2 middle so this is
what I did before so I guess this worked
out a little bit better because there's
a lot more
there's no terminal point at the first
move is this what kind of like what
violet suggests x/x oh oh oh interesting
people telling me it was a good
explanation okay maybe I'll just show
this really quickly
yeah you know what I'm gonna do I'm not
going to explain it again but I'll just
really quickly redraw it and we can do a
like a fast 40 thing all right let me
try that so let me go back to here
I say it's fine all right I'm just gonna
leave it let's move on because I got a
code this I don't have infinite time so
I'm gonna move on I'm gonna move on I
appreciate everybody's thoughts okay
so how could we possibly do this so how
do we implement this in code so that
your first clue should be the fact that
this is a tree diagram and each time you
see a tree diagram typically speaking a
recursive algorithm a function that
calls itself is something that will
apply in this case is no different I
want to call minimax on this board
position try and then loop through all
the possible moves and call minimax on
each one of these board positions loop
through all the possible moves if I ever
get to an end point return the score and
have that score backtrack up or ripple
back up the chain so let's see if we can
write the code to do this I think I can
I'm thinking about think about how to do
this okay
two-twenty I think I can get this done
in an hour okay so this here is the
place where I want to write the new code
currently a move is chosen by picking a
random available spot and I no longer
want to do that so I'm gonna I'm
actually gonna change the name of this
function to best to best move
and I don't need to worry about this
what's available I just want to actually
look at all the possible spots so
basically I just want to know is the
spot available
if the spot is available now I want to
call minimax on this particular board
wait yeah yeah if the spot is available
I want to try going there so I'm gonna
say board I J is the AI player and then
I want to call minimax on the board why
do I want to call minimax because I want
to get what is the score so I want
minimax to return to me and want minimax
with to return to me the score for this
particular move now it's gonna have to
recursively check these two spots but
we'll get that we're getting there
ultimately though I need to figure out
which one was the best so I need to keep
track by saying like the best score is
negative infinity I can't spell was
never the keyboard all along and if
score is greater than the best score
then that's the new best score and the
best move is this particular IJ right
it's an object with it I and J yeah and
let me declare best move and then move
so again I haven't implemented minimax
yet I'm just getting the basic idea down
for the very furthest turn I need to
start by looking at all the possible
moves get the score for those by calling
this mysterious minimax function I'm
about to write and find the best one
then apply that move there's a bit of an
issue here though because I am actually
placing the AI character sorry yeah it's
being pointed out in the chat there's a
big issue here though because I'm
replacing there's a big issue here
though because I am changing the global
variable board
I am altering the game I am moving X to
that spot and then moving X to the next
spot so I could make a copy of the board
but I think an easy way to deal with
this would actually just be to undo it
so right afterwards I'm gonna just
quickly undo that move so let's write
them in let's write the mini mat okay
the next step would be to write the
minimax function that drooly write the
algorithm itself I kind of want to know
to see if there's any errors here so
actually what I'm gonna do is I'm just
gonna put a placeholder function in
minimax which receives a board and then
I'm just gonna return the score of 1 so
minimax is always going to return the
score of 1 meaning it's not gonna be
able to pick it's just gonna they're all
gonna be a tie so it's good always gonna
pick the first one but let's let's see
how that goes next turn is not defined
because I called it best move sketch 23
and it's in one more place here in setup
okay well it went there watch it's gonna
go to the next available spot to the
next available spot to the next
available spot so you can see this is
working it's working the sense that the
algorithm is making a choice but no
matter what it's always just going to go
in order of the board and Oh X 1 Oh
function is named best move as well as
the variable thank you for that
there's also a big issue here which
thankfully didn't no major problems
happen but I named this function best
move and then it created a variable
inside of the function with the same
name which you know you can always run
into trouble so let me actually just
change this to move sort of by
definition it is the best move make sure
this still works
all right X is just picking straight
down okay yes
all right
now the hard part I need to write the
minimax function itself so one thing
that I've skimmed skipped here which is
typical of a minimax algorithm is I
would also keep track of the depth
because ultimately I could want to
consider like how long is it taking me
to get somewhere what is the depth of
the tree that I'm on so that's something
that you'll you'll see often within the
algorithm implementation so let me add
that so I add an argument depth here and
then give it a zero at first oh you know
what I need another really important
argument this is a turn-based game and
the algorithm is going to behave
differently whether it is the maximizing
player's turn X or the minimizing
player's turn Oh in this case again you
could have more than two players or
there could be a lot of weird stuff
going on but that's ultimately what I
need here so let me also add is
maximizing is maximizing and the this
here would be true right so I want to
perform minimax on this board with the
starting depth of zero and the first
move is the maximizing player
all right
so let's say I'm maximizing what do I
want to do actually what I want to do
even before this is check to see if
somebody won so I'm pretty sure I
already have a check winner function
that's something that I wrote in the
previous coding challenge let me double
check that check winner the winner is
null and then by the end it returns a
tie it returns null a tie or the winner
so the score is and let's let's make a
little lookup table that's basically
exactly what I wrote here if X wins it's
a 0 if a wins is a negative 1 if it's a
tie it's a 0
all right so this is my lookup table for
what the score then and again it's a
very simple scenario there's only three
possible scores and the score of the
result is okay well if if the result is
not equal to null
the score is the associate a that the
number associated with this particular
oh it got rid of those I didn't need
those quotes Visual Studio code just
clean that up for me thank you very much
the score is based on whoever won and
then I can return that so this would be
the terminal condition if if I am
calling minimax on this particular board
configuration at this particular depth
and it's an end state if it's a terminal
state just return the score that's what
I'm supposed to do
otherwise I need to go through and pick
the maximum need to go through all the
possibilities again for the next player
yeah
if it's not a terminal state if it's not
this state and I can't just return the
score I need to check all the possible
next moves and by the way I got this
wrong
so I got this wrong actually the next
move right I'm actually this is the AI
player trying to trying a spot there
move so the next move is actually not
the maximizing player this should be
false but I'll better write this as if
it is so if it's the maximizing player
I can copy-paste that from above check
all the spa possible spots again
if it's available try yeah now that's
the human right then oh no this is the
AI I'm confused because currently in my
brain the way I'm stepping through this
it's not the a eyes player turn ah I'm
confused because currently the way I'm
stepping through this it's in my mind
it's o's turn but i'm writing the code
for both whether it's x's turn or o
stern so for the sake of argument its
X's turn right now so I'm checking all
of those spots and then I'm going to say
return oh no no no I need to check I
need to find I define the best move I'm
kind of doing what I did again is there
a way for me to reduce the amount of
code I'm writing I'm not going to worry
about this I want to once again find the
best score which will be in this case
negative infinity and I want to say the
score is the minimax algorithm of the
board at depth plus one and now the next
players turn is false because I'm the
maximizing player and then I've got to
undo this just like I did before why do
I have to write this code twice I'll
think about later if I can refactor this
only do it once and then and then if
score is greater than best score best
score is equal to score and then after
all of this right for loop for loop if
it's an empty spot boom boom return the
best score so this is finding the best
score for all the possible next turns by
the AI player or if it's not the AI
player if it's the minimizing player we
can do exactly the same thing and again
maybe there's a way and I
I'm missing a curly bracket yes thank
you there's a curly bracket so this is
very important there's a lot of ifs and
four-state for loops here so maybe
there's also a nice way to refactor this
to make it more readable but if it's the
maximizing player check all the spots
find the best possible outcome and
return it but call minimax recursively
and the next future move and then here
I'm going to start right the minimizing
player wants to find the best score for
it which is the lowest score so and
that's the human player and if the score
is less than the best score and then
return that best score whew oh forget
about this return one let me see oh this
is very hard to look at
I'm gonna stare at this to make sure on
my my curly brackets line up if if for
four and then return the best score
otherwise if if for four and return the
best score I think this might actually
be good
should we just try it
err on line 33 which Oh hold on being
told there's an error on line 33 Richard
oh this should be ah look at this so
this is a huge mistake here return true
why did I have that there I don't know
why I did that that's return score this
should be returned score the whole point
of this and I don't even need a separate
variable here I can just say return
scores result right so this is any kind
of recursive function needs a terminal
condition needs to exit right this
function is always going to keep calling
itself and calling itself minimax
minimax minimax minimax
I don't know what this extra code isn't
here and minimax okay oh yes aha
all right so there's also I'm Simon is
pointing out something to me which is
great which I don't need this if
statement to find the best score the
whole point of this mini and also
there's a great chance for you use the
min function in the max function because
it's the minimax algorithm so I can
actually say score is the lower one the
minimum between score and best score
it's going to make it way easier to read
thank you and oh no but this one is Max
is Max and then this one oh thank you
for this I don't know why I didn't think
of this I'm sure it's in every
implementation score is the minimum
between score and best score so this
makes it much more much easier to read
here okay let's try it let's try it why
not
caution to the wind
okay excellent and the top left that's
definitely the optimal place to go I'm
gonna go here no no bad X bad X you're
not going in the right place yeah I beat
you
you're still going into order why are
you still going in order oh whoa you're
not going in order you're making weird
decisions Oh what's going on here
oh we're gonna have to debug this I
thought like maybe for once in my life I
actually wrote a complicated algorithm
correctly
oh I see the error I see the error of my
ways this has to be true right after the
maximizing players turn its the
minimizing players turn after the
minimizing players turn
it's the maximizing players turn okay
that's a good move X I see what you did
there
why can't you figure out to go there X
best score is max okay oh okay
this I'm finding this this is the score
is the new score and the best score
should be the bigger one the maximum
between to score and best score not
score ah this has to be this because I'm
returning best score and this has to be
this okay
okay let's see I really should not
continue to play this come on X figure
out where to go okay I'm gonna go here
[Music]
it should really know to go there right
maximising is the AI is maximizing
minimizing is the human oh no no that's
in the wrong place it's in the wrong
part I new look like brackets bad
brackets I need to return the best score
hello after I've checked all of the
possibilities meaning both four loops
the I and the J have completed I got
that right up here but not down here oh
okay I really think we've got it this
time
Tiffany's housed tie alright alright you
we're gonna go into competition now I'm
gonna I'm gonna let you win by going
here
ah so if I don't go in if I go in a
corner yet if I don't go to the middle
it's always going to win so X is always
going to win unless I go to the middle
in which it's always gonna be a tie
unless I make a mistake and then it's
gonna win but if I go to any other spot
X is going to be able to win because
it's always gonna
[Music]
take a little time to 3 o'clock amazing
trying to think of why I want to wrap
this up all right oh I'm so happy about
this where did I go
okay all right
now for a five-by-five yes so let me
make a list and make a list of things
you could try as exercises
the initial loop you wrote to check all
possible moves is it necessary you could
just start off with minimax for the
maximizing player in the current board
okay let's just think Oh in other words
I can say but how does that give me the
problems minimax returns a score so it
doesn't return a move so I'm not sure
how I would do that
I'll let somebody smarter somebody whose
brain isn't as tired they could try to
not compare it smarts can I explain the
algorithm Bruno asked can you explain
the algorithm well if you watched
earlier in this livestream mics I
explained it so well it's perfect nobody
could ever possibly misunderstood me
yeah yeah I feel like this initial loop
shouldn't matter but I mean sorry this
one but I need a way to actually pick
the move and here I don't pick the move
I guess I could pick it in the minimax
oh I could pick it and not undo it or
something I'm not gonna worry about that
I'm gonna let somebody I'm gonna I'm
gonna create a list of things so for
example try
you can apply this to connect four right
all right I think I'm done I think I'm
done this coding challenge is done
yeah okay thanks Coby all right
I'm feel like I want to make a list of
suggestions even if it's just like a
script that I'm gonna follow so let's so
let's think about what things people
might do to AI players larger board
other game I connect for animation and
timing and
and complex game Oh fruit at pruning
alpha beta pruning game requite that
requires a heuristic okay
so these are my suggestion exercise
suggestions anybody have other ideas or
these good good suggestions three play
or tic-tac-toe larger board more players
Oh depth this is I could add okay faster
his use depth in score
we can actually beat it go bottom right
then bottom left and top right okay
bottom left bottom right bottom left
bottom right
no you it went there it knows I can't
beat it
it knows so sirs this I will ask what do
you mean by complex game across your
wrist Rick I will I will a 3d oh I could
go first the other thing is I could go
first
so one thing that might be interesting
for me to try here really quickly before
I sort of finish off this challenge
would just be to comment out the AI
going first so I know this is
technically incorrect because X is
supposed to go first but let's see what
happens if I start with a fresh boat
forward and I go first so I'm gonna go
here I'm gonna go here oh it's high am
Ioana something became undefined oh if I
go last I guess there's no move for X
and I've got it here so that's things
like can I beat no I lost is there a way
for me to beat no it's always going to
go to the center so there's no way for
me to beat it I don't believe there's
any way for me to beat the AI player
it's a solved game because it's always
gonna go to the center so I could try
going here I could go here go here and
I'm the best I can do is a tie
okay yes I mean that one is counted in
the larger board okay tie or lose yeah I
could try it starting with the center
it's not gonna do me any good the best
move is the corner I believe but
whatever I mean it's gonna play
optimally no matter what
let's finish this
are you so you've watched this coding
challenge maybe you have a creative idea
about how to make your own version of
this but I just quickly whipped up
thanks to the chat in the live stream
also some suggestions of things you
could try so first of all what happens
if you just make two AI players that
just plate against each other over and
over again well in that case it's gonna
be a tie every single time it's a solve
game but that might lead to some
interesting possibilities um one thing
that I'm not using is using the depths
in the score so for example if I go back
to the code and I change these to say 10
and negative 10 I could account for the
depth meaning I could have a higher
score if I win more quickly than if it
takes longer to win right now I'm not
accounting for that so where in the code
would you subtract out depth that's
something you could try certainly would
be interesting to make a larger board so
can you play trick tic-tac-toe and a 5
by 5 or a 7 by 7 you the winning
conditions would change
the optimal play would change that would
be exciting to try I hope somebody does
that you could try more players like how
would you have a tic-tac-toe game with 3
players that's something you try with a
larger board you could try another game
maybe connect 4 would work that might be
able apply minimax to just thinking
about the interface animation like right
now how whenever whenever I go the next
turn happens immediately you could have
you could think about timing and that
sort of thing but then I always say high
degree of difficulty and maybe worth me
coming back to in a future video at some
point would be these two last items one
is known as alpha beta pruning oh three
3d tic-tac-toe thank you
it's the case in a minimax algorithm or
sometimes you discover an optimal path
and you don't need to check the rest of
possibly all of the possibilities and
that could alpha beta pruning refers to
the aspect of the minimax algorithm
where you find an optimal path and you
know that all the other possibilities
are going to be worse and you don't need
to go forth and check every possibility
so you could research how that works and
add it to this algorithm it's explained
in the Sebastian lag video then there's
this idea of games that you can't
possibly compute all the possible moves
so you need some way whoops oh shoot I
messed up the camera a little bit but
hopefully it's gonna be fine
then there's the possibility of where
was I walking to oh yeah where I was
then there's the possibility I just
noticed that that was out and that's I
don't know right yeah okay I think I was
here then there's the possibility of a
game I'm gonna let me take this out here
I'll just back it then there's the
possible
then there's a possibility of a game
where the number of moves is so vast you
couldn't possibly compute the entire
tree chess is the classic example of
that so you need some heuristic or a way
of an estimated guess of what the score
could be with any given state so one way
of doing this which I'm not saying is a
good strategy but a simple thing you
could do with chess is you could add up
the total number of white pieces versus
black pieces but you could say like well
the score is higher if you have if the
maximizing player has more pieces than
the opponent so that's one way of
approaching it so you don't actually
stop at the end of the tree but you just
go some pre-determined depth and then
calculate an estimate and have that
ripple back up the tree so you maybe
give that a try with a more complex game
um certainly other types of AI
algorithms that neural networks could
play a role at some point with how you
make that estimate but ultimately that
that is something that would be
interesting to try as well one more idea
that just came from the chat so this is
quite similar to the idea of a larger
board but instead of just a larger
2-dimensional board you could create a
3-dimensional board so tic-tac-toe that
happens in a cube I should just do that
as a coding challenge separately myself
so hopefully you learn something and
maybe this algorithm that looked a
little terrifying to you at one point
now seems quite accessible and doable
um maybe your creative idea is not on
this list please make your own version
of this share it with me at the coding
trained
komm there will be a link in this
video's description with instructions
for how to share it if you don't know
how to share it just write a comment
file an issue we the coding train
community are here to help you oh and I
can't wait to see you in a future video
good bye
alright I think I am done oh by the way
the alerts were the alerts going off
I guess not cuz nobody joined I I didn't
actually turn them off
alright I'm kind of excited that that
worked I can't believe it actually
oh the xkcd cheat sheet oh I forgot
about that I did not show that
Matthieu let's edit this in after the
fact and maybe we can capture and scroll
this separately because this is really
good this is another idea that could be
great one more idea which just came in
from the chat there's a wonderful xkcd
cheat sheet that shows a map of all the
possible optimal tic-tac-toe moves for X
and for o and this is really a beautiful
visualization that's much better than
the diagram that I made here and maybe
actually what a project you could do is
think about how to visualize the minimax
algorithm as it is happening and there's
there's maybe some just wonderful
artistic or data science possibilities
that could come out of that alright
thank you for watching this Cody Tojo
the coding tree I'm so glad that you
were here with me I can't wait to see
what people make with this this was a
this was like a tough one for me I was a
little scared of it and I think it
worked ok and I'll see you in a future
video goodbye
super chat final test David Schneider it
popped up thank you so much you're all
too kind do predator and prey game yes
so what I'm gonna do next is who knows
what but I will remind you that the next
videos to come out on the channel are
things that weren't from a live stream
and they will relate to the neural
network well Simon was telling me to
show some community contributions for
what the interactive drawing a teachable
machine let's see if there's any
teachable machine ones so what's a
little confusing is actually you know in
some cases and this is a good question
this is a question for those of you who
work on the coding train website does it
make sense for there to become community
contributions for each one of these
videos or just with just with one of
them I'm just on this page in general I
don't know but let's let's take a look
at these let's see where they came in I
think I have a memory of looking these
ok so not on this one Oh a whole bunch
ha ha ha theremin rock-paper-scissors
Wow look at these
like oh it didn't I have a memory of
looking at these now you sure I didn't I
didn't look at all of them cuz I don't I
don't remember looking at uh Simon's
make 24 let's sort of see I remember
looking at this one yeah I looked at
this one for sure there's this one ooh
look at this oh this is actually
training with the canine classifier
cool I like this interface that is
awesome
great job I really like this interface
that it's just sort of like in real time
so with KNN you don't actually need to
you don't need to train the model KNN is
just doing a search for the closest one
or the KN the K nearest neighbors so
that that can be very effective once you
have a lot of sample images it can
become too big to deal with but that's
great okay let's look at which one of
these these is there one of these I
should look at let's try are they the
same let's look at the second one use
Kies one two three and four to select a
quadrant then choose an operation using
plus/minus asterisk and slash boy oh boy
seems hard new level one game wait how
is this teachable machines quadrant
could be one of the four sections of the
window use keys one I'm confused
where is the teachable machines aspects
I'm ed although this is cool now can I
level one I think is where I am 24 so
wait oh I've just selected like this oh
look at that one plus plus 1 for 2 I
don't do I didn't do this right when we
trying to get to 24 Oh too late for me
well that wasn't right the snake game
contribution room earlier today is
really cool ok what's your other one
make 20 for a teachable machine uh-huh
I see I see hold your hand in front of a
quadrant to select it that's cool that's
really cool so the interaction is with
teach a little machine so I'd have to
train a model and then copy paper oh and
you get to put your model there that is
such a clever idea ah I never thought of
that so what Simon is showing here which
is very very clever is that you could
make a game where the interaction is
based on a teachable machine train model
but you could actually tell the user go
train your model just
these labels and then paste the URL
remodel and now you can play this game
that's wonderful so I I'm I'm not going
to step through training it right now
but oh and the camera of course went off
because it is the coding train after all
where the cameras go off now they go off
if they overheat hopefully it's gonna
come back on I have to refocus the
camera I have to refocus the camera with
my trusty Elgato focusing device
I turned the fan on which will keep it
cool I'm not gonna worry about the focus
too much right now okay
wonderful idea let's see the snake game
evolutionary AI and then theremin and my
own the snake game let's take a look at
these whoa oh man this is this is
everything I always wanted to make I
haven't had the time for it so this is
one thing I've talked about with my
evolutionary neuro evolution tutorials
is that sometimes you can't have you
can't try a bunch of different players
in a game all at once on the same game
board you have to run separate parallel
simulations and that looks like that's
what's happening here in this one which
is really really cool to see oh thank
you for this wild this is by Alex N and
then the theremin here
[Music]
so this looks like maybe this is similar
to Alexander Chen posted an interesting
teachable machine music e thing I'm
gonna let this go and then let's look at
these audio ones make the sound of the
animal game let's try this
hi there loading CG machine samples
per hour where uh you think it's can we
see whether it's getting my through a
console log cat waiting
ah anything I make apparently is a cat I
wonder if it's not actually getting my
audio or something anyway let me know if
you could get this game to work I love
the idea of it it's really fun is that
you know so many of them are like fine
the image and show it I love this make
the sound okay thank you for this I
think I'm gonna stop for today I can
come back to the interactive drawing
ones another time I think oh I guess I
can look at those just to keep up with
everything there no up still no
variations so hey everybody this was a
good one make a Korean version of this
share it with me alright I think I'm
done for today it is time to go off for
Thanksgiving holiday here I maybe I'll
read you some random numbers I could
work a little bit on this no I'm gonna
oh I haven't just a little bit more of
this red side to do right this is I just
have a little bit more of this red side
to do so I did the white I did the
yellow and I did the green so I just
have two rows of the red side to do and
it looks pretty doable I feel like this
is too much pressure do this on my own
time this is my my quiet enjoyment
activity I will read you some random
numbers I hope everyone has a nice if
you are celebrating this holiday I hope
you have a nice holiday if you are not
celebrating this holiday hope you have a
nice rest of your week if it is bedtime
for you
this is perfect timing through ninety
nine thousand seven hundred fifty one
fifty eight thousand seven hundred
eighty five seventy two thousand nine
hundred eighty six two thousand four
hundred eighty eight twenty-three
thousand three hundred fifty-four 80
thousand two hundred seventy-five I just
thought of something really important I
have not made a new coating in the
Cabana video and it is time for me to do
that and so I must on a live stream pick
the next topic for the next coating in
the Cabana video now I had a list and I
had the spinny thingy I really David
Schneider was helping me with this but I
don't know how I'm gonna find it
Schiffman github randomizer I guess I
could remake it so let's go to that I
have way too many systems for keeping
track of my topics but right now I was
let's let's let's pick from a few
possibilities so there was this to-do
list here which was everything I'd hoped
to do this fall which you guys you can
see I got very little I'd like to pick
naturey things for coding in the Cabana
I like ASCII art so let's let's pick
some things so okay what was that other
rows like the star wrote like there's
the Mara Rose but when I looked at it
there was also with it like on the math
world page there was like another star
rose so why not oh I feel like that's
not the best one to arrive at because
I'll put it on the list star Rose
marching squares I mean why not that's
not really a Cabana video though that's
more like a ASCII art
you know let's well you'll help me
curate this in a second and just put
anything that's a possibility memory
game oh I love the idea of doing a
memory game break out I feel like that
the game stuff I should save for here
hexagonal grid whirly noise I definitely
want to do I want the like I'm looking
for like the nature II pattern stuff so
de l'année triangulation could fit into
that yeah marching squares fits because
could make nice cool patterns with that
like this so that that works
what other noise algorithms are there
anything else let's look at I mean
hexagonal grid is kind of interesting
that works memory game could be
memorizing trees and plants cuz star
roses very little documentation might be
hard to do yeah it's all soaked I could
take that out because it's basically
very similar that the the the more rows
let's look at because I might try to do
one this weekend if I have some time I
think that I may have done a thing where
I like assigned a bunch to myself that I
was kind of interested in how to encode
her I'm decent been done now to rhom
curve what's that
okay
like this why not this looks good well
consider to that
haven't I really not done this all right
sure I feel like that's a thing that
I've done but probably spelled that
wrong
well I've really got to go soon
quadtree I did birds flocking constant I
did so many of these so I cleaned this
up to spiral oh yeah this is something I
talked about with Matt Parker from
stand-up maths at one point and isn't
there a I think there's a three blue one
Brown video on this as well okay well
there's some very large images suddenly
in this lecture by the way when you put
I know you know this is a convention
again if you post a plane post an image
and the slack chat it I have it
everything zoom so I could see people
text and it just blows up and I can't
really see what it is or see anything
anyone's written just so you know
musical melody generator I do want to do
that but I think that's maybe not a
Cabana video right now plane Z curve
I've been there's somebody on Twitter
has been sharing a whole bunch of these
with me so what is Kenny I I feel like I
know what that means but I'm not sure
like this kind of thing like this kind
of curve Lyapunov fractal lie up hoon of
fractal
that looks hard but okay why not all
right well now I have too many things
alright maybe this list is good now if
someone can explain to me what was meant
by Z curve I'm kind of interested in
that where was I here I lost the page
that had my things that were assigned to
me wait here it is this is it okay
anything else recently and I haven't
looked what's this
whoa
mmm I feel like
whoa all right this I have to look into
more that does look really cool curl
noise curl noise I you had me at noise
okay this works for me
curl noise alright three D CA okay yeah
I'll do that three Central Avenue k 3d
cellular I mean that should probably be
more specific than that but so that's
the Z curve I can't die once again I
cannot see what's in the slack channel
I'm going to try to
z curve all right this is good enough
I'll put Z curve on here why not we'll
figure out what it is all right we are
done I'm now going to pick the next
coating in the Cabana video
all right everybody you're ready for
this this is a big moment these are all
the possible things Oh trapped Knight
maze solving all such good you know yeah
Rubik's Cube part for someday that's on
my list okay okay one two three four
five six seven eight nine ten
what's it gonna be it's gonna be the
seeker spin again do I get like one veto
I feel like the Z curve was like the big
tonight let's look at what this is Z
curve is whoa this word this fits with
the Cabana sort of theme three
dimensional curve that consists of a
unique representation of a DNA sequence
huh zigzag shape huh is this really what
was meant fascinating I know nothing
about this oh well what do we think
spin again or Zeke earth is the wheel
like I mean I can't violate the wheel
it's landed on Z curve it's what I have
to do there's no choice I can't spin it
again it's too late similar to a hilbert
curve
spit again I can't spin it I'm not gonna
I mean I could have established some
ground rules which is that I could
create a framework by which I am allowed
to sometimes spin again - spin again
Simon you're always so like big on the
rules I'm surprised you say spin again
yeah I think I don't think it means Zee
curve so here's what I think it is give
me a second to find something on Twitter
I'm just gonna I don't want to pull up
something by accident that I don't want
to see I thought maybe it's this hold on
I found the Twitter feed I'm looking for
so this I'm looking at Juan Carlos on
Twitter and I'm about to looking for
here we go this is what I thought I
think we're going to make this the Z
curve hold on a sec so I thought Z curve
meant something like this it's because
it's an equation with some Z's in it and
then a bunch of difficult this is very
coding train like and there's like a
bunch of them that Juan Carlos this is
pons Campuzano on Twitter has made quite
a few these here's another one yes see
what about some rain with complex
numbers see look Juan Carlos was even
suggesting this for a future coding
challenge so how about this the order
curve oh that's what vaio there's also
the Z order curve I like that Z order
curve so space-filling curve plotting
complex functions yeah so plotting a
complex function I mean I've done some
of that but I also love this idea of Z
order curve this looks fun
space-filling curves okay we've got some
stuff to work with alright this could be
interesting right so we'll we'll discuss
this here I mean I'm not saying I'm
going to follow but I'm gonna do a quick
straw poll there should be a way for me
to put a poll in the YouTube chat I
don't think there is which Z curve so Z
order curve complex plot complex
function which is like Z equals X is
that is V equals x plus y times I kind
of is that right and then Z curve like
genomics or something and then spin
again I'm gonna allow multiple choices
which is a little weird because I want
people to be able to don't just
everybody pick yeah well maybe I won't
allow mo I don't think I'm gonna pick
spin again I feel like I cannot go
against the wheel like I've made a
choice in my life and that choice is to
follow the wheel I mean I should just
take spin again off I'll leave it on
there just see see what happens I allow
multiple choices I'm gonna allow my
minute let's try this yes the order
curve sounds really good one choice ask
hard it's interesting F of Z we're both
Z and F of Z are complex the colors are
used to show phase all right no multiple
choice is I'm creating this pole there
we go alright somebody will kindly post
this into the chat for me
well somebody kindly post this there I
guess it'll take a minute and we'll move
on
[Music]
let's see if I can post it
[Music]
came a little bit afraid to look at the
results because I hear that this bit
again is waiting no I'm not spinning
again I just wanted to know if that's
how you felt see the thing is though if
you add up all of these all three of
these are beating spin again right so
technically speaking
spin again is not winning it's like
losing by quite a bit
spin again has to be more than 50% of
the vote for it to win making that rule
right now so I'm picking Z order curve
right now is winning as far as I'm
concerned because my assumption is that
anyone who picked these two is picking Z
or to curve as their second choice okay
just so you know you've got spin again
has to have a what's the word it's a
plurality but it actually has to have a
majority when it's going down come on Z
order curve well I know how to fit I
know one person who can oh whoops
yeah I can vote here I've got I've got a
vote just like everybody else all right
we're gonna give this another minute or
two
the order curve with the hilbert curve
ah okay is that a thing
yeah so they're related yes but oh well
there we go
oh look at this oh well we're doing this
look at that okay we're doing this it's
been decided we're talking about the
wheel has spoken
[Music]
yes that's what Spencer says that's why
you should have allowed multiple options
I know I know
that's why Wow
[Music]
oh this is exciting
[Music]
can all blame Danny guy two for six for
this alright doesn't seem like people
are voting anymore no no no no no no
only spit again has to get to 50% no one
else has to get to 50% for involves the
order curve is one it's too late the
order curve has one but the reason why
is I should have done two votes like
spin again or one of these three so I'm
adding all three of those up if they're
greater than spinning again then I'm
picking the one of those three that has
the most votes so um we're good
Z order curve / hilbert curve I'll look
into it who knows might even have time
to do this weekend this all might be
moot but that's what will be the next
coding in the Cabana video so I'm gonna
play my goodbye song that I haven't
equate a while I'm gonna play a little
tic-tac-toe against my friend the
tic-tac-toe player
[Music]
all right I'm gonna win if there's any
last questions wait a second
so hilbert curve three blue one brown
yeah I would say so um
violet saying posting that there is a
look at these space-filling curves
hilbert curve yeah so this is great if
there is a three blue one Brown video oh
and this is just a montage oh so
satisfying okay I mean hello
have I just totally ruined three blue
bug Browns beautiful video here of
space-filling curves you can't really
hear the music right here quietly hold
on let's fix this for a second sound
[Music]
yeah I mean we're doing this and then
there's also a by heart video
[Music]
okay this I definitely want to do flow
sing
[Music]
wow this is so ridiculously satisfying
I'm very excited about this topic now
[Music]
this is awesome
and then there was also a vie heart
video on this someone will share it with
me I don't want to find the wrong things
by accident maybe you'd like to draw
squiggles on your board in class somehow
the wondering why not subscribe only not
subscribe to by densely as possible in a
single line that doesn't cross itself
it's like the idea would be to sit down
at the beginning of your leaves these
are so good on the page and keep drawing
a single line yeah vintage okay okay it
has been decided this is gonna be a
great topic I'm excited to give it a try
and I will see you all next week
sometime I hope I don't know what I'll
be doing next week but hopefully I'll be
getting this encoding at the Cabana any
last questions before I go feel free to
ask
[Music]
so Lars asks if you can suggest things
so one of these days I'll have a better
system for this and it's something that
the community and is always thinking
about at present the way to suggest a
topic there are really two approaches
well there's more there's the rainbow
topics github repo where you can file an
issue with an idea I will definitely see
that and people in the community will
see that you could join as a member our
patron and then we discuss ideas in the
slack Channel which I'm moving to
probably a discord soon enough and then
those are the two main ways you could
also tweet me at Schiffman on Twitter
well I read all the my mentions
[Music]
where is the full list of coding
challenges in text formats asks 623 so
that's a good question I don't know that
there is a just a text form text text
version of the list but all the coding
challenges are on the website and you
can find them here you can see me
scrolling through them some of them are
multi-part but here they all are on
coding train comm decoding Korean home /
coding challenges so Styx is asking if I
have a current discord chat there is a
Cunniff Ischl coding train discord that
you can find enjoying I have a patron
community that's on slack I'm thinking
of kind of merging combining those but
having a member only this core I don't
know how to do it about to do it without
that but yeah that's what they're
currently I'm thinking that I'm going to
move to having a discord for the member
community and then there might be like
one open channel on that that's just
open and the rest would be private for
members only I'm not really that's kind
of my current thinking it's something
I'm working with Sion
[Music]
Lucas asked can you make a search bar
for the challenges that would be a nice
feature for the website potentially I
don't know that there's that many that
you need to search there are quite a few
of them I would say if you just look for
if you do coding Train challenge and
then type what you're looking for like
you know if I typed fractal for example
that's usually go you're gonna find it
just with a Google search or your search
engine of choice thank you Andrew for
your nice comment Python series that
would be nice
only so much time in the day the week
the month the year
all right thank you everybody I'm going
to switch over to the wallpaper where
your name will pop up keep subscribing
and leave this going for a bit why stop
all the recording and upload all the
footage have a wonderful holiday Jonas
or yonas asks when is your next
livestream let me answer that probably
Thursday or Friday December is that 5th
or 6th I'm not sure sometime next week I
don't have a regular schedule right now
I think in 2020 I'll be able to have a
regular schedule and probably won't be
on Fridays but stay tuned bye everybody
[Music]
I always forget this this star this star
this star this star song never forget
the Vista
[Music]
auto-tuned and the internet will fix
that
coordinates
[Music]
so I'm back in the dark just to say
goodbye thanks everyone and I am about
to click and stream yeah