So now let's actually start implementing everything that we've done so far.
So how I usually like to start is the right kind of the main function.
So it's the do while loop that we did up here.
This will be our main function is do play the game while we want to play again.
OK so while
say want to play again I know I haven't declared the functions yet but we'll do that in just a moment
here.
Clay.
That's going to be our main function actually.
It's also going to have declare in when we make the core functions we should always think about what
it should have.
So play game really doesn't return anything and it doesn't need anything actually.
So should be fine like this.
Want to play again though will return either true or false right.
Whether that player wants to play again or not want to play it again.
And it won't have any arguments.
OK so we're not going to have any any arguments for want to play yet.
Does doesn't matter.
So that's it for the main loop.
Let's go to the play game function.
It's got a copy both of these
skin paste them and here
and that stub out the want to play it again by returning false.
For now we're going to implement sensate to do implement each
so for the play game.
We know how this works so we set the secret number the set the number guesses and then we do the do
while loop and prompt user to get that gas and then check the guess and so on.
We've already solved the problem.
Now we just need to translate this into code so let's do that.
So our secret number sorry is just an integer right.
Secret number and it said to set the secret number to some known value for now k.
So we're going to say it's 65 say k and then our number since a number of tries.
We said that the number of tries should be the log base 2 of the upper bound.
So let's define our upper bound.
It's a contest in which for this
and a log base 2.
So we don't have this function.
And we probably don't know how to write it either.
But thankfully if we include sci math sci math actually has this function so thankfully we don't have
to rate ourselves so you say log.
Just log to of a paper about Hey that's it.
In fact it even has the ceiling function.
See all this.
So there you go.
That's our number of guesses so we always know how many guesses we should have.
So the next part is to do the do while loops do they usually like to write the condition first.
So while not is game over or so we know we have a function called is game over at here.
So we will we should declare which.
And it's game over.
Returns true or false right.
That's the condition.
Is Game over.
Yes.
And we don't know if what it takes yet.
Well actually that's not true.
We actually do know it.
If we go down here we see.
So here's our kind of definition of it.
And has the number of guesses left the players guess and the secret number right.
These were all things that we said should be variables.
So in fact we do know what should be here it should be secret number
he number of tries.
Left right.
In the current player yes.
Yes.
This K
is copy that and put it below.
I want to play again.
All implement.
Want to play again soon.
So you don't have to guess at since I just find the guess
zero for now.
And that should be secret number
number tries and guess
and in fact we can implement this right now.
Just turn the secret number
is equal to that guess guess or number of tries.
Is equal to zero.
Or could say less than or equal to zero.
All
doesn't matter.
So that is game over.
So the next part.
And see I'm not really doing everything in order because I don't need to anymore because I know I know
how this all works because I've already solved problems.
I can kind of do it in whatever order I really choose.
So we said that we want a function for getting the gas so gas and if you get something you're going
to return the values are probably going to return an integer to get gas in here it's said we should
output the number of guesses left so it should take the number of tries.
Right.
This K is back up here.
He should return an integer and get number or get gas.
It takes the number of tries
this
is copy
this
is he's down here.
So let's actually implement this one right now.
The should be have the inputs that gas storage and then remember had the failure here.
And then we do like the do while loop.
See because they could mess up all of the press like F instead of 10 or something.
So for do
while while there's still a failure rate and this before
failure.
False it's always false of the loop.
And then we're are going to output these enter your guess and we have to say how many guesses you have
left.
A number of guesses laughs.
The number of tri
get I guess
so they should all look very familiar to you.
Right.
And this couple of times now it's clear
we need a constant shrink.
In for snoring all characters
to every launch
see it nor
no more sure it's an edge up until the enter key.
SEE out input air please
try again
this and we have to say that there was a failure here.
This is not OK.
And then at the end it's done some sort of turn the guess because we know at this point that the guess
is valid.
Right.
Because he exited the failure.
While that while it scary.
So that's good guess.
So we've gotten the so what's next.
So got the guess.
That's kind of three and four here.
And then check to see if the guess is he called to the secret number.
OK.
So you going to say if the guess is not equal to the number.
K because up here said there's really only one case so the case is if it's not equal to the secret number
so then we'll temperament the number of guesses left so you each hear number of tries left is minus
minus remember that the decrement operator minus minuses one off the number.
Or the current tally K and then check to see if the guess was higher or lower than the secret number.
So let's do that.
So if the guess is greater than the secret number
then we will just output
your guess was too high.
OK.
Else and we don't need another case right because we're very checked to see if they're equal and they're
not.
So we know that if it's not greater than Then it must be less then.
So
your guess is to
each
time it's just an exclamation point in your.
So while the game's not over so we're almost done with the play game function but we just need to display
the results.
And what should display results take.
Well if you looked back here we need to see what we need to see her number for sure.
OK.
But there's something here that we didn't really go over and it's this If the player got it.
So how do we know if the player got it or not.
Well if the number of tries is greater than zero then we know they must have gotten it right.
So we're going to need the number of tries as well.
So just say secret number
and the number of tries.
This is really the number of troops left so need to declare this function
and it doesn't return anything because it is going to output something.
So it's way
so inch
secret number
in the mirror tri's and so copy us.
Hello here.
Weren't too much about the organization of it evidence.
Pretty small program so.
So how did this work.
So if the if the player got it it would just print out that they got it.
And the secret number and if they didn't and would print out that they didn't answer number.
If so they got it.
If the number of tries is greater It's greater than zero.
Right.
So they have tried left
and this is the ouch.
You got it
was
easy.
Number
else
you didn't get it.
It was
this number
that K..
So really just following our kind of template that we laid out here.
OK.
So what do we have left here.
So we have to implement.
We want to play again.
And each
It's really I think.
Yeah.
So let's do that now.
So we already know how this works.
So it's fine actually we're going to
cut that and put this in global scope.
So we can use it for both functions of the get gas and watch play in.
So here how I want we want to play.
Going to work is I really want them to input a character.
So we're going to say that's the inputs and again we're going to have this failure.
And again to do so while failure thing for
an We're going to set the input or the failure.
Sorry to be false and it's going to prompt the user would you like to again
get the input Again this could fail right.
So if we see and fail
season on it and send a clear and and no more
no more chores.
You on
see
input each error.
These try again.
There isn't failure in this case.
And just return
the if there if we said
they said why can't they input a character why.
And in fact let's just give them a helping hand here.
This
so if they type why it will return that they do want to play again.
In fact if they type anything else they won't want to play them.
So
let's take a look at this one more time.
We're just really.
So let's run this in over.
So please enter your gas number of guesses left is seven right.
So one thing actually we didn't do is output the range.
So you do that right here.
See out
the range of the number is between 0 and onward.
K. stratas in K. So the range of the number is between zero and a hundred and you have seven guests
left so eight gas is too low.
And see updated that we have only six tri's left so let's in this test this whole thing.
And this time we're going to test to see if we can completely fail at it.
So 90 I 100 Do I say 32 to high 3 9 0 to low and then it said you didn't get it right.
It was 65 k.
So that case is working.
And say I do want to play and notice that it outputs the range again because we're in the do while loop
and call play game and play game again we'll do all this code again.
So while in this era and this time we actually try to get nine to
say 65.
You got it.
Sixty five and say no and exit it.
So there you go.
This program is working.
So gratulations if you followed along this is your very first game in C++ and in the next lecture I
went to sort of change just a little bit.
So if you remember we wanted to be able to change the secret number every time you play the game.
So we're going to see how we can solve that problem.