Thursday 17 October 2024

8C-g-m5OSjA

8C-g-m5OSjA

so hello there and welcome to another
tutorial my name is henry bakshi and
this time we're going to be going over
tower fanoy part 3.
now
just in case you haven't watched the
other parts and you wanted to skip
straight to three here's a little
rundown or a recap of of the other parts
now it's been quite a while since i've
released part two or even part one
so here it is
in part one we covered what the tower of
hanoi is
because as i know it's not that much of
a famous game uh so some people don't
know about it so uh we talked about what
the tower of noise is we talked about
how you can play the tower of noise all
the rules etc uh and then we talked
about how many maximum number i mean
minimum number of moves it would take uh
to actually solve optimally one tower of
anoint game with n number of disks
uh then in the second part of our
tutorial series uh we went through the
algorithm that i created uh and it uses
the trees data structure uh now i'm not
exactly sure and i haven't actually seen
this online yet or in any book or
anything so if you can find it leave it
in the description or i mean in the
comments or you can email me attachment
gmail.com more info on that at the end
of this video
anyway then we talked about solving the
puzzle manually and we also talked about
the code itself
uh so i showed you the code uh i
showed you an implementation of it in
swift too
uh and so
uh now in this part we're gonna be
showing you the ui
for this uh code now i'm not gonna be
making an entire like sprite kit
full-fledged ui or even a scene kit
full-fledged 3d ui uh all that i'm going
to be doing is i'm going to be creating
a character user interface for you uh
meaning it'll print out uh turn this of
this
uh moving
this disc from peg this to peg that and
then it'll be like uh you know those old
dots or command line sort of printed
graphics so it's gonna be fun yeah okay
so let's get to the mac part now where
i'm going to be demonstrating uh how to
create the character user interface for
the tower finoi let's get to it
so welcome back to the mac part and now
i'm going to be showing you the code of
this app and also before we get into the
code i'm going to show you a little demo
of this app so let's just make my face
smaller and move it to the corner here
now i'm going to make this full screen
and move it out of the way a bit okay
perfect now let's just see how many
discs we want to give an example of so
first of all i'm going to give an
example for three disks so if i type in
the number three here
and i click enter
as you can see it gives us this
wonderful little character user
interface
and it says turn zero of seven so it
calculates some of the number of moves
that it will take
uh and then it sees which move we are
currently on and then we use a print
legs function to actually print out
these legs over here
uh next we can just click enter
and as you can see turn one of seven
moving disk one from peg one to peg
three
turn two of seven moving disk two from
peg one to peg two
now as you can see this will continue
uh it's on turn five now
now it's on turn six
and as you can see it has solved it now
if i click enter we'll just stop okay so
that was an example of three disks now
just before we get into the code i'll
show you an example of four disks then
we'll get to the code
so four disks turn 0 14
and as you can see as i click enter
this tower of hanoi puzzle is getting
progressively solved
and
turn 14 and turn 15. perfect okay now
what we're going to be doing is we're
going to go into the code so now let's
close this down
and let's do this
um
okay so now i'm also going to show you a
demo of five however first i'm going to
go through the code then i'm going to
show you two more demos
so
okay perfect let's get started now there
is some code that you should already
know from the older parts such as this
hanoi node class which holds the disks
the from using two leaf one leaf two
leaf three and the initializer so you
know that from part two
uh now there's one thing that you don't
i yes you do know this the inst array uh
it's an array of strings which holds our
instructions
then there's this input function which i
i did not make this i got this from
somebody's blog i forgot the name i will
leave the link in the description
okay so this is an input function what
this allows us to do is allows the user
to enter input in this case i'm just uh
using it to have the user enter or
actually just click the enter button so
i know when to move to the next term
okay then there's a function that you
haven't exactly seen before the hanoi
function
uh what this function will do or
actually the hanoi underscore function
is it taste takes um
disks as an integer
it takes a as an integer it takes b as
an integer i i mean a is a string b is a
string and c is a string
so disks is the number of disks that
we're going to be using
uh for this hanoi puzzle
then a is going to be
the name of the apex then the name of
the bpeg and then the name of the cpeg
of course
uh then uh you know already all of this
code from part two again
now these are the contents over here
where we declare the initial leaf or
node uh then we check if it has leaf one
and then we check if it has a leaf two
and then we check if it has elite three
etcetera etcetera
uh
uh and then so
we go on from there okay
perfect
so next after the hanoi underscore
function we have the normal hanoi
function now this has a function much
more similar to what you what a user
would want
this just taste takes disks that's it
nothing else and it returns an array of
strings
and so the array of strings that it
returns will be the instructions
required to actually solve the hanoi
puzzle for the required amount of disks
and this function just calls the hanoi
underscore function
with the disks that you gave it
the apec named as a the be pick named as
b and the c peg named as c
then we just return inst or the
instructions
perfect then we have a new function
which you haven't even heard of before
it's called print legs what this does is
it takes
discs as an integer these are how many
discs we're going to have in the legs
uh then it takes var a as the entire peg
not just the name of the peg the actual
contents of the peg so if uh peg a if
we're just starting out then peg a will
have all the disks in the uh container
uh so
uh let's say we're starting out with
three disks uh it'll a will be one two
three b will be blank c will be blank uh
et cetera et cetera
okay then what we're doing is we're
checking if a dot count is less than
disks
meaning
not all of the disks are in a
if so
then go
4i and 1 to disks minus a.count what
this will do is it'll see how many disks
are not in a
depending on how many there are loop
through that many
and insert zeros in their place
so instead of having uh something like
this let's just imagine
uh this is this is a
uh one two or sorry
this is a one two
over here then this is b which is just a
three and this is c a four
now instead of having a as one two we're
going to have it as zero zero one two
then instead of having b as just three
we're going to have it as zero zero zero
three and instead of having c as just
four we're going to have a 0 0 0 4
because there are four
as you can see
disks that we could have put
on this peg
there are three of them and so we add
three zeros to make up for those place
okay then we do the same thing for b and
c
then we loop through
four i and one to disks
so what we're going to do here is we're
going to say
print and then
listen carefully this because this can
get a little bit complicated
we're going to print pipe
space
then a
the a peg array i minus one so we're
going to say which disk are we on
okay uh if it's uh let's say we're on
the first disk we're on one
we're going to say one minus one that's
zero so we're going to get the zeroth
element from a
when we get the zeroth element for a we
put it in between these pipes uh and
then we have four spaces and then
another thing except instead of a for b
then four spaces again and then for c
then what we're doing is we're handling
what happens if we have a zero or if we
have since we've inserted all the zeros
uh now before i even continue what i'm
going to do is i'm going to um
quickly take this little snippet of code
command exit and put it in the comment
here now if i run the app as you can see
i've removed that little line of code
now if i run the app on let's say three
disks it shows you all the zeros that
we've inserted into peg a peg b and peg
c
so instead of just having nothing in
these uh pegs that are blank it has
three zeros so now if i click enter
it's zero two three zero zero zero and
zero zero one
and as you can see
it keeps going on so the arrays of the
pegs are actually never empty they're
always full of something like completely
full to the brim of at least
normal disks or zeros but they will be
completely full because then what we're
doing is this command that i removed
actually replaces all zeroes with spaces
from inside of that string
and so what happens is if i run this now
as you can see
all the zeros that were in these pegs
have been replaced with spaces
and so we don't see the zeros so it's uh
programming friendly and it's user
friendly as well
perfect then all we're doing is we are
just printing out uh
five equals then i think one two three
four yeah four spaces then five equal
signs then four spaces and five equal
signs then a new line and then print
adds a new line by itself uh this is so
that we have this little ending of the
jar here it's like a little illusion
it's like uh the equals act as the
bottom of the jar
okay then we're just printing so it adds
yet to another line
okay you know actually instead of doing
that we could just do this perfect
okay
next we're getting into the really
really interesting stuff
so let's just close that so you can see
the code nicely
okay so now what we're doing uh is we
are creating a new constant called ndisk
now ndisk is the variable that will hold
the number of disks that we have
throughout the game
and so in order to calculate this not
really calculate you get the input by
doing the integer conversion of the
input function that i just introduced to
you if you want a little while ago and
then we are forced unwrapping this
because i don't want to implement error
handling it this this is just
a proof of concept for simple uh
programming beginners not beginners but
you get the point uh just the tower of
noise and so i'm expecting that the user
has inputted a number
so you can take care of the error
handling if you'd like to or you can
email me uh if you need assistance with
that anyway after that we are creating
the pegs dictionary which is a
dictionary of index type string and
value type array of integers and we're
declaring this as a blank dictionary
then what we're doing uh is we are doing
a loop four i and one to n disk this is
going to loop through from one to three
if we type in three numbers uh or three
disks one to four if we type in four
disks one to five if we type in five
disks
etc you get the point
okay
uh so next we are creating a actually
sorry i forgot to tell you uh inside of
this loop what we're doing is we're
checking if i is one if i is one we are
setting pegs a to a blank array pegs b
to a blank array and play uh sorry pegs
c to a blank array
and then what we're doing is regardless
of if i is one or if it's two or if it's
three or if it's four whatever
we are just saying pegs a
dot append i
that symbol
so what it's doing
uh is it's saying
fill up a
with
top with the amount of disks that the
user gave us
so if he gave us
three fill up peg a with one two three
if he gave us ten or actually ten
doesn't work with this app nine
uh then fill it up with one two three
four five six seven eight nine
that simple
okay
then we have a new constant uh outside
of this loop and what it does is this is
the turtle i mean total turns uh that's
our apple need to create that uh that
our apple needs to
take
uh and so we calculate this by con uh
taking the power
uh two to the power specifically two to
the power uh n disk uh then we minus one
from that and then we convert that to an
integer i believe
we can force unwrap that
okay never mind uh only converting
strings to integers gives you an
optional if you convert a def uh i mean
a double to an integer it does not give
you an optional so you don't need to
unwrap
okay uh then we just print out
turn zero of total turns
uh then we call the print likes function
that we just created on the uh three
pegs that we have uh and the end disk
then we just take input meaning uh we're
waiting for the user to click enter or
they can type whatever they like at
really but once they click enter uh it
will actually starts uh with the um
with the actual uh solving
okay then we're actually storing turn as
a variable uh because we have to change
this i can't have this as a constant
okay then what we have uh is we have a
function called peg english so what this
does is it takes a peg as a string and
it turns a string
now we expect this peg to be either a b
or c
so we're doing a switch case on this peg
string and we're checking if it's a
return peg one if it's b return peg two
and if it's c or turn tag three
the reason we're doing this is let me
just show you um
because as you can see it says moving
disk one from peg one to peg three it's
actually peg a to peg c but then we call
the peg english function on these pegs
uh to get peg one instead of a so the
user can understand it so it's a bit
more you know user friendly
okay
so if it's not a b or c we just return a
blank string because we don't know what
this is
actually you know what i don't think
swift 2 requires you to have a default
case
yep it does
anyway okay
perfect
or in this specific case it told me that
you have to have a default um
case okay
then we have a very big loop uh this is
4i in hanoi and disk so what this will
do is it will call hanoi function
which will turn an array of instructions
of how we can solve the tower of hanoi
and so we're calling that function with
endisk and so we're looping through the
instructions required to solve the tower
of noise for a specified number of disks
and so first of all what we're doing
this is the most important we're just
incrementing the turn so that the user
can see what turn they're on then we're
actually printing turn turn of total
turns
uh then we're seeing since uh these um
what do you call it
instructions are formatted in uh this
sort of way like let's say we have to
move peg 1 to peg
2
aka in computer language peg
peg a
to peg
c
i mean b
then actually what it's giving us the
the real instruction from the uh array
would be
a to me
or just a sec
no it's actually that was an older
method of mine it's actually just a b
so
it's a very simple language for the
computer i'm just to understand because
the
from is the first letter and the two is
the last letter there can be anything in
the middle and the computer will just
ignore it
because all swift is doing is it's
parsing out the first character and
parsing out the last character
okay perfect
okay
so now what we're doing is after we've
calculated from into
we are going to print out moving disk
now this is a bit hard
we're moving dicks disk
pegs
from
so we're seeing which peg are removing
from
take the
highest disk in that peg
the first disk in that peg
and replace it here and put it here so
moving disk
then whichever disk is on the top of the
peg that we're moving from
simple
and then we're saying moving this disk
from
this peg so peg english for the front
peg two
and then peg english again for the two
peg
simple again then we're just making a
new line
okay
it's a bit complicated uh but
once you just read it a few times it
you'll get the point
anyway after that what we're doing is
we're saying pegs two
so now we're actually moving the disk in
software terms
so we're saying pegs two
so we're taking the two peg and we're
force unwrapping it and we are inserting
whatever we remove
actually we're inserting
from
the pegs for the from uh
how do i explain this uh
if i do this it could be a bit simpler
uh from value
is equal to
this now quick uh thing i need to tell
you first
when you call the remove first function
uh from an array uh to an or you call
this function on an array it will remove
the first value from the array but it'll
also return
that first value to uh
to you so you can use it in let's say a
variable
and so what we're doing in this case is
we're removing the first
value from the from peg because of
course we're going to remove from from
put on to so removing it from the from
and we're getting that value we're
getting that disk value
and so what we're doing is we're taking
that disk value and inserting it to the
zeroth index the highest index the first
spot on the two peg
now it's quite simple okay
if you'd like more information on that
or this is essentially simulating a
stack so if you'd like some more
information on that you can visit my uh
operator or actually math parsing
and evaluating in swift tutorial i have
two of them there will be two links in
the description for them
okay
then what we're doing is we're calling
the print legs function for end disk and
then of course our three pegs and then
we're just waiting for input right as
the input is given meaning the enter
button is clicked uh the i this loop
will iterate once more and it will go
through all of this now let me show you
a demo for numbers five and numbers you
guessed it nine
okay
okay so now let me show you a demo for
five disks so if i type in five
turn zero thirty one so as you can see
as i click enter
five is progressively getting solved one
turn six seven eight
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29
30 31
and as you can see we have solved five
disks
now let me show you something really
interesting
nine discs
seems crazy right well it is
so as you can see there are 511
turns
for the number nine
that is a lot of turns
for us to possibly go through so now i'm
going to show you something that's
amazing
as you can see it shows us nine discs
here
now if i take the enter button and i
just keep it clicked like i don't
release my finger
as you can see you can watch the tower
of hanoi get progressively solved
i'm going to stop at
100 here
and as you can see on turn 100 we have
already gotten to this sort of a state
we moved disk 3 from peg 1 to peg 3 and
as you can see
we can actually see the entire history
of all the pegs we can see how each peg
was moved
now let's continue to 200
as you can see
is getting solved
quite
nicely and as i said this is going to be
no matter what like guaranteed
to give you
the lowest number of moves
it will never give you higher than
what's required so let me just show you
quickly though uh 511 so
yeah this is the minimum number of moves
you can't get better than this at this
point so as you can see we are in turn
200 uh and we are moving disc four from
peg one to peg two okay turn 200 next
turn 300
and as you can see we can see
um the tower
if you can't see where i'm seeing this
just look at this specific board oh i
missed 300
but as you can see uh now we're moving
this 4 from peg 3 to peg 1
we're just moving all of this
all of these discs and stuff until we
get to the
end where we have solved it
so actually uh one thing that i haven't
told you uh this app isn't looking to
complete the tower of hanoi
uh
what it's actually just doing
is it's just taking the instructions and
following them one by one
it's not actually uh looking have we
finished yet if we have then stop or
else look at the next instruction it's
just blindly following the instructions
uh that it gets from uh the hanoi node
class
uh and so
don't that have hanoi cloud node class
is guaranteed to give you the
instructions that will work uh and so
that's why uh it can blindly trust the
instructions from there now again i'm
going to click the end keep the enter
button clicked now i'm just going to go
straight to 511 actually i'll stop a
little bit before there
now if we just keep going
as you can see we are very very close to
having nine discs in the tower of noise
solved
and as you can see
last two moves
two goes on to uh peg three
and one goes on to pick three
now let me just show you uh my ui isn't
yet able to handle stuff like 10.
this is due to the fact that first of
all 10 has two digits which completely
messes up the pipe formatting uh so
that's one thing next 10 has a zero in
it and the zero automatically gets
removed as you know using our algorithm
uh so i mean i can fix that really
easily by putting a symbol that you
wouldn't really use in numbers like a
tilde or something like that
but again
then we'd have to redo the pipe
formatting and so it's just really hard
uh so till now it's only able to do uh
single digit numbers so from one to nine
though you wouldn't really want to do
one so from three to nine it'll handle
uh so
even though the instructions the
instruction function
uh can uh
get
for like even 100 discs if you were to
give it enough time uh but it can
so this app this ui can only solve
the number of disks up to nine that's it
that's the final thing
uh and the uh
but really note so
one last note just so you don't forget
this the algorithm can solve any number
of disks as long as your memory permits
so final note the algorithm can solve
anything the ui
that's lacking behind
but i'm planning on releasing maybe if i
get enough
views or ratings on this video or
request for it i will make this ui
better uh and i may just include uh the
double digit or actually any digit uh
version of the gui or cy
and that's going to be it for this
tutorial if you enjoyed please leave a
like down below you can also comment
down below contact me on
edgemanagemail.com
or tajimani on twitter if you have any
questions suggestions concerns
app ideas
anything really
you can just contact me on any of those
three places you can also send me a
video question and i might just feature
in one of my next videos
and you can also subscribe to my channel
if you're new or you like my content you
want to see more of it really does help
and that's going to be it for this
tutorial goodbye

No comments:

Post a Comment

PineConnector TradingView Automation MetaTrader 4 Setup Guide

what's up Traders I'm Kevin Hart and in today's video I'm going to be showing you how to install Pine connecto...