WEBVTT

00:00.760 --> 00:07.430
So now let's actually start implementing everything that we've done so far.

00:07.840 --> 00:14.370
So how I usually like to start is the right kind of the main function.

00:14.410 --> 00:20.800
So it's the do while loop that we did up here.

00:20.800 --> 00:26.060
This will be our main function is do play the game while we want to play again.

00:26.190 --> 00:28.510
OK so while

00:31.500 --> 00:41.100
say want to play again I know I haven't declared the functions yet but we'll do that in just a moment

00:41.100 --> 00:42.500
here.

00:42.640 --> 00:43.730
Clay.

00:46.000 --> 00:51.130
That's going to be our main function actually.

00:51.290 --> 01:02.670
It's also going to have declare in when we make the core functions we should always think about what

01:03.690 --> 01:05.280
it should have.

01:05.280 --> 01:11.340
So play game really doesn't return anything and it doesn't need anything actually.

01:11.430 --> 01:15.480
So should be fine like this.

01:15.480 --> 01:19.860
Want to play again though will return either true or false right.

01:19.890 --> 01:27.230
Whether that player wants to play again or not want to play it again.

01:28.110 --> 01:30.190
And it won't have any arguments.

01:30.260 --> 01:34.250
OK so we're not going to have any any arguments for want to play yet.

01:34.250 --> 01:35.670
Does doesn't matter.

01:37.110 --> 01:40.290
So that's it for the main loop.

01:40.290 --> 01:43.840
Let's go to the play game function.

01:44.380 --> 01:46.350
It's got a copy both of these

01:50.670 --> 01:52.830
skin paste them and here

01:58.450 --> 02:03.630
and that stub out the want to play it again by returning false.

02:03.630 --> 02:08.380
For now we're going to implement sensate to do implement each

02:12.780 --> 02:16.340
so for the play game.

02:16.340 --> 02:25.190
We know how this works so we set the secret number the set the number guesses and then we do the do

02:25.190 --> 02:32.200
while loop and prompt user to get that gas and then check the guess and so on.

02:32.450 --> 02:34.610
We've already solved the problem.

02:34.610 --> 02:40.330
Now we just need to translate this into code so let's do that.

02:41.000 --> 02:46.510
So our secret number sorry is just an integer right.

02:46.530 --> 02:55.060
Secret number and it said to set the secret number to some known value for now k.

02:55.220 --> 03:06.380
So we're going to say it's 65 say k and then our number since a number of tries.

03:08.290 --> 03:15.520
We said that the number of tries should be the log base 2 of the upper bound.

03:15.550 --> 03:18.260
So let's define our upper bound.

03:18.720 --> 03:24.010
It's a contest in which for this

03:26.990 --> 03:28.830
and a log base 2.

03:28.860 --> 03:32.560
So we don't have this function.

03:32.610 --> 03:35.600
And we probably don't know how to write it either.

03:35.880 --> 03:49.960
But thankfully if we include sci math sci math actually has this function so thankfully we don't have

03:49.960 --> 03:52.670
to rate ourselves so you say log.

03:52.990 --> 03:59.990
Just log to of a paper about Hey that's it.

04:00.060 --> 04:02.780
In fact it even has the ceiling function.

04:02.820 --> 04:09.520
See all this.

04:09.680 --> 04:11.170
So there you go.

04:12.360 --> 04:18.090
That's our number of guesses so we always know how many guesses we should have.

04:18.090 --> 04:26.760
So the next part is to do the do while loops do they usually like to write the condition first.

04:26.760 --> 04:38.690
So while not is game over or so we know we have a function called is game over at here.

04:38.870 --> 04:45.450
So we will we should declare which.

04:46.100 --> 04:47.830
And it's game over.

04:47.840 --> 04:50.680
Returns true or false right.

04:50.690 --> 04:51.820
That's the condition.

04:51.890 --> 04:54.370
Is Game over.

04:55.420 --> 04:56.820
Yes.

04:57.550 --> 05:00.760
And we don't know if what it takes yet.

05:01.120 --> 05:02.430
Well actually that's not true.

05:02.440 --> 05:04.000
We actually do know it.

05:04.030 --> 05:07.690
If we go down here we see.

05:07.850 --> 05:11.040
So here's our kind of definition of it.

05:11.920 --> 05:18.880
And has the number of guesses left the players guess and the secret number right.

05:18.880 --> 05:22.040
These were all things that we said should be variables.

05:22.060 --> 05:26.490
So in fact we do know what should be here it should be secret number

05:30.190 --> 05:34.600
he number of tries.

05:35.470 --> 05:36.560
Left right.

05:37.810 --> 05:41.150
In the current player yes.

05:41.290 --> 05:41.820
Yes.

05:41.920 --> 05:44.950
This K

05:47.850 --> 05:52.840
is copy that and put it below.

05:52.860 --> 05:54.180
I want to play again.

05:55.360 --> 05:56.110
All implement.

05:56.120 --> 05:58.000
Want to play again soon.

06:00.590 --> 06:04.430
So you don't have to guess at since I just find the guess

06:06.980 --> 06:08.080
zero for now.

06:12.530 --> 06:15.620
And that should be secret number

06:18.650 --> 06:22.580
number tries and guess

06:26.310 --> 06:29.080
and in fact we can implement this right now.

06:29.160 --> 06:32.850
Just turn the secret number

06:38.620 --> 06:47.760
is equal to that guess guess or number of tries.

06:49.060 --> 06:51.180
Is equal to zero.

06:52.290 --> 06:54.950
Or could say less than or equal to zero.

06:55.050 --> 06:55.370
All

06:58.670 --> 07:00.040
doesn't matter.

07:02.270 --> 07:06.490
So that is game over.

07:06.660 --> 07:08.490
So the next part.

07:08.640 --> 07:14.940
And see I'm not really doing everything in order because I don't need to anymore because I know I know

07:14.940 --> 07:17.880
how this all works because I've already solved problems.

07:17.880 --> 07:22.600
I can kind of do it in whatever order I really choose.

07:22.630 --> 07:39.720
So we said that we want a function for getting the gas so gas and if you get something you're going

07:39.720 --> 07:50.240
to return the values are probably going to return an integer to get gas in here it's said we should

07:50.240 --> 07:57.290
output the number of guesses left so it should take the number of tries.

07:57.320 --> 07:57.850
Right.

07:59.710 --> 08:05.850
This K is back up here.

08:07.460 --> 08:17.740
He should return an integer and get number or get gas.

08:17.810 --> 08:20.430
It takes the number of tries

08:22.960 --> 08:23.470
this

08:26.880 --> 08:28.200
is copy

08:30.470 --> 08:31.190
this

08:35.680 --> 08:40.160
is he's down here.

08:40.350 --> 08:44.380
So let's actually implement this one right now.

08:47.280 --> 09:02.610
The should be have the inputs that gas storage and then remember had the failure here.

09:03.810 --> 09:05.870
And then we do like the do while loop.

09:05.870 --> 09:12.670
See because they could mess up all of the press like F instead of 10 or something.

09:12.710 --> 09:16.110
So for do

09:18.750 --> 09:25.300
while while there's still a failure rate and this before

09:27.840 --> 09:29.970
failure.

09:30.140 --> 09:35.420
False it's always false of the loop.

09:35.830 --> 09:46.220
And then we're are going to output these enter your guess and we have to say how many guesses you have

09:46.220 --> 09:46.750
left.

09:46.760 --> 09:52.070
A number of guesses laughs.

09:55.850 --> 09:58.250
The number of tri

10:06.850 --> 10:08.730
get I guess

10:13.460 --> 10:16.410
so they should all look very familiar to you.

10:16.420 --> 10:17.250
Right.

10:17.320 --> 10:21.910
And this couple of times now it's clear

10:24.860 --> 10:29.460
we need a constant shrink.

10:29.490 --> 10:34.470
In for snoring all characters

10:37.070 --> 10:38.600
to every launch

10:43.140 --> 10:44.850
see it nor

10:47.450 --> 10:56.710
no more sure it's an edge up until the enter key.

11:00.400 --> 11:07.510
SEE out input air please

11:09.920 --> 11:11.540
try again

11:17.590 --> 11:21.350
this and we have to say that there was a failure here.

11:21.430 --> 11:27.890
This is not OK.

11:28.070 --> 11:35.270
And then at the end it's done some sort of turn the guess because we know at this point that the guess

11:35.270 --> 11:36.240
is valid.

11:36.410 --> 11:36.930
Right.

11:38.600 --> 11:41.320
Because he exited the failure.

11:41.540 --> 11:44.630
While that while it scary.

11:44.810 --> 11:46.250
So that's good guess.

11:46.250 --> 11:49.160
So we've gotten the so what's next.

11:49.900 --> 11:52.080
So got the guess.

11:52.090 --> 11:54.580
That's kind of three and four here.

11:54.580 --> 11:58.830
And then check to see if the guess is he called to the secret number.

11:58.970 --> 11:59.480
OK.

12:00.410 --> 12:07.040
So you going to say if the guess is not equal to the number.

12:12.420 --> 12:21.420
K because up here said there's really only one case so the case is if it's not equal to the secret number

12:22.530 --> 12:35.040
so then we'll temperament the number of guesses left so you each hear number of tries left is minus

12:35.040 --> 12:43.160
minus remember that the decrement operator minus minuses one off the number.

12:43.170 --> 12:54.880
Or the current tally K and then check to see if the guess was higher or lower than the secret number.

12:54.880 --> 12:58.560
So let's do that.

12:58.570 --> 13:03.940
So if the guess is greater than the secret number

13:08.390 --> 13:10.250
then we will just output

13:14.990 --> 13:18.790
your guess was too high.

13:20.820 --> 13:21.750
OK.

13:22.110 --> 13:27.390
Else and we don't need another case right because we're very checked to see if they're equal and they're

13:27.390 --> 13:28.490
not.

13:28.500 --> 13:34.780
So we know that if it's not greater than Then it must be less then.

13:35.160 --> 13:35.590
So

13:41.730 --> 13:45.190
your guess is to

13:48.710 --> 13:48.970
each

13:52.220 --> 14:04.940
time it's just an exclamation point in your.

14:05.020 --> 14:13.270
So while the game's not over so we're almost done with the play game function but we just need to display

14:13.270 --> 14:14.640
the results.

14:16.580 --> 14:19.860
And what should display results take.

14:19.870 --> 14:29.100
Well if you looked back here we need to see what we need to see her number for sure.

14:30.030 --> 14:30.690
OK.

14:31.120 --> 14:38.780
But there's something here that we didn't really go over and it's this If the player got it.

14:38.830 --> 14:41.430
So how do we know if the player got it or not.

14:41.890 --> 14:50.490
Well if the number of tries is greater than zero then we know they must have gotten it right.

14:50.800 --> 14:56.450
So we're going to need the number of tries as well.

14:56.570 --> 15:00.070
So just say secret number

15:04.890 --> 15:07.860
and the number of tries.

15:11.190 --> 15:18.340
This is really the number of troops left so need to declare this function

15:22.510 --> 15:26.290
and it doesn't return anything because it is going to output something.

15:26.390 --> 15:28.280
So it's way

15:30.990 --> 15:33.930
so inch

15:37.720 --> 15:38.650
secret number

15:41.090 --> 15:52.780
in the mirror tri's and so copy us.

15:53.290 --> 15:54.440
Hello here.

15:56.760 --> 15:59.790
Weren't too much about the organization of it evidence.

15:59.800 --> 16:02.530
Pretty small program so.

16:02.710 --> 16:05.600
So how did this work.

16:08.120 --> 16:12.590
So if the if the player got it it would just print out that they got it.

16:12.590 --> 16:18.750
And the secret number and if they didn't and would print out that they didn't answer number.

16:18.920 --> 16:22.160
If so they got it.

16:22.160 --> 16:30.450
If the number of tries is greater It's greater than zero.

16:30.480 --> 16:31.400
Right.

16:31.500 --> 16:32.880
So they have tried left

16:37.190 --> 16:41.960
and this is the ouch.

16:42.100 --> 16:42.740
You got it

16:45.480 --> 16:46.490
was

16:53.360 --> 16:53.910
easy.

16:54.060 --> 16:54.420
Number

16:58.840 --> 16:59.520
else

17:04.150 --> 17:06.350
you didn't get it.

17:11.620 --> 17:12.880
It was

17:15.720 --> 17:16.660
this number

17:19.750 --> 17:22.910
that K..

17:23.500 --> 17:29.150
So really just following our kind of template that we laid out here.

17:29.350 --> 17:30.730
OK.

17:31.480 --> 17:34.370
So what do we have left here.

17:35.240 --> 17:36.930
So we have to implement.

17:36.950 --> 17:39.280
We want to play again.

17:40.070 --> 17:41.380
And each

17:45.150 --> 17:46.600
It's really I think.

17:46.870 --> 17:47.990
Yeah.

17:48.130 --> 17:50.170
So let's do that now.

17:50.230 --> 17:53.060
So we already know how this works.

17:54.310 --> 17:58.090
So it's fine actually we're going to

18:03.130 --> 18:06.540
cut that and put this in global scope.

18:10.080 --> 18:17.570
So we can use it for both functions of the get gas and watch play in.

18:17.610 --> 18:21.860
So here how I want we want to play.

18:21.870 --> 18:28.690
Going to work is I really want them to input a character.

18:28.800 --> 18:36.950
So we're going to say that's the inputs and again we're going to have this failure.

18:37.560 --> 18:46.820
And again to do so while failure thing for

18:49.450 --> 18:52.700
an We're going to set the input or the failure.

18:52.700 --> 19:06.300
Sorry to be false and it's going to prompt the user would you like to again

19:10.360 --> 19:16.160
get the input Again this could fail right.

19:16.180 --> 19:22.140
So if we see and fail

19:26.900 --> 19:33.290
season on it and send a clear and and no more

19:36.470 --> 19:40.380
no more chores.

19:44.150 --> 19:44.920
You on

19:49.880 --> 19:50.480
see

19:54.380 --> 19:56.480
input each error.

19:59.330 --> 20:03.160
These try again.

20:06.140 --> 20:09.950
There isn't failure in this case.

20:17.640 --> 20:19.730
And just return

20:23.530 --> 20:27.570
the if there if we said

20:31.730 --> 20:38.500
they said why can't they input a character why.

20:38.540 --> 20:44.240
And in fact let's just give them a helping hand here.

20:45.300 --> 20:45.840
This

20:50.780 --> 20:54.890
so if they type why it will return that they do want to play again.

20:54.890 --> 21:00.030
In fact if they type anything else they won't want to play them.

21:01.610 --> 21:02.420
So

21:04.910 --> 21:08.960
let's take a look at this one more time.

21:09.320 --> 21:11.980
We're just really.

21:12.070 --> 21:22.650
So let's run this in over.

21:22.780 --> 21:28.080
So please enter your gas number of guesses left is seven right.

21:28.170 --> 21:37.340
So one thing actually we didn't do is output the range.

21:37.560 --> 21:41.140
So you do that right here.

21:41.170 --> 21:42.350
See out

21:45.250 --> 21:54.490
the range of the number is between 0 and onward.

21:54.760 --> 22:07.090
K. stratas in K. So the range of the number is between zero and a hundred and you have seven guests

22:07.100 --> 22:11.350
left so eight gas is too low.

22:11.370 --> 22:20.300
And see updated that we have only six tri's left so let's in this test this whole thing.

22:20.640 --> 22:25.980
And this time we're going to test to see if we can completely fail at it.

22:25.980 --> 22:43.360
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.

22:43.390 --> 22:45.130
It was 65 k.

22:45.160 --> 22:47.140
So that case is working.

22:47.380 --> 22:57.040
And say I do want to play and notice that it outputs the range again because we're in the do while loop

22:57.250 --> 23:06.010
and call play game and play game again we'll do all this code again.

23:06.010 --> 23:13.350
So while in this era and this time we actually try to get nine to

23:16.020 --> 23:18.040
say 65.

23:18.420 --> 23:18.950
You got it.

23:18.980 --> 23:25.670
Sixty five and say no and exit it.

23:25.690 --> 23:26.930
So there you go.

23:28.160 --> 23:30.150
This program is working.

23:30.240 --> 23:39.800
So gratulations if you followed along this is your very first game in C++ and in the next lecture I

23:39.800 --> 23:43.190
went to sort of change just a little bit.

23:43.190 --> 23:52.930
So if you remember we wanted to be able to change the secret number every time you play the game.

23:53.000 --> 23:56.120
So we're going to see how we can solve that problem.
