Hello and welcome.
I'd like to provide a demonstration solution to our previous
challenge which was implementing a guess and number type game.
So let's bring up code blocks, close my current
project and create a new one.
This new project I’m just going to call guess number game.
It's going to be a console application.
So I’m just going to call it guess the number.
And what I’m going to do in this to start this project I’m
going to declare my variables.
I always like to declare and it's my variables at the top.
But I also am going to use the random number generator.
So I want to make sure that I have a couple includes.
Stdlib is already there by default.
So all I need to do is add my time.h because again this is
for a random number generator.
This is basically for the random number generator seat.
The other variables that I’m going to need in this program is
I want to store my random numbers.
So I’m just going to say random number, I’m going to
initialize that to 0 until I get it from the actual library.
So declare initialize.
I’m going to create a variable to store the guess from the user, and
I’m going to initialize that with 0.
I’m going to create another variable which is going to be the number
of guesses, the current number of guesses or actually I should say
maximum number of guesses, maximum number of guesses, and I’m going
to initialize this to 5 because they can only guess 5 times.
I also have to create a time variable so that I can use it
for my random number generator.
So I’m just going to say time t t.
First thing I want to do before I ask the user for input is I want
to generate the random number.
I guess I could do this after asking the user for input but it's just
easy to do the first thing you do it.
So I’m going to initialize my random number generator, I’m going
to put a little comment in there.
Make sure you use good meaningful names and make sure you indent.
It's going to use that srand function that I talked about in the lecture.
You can just pass it in a seed essentially.
So I’m going to say unsigned, and I’m going to just pass in my t variable.
This is going to get time is a function that gets the current time.
I’m going to pass in the t so that it seeds it and then I’m going to end it.
Now I actually need to get my random number generator, so I’m
going to say get the random number.
And all this is going to do is assign a variable to what's
returned from the rand function.
So let's say random number which is my local variable equal to
and use assignment operator say rand which is calling a function.
You don't need to know any of this, but I’m going to seed it with 21.
This will ensure that my random number is only between 0 and 20.
Now I’m going to just output a couple messages to the screen so that
the user knows what they're doing.
So I’m going to do a print app and I’m going to say this is a guessing game.
And then I’m going to also tell them that I have chosen a
number between 0 and 20 again, just so they're informed.
And then 0 and 20 which you must guess and end it with the line feed.
Now that I told them what I want in the game, I’m going
to actually start looping.
And I’m going to loop until my number of guesses is equal to 5.
So what I would actually rather do is I wanted to clear my
number of guesses up here, but I don't want to initialize.
I want to initialize in my loop just to show you that I can do that.
If I did initialize it there, before the first semicolon in the
for loop, I could keep it empty.
So for example I could do something like this, initialize it up here.
And then in my for loop, I would just put a semicolon because I
don't need to do an initialization.
Because remember, in our for loop, the first thing you have before
the semicolon is the initialization of the loop control variable.
I’ve already initialized it so I don't need to do it,
but I don't want to do that.
I’m going to just declare it up here, and I’m going
to initialize it down here.
I’m going to say maximum number of guesses equals 5.
That's my loop control variable, I’m going to initialize it.
I’m going to jump out of the loop if my maximum number
of guesses is reached.
So here I would say actually I’m going to write it a different way.
I’m going to write it so that the loop goes from 5 to 0.
So my maximum number equals 5.
It would be better to rename this variable.
Let's rename it and say number of guesses.
And the number of guesses is going to start off as 5.
And I’m going to make this go backwards.
And when the number of guesses is 0, I want to jump out of the loop.
So my loop exit condition, I’m going to say number of
guesses is greater than 0.
See what I’m doing here.
I’m only going to jump out of the loop if I have no more guesses left.
So if I’m greater than 0, I’m going to stay inside the loop.
And then last thing I want to provide in the for loop after the
second climate colon is modification of that loop control variable.
So I’m just going to do a minus minus number of guesses.
And what you should notice in this for loop is that
I'm starting... I’m going backwards almost.
I’m starting at 5 and I’m going to 0.
For all the examples we provide in the lecture, we start at 0 and we go up.
But you can do both, it doesn't matter.
You're still iterating here 5 times.
Now I want to display a message telling them how
many tries they have left.
So I’m going to say printf, and actually I’m going to
incorporate the ternary operator here since we haven't used it.
Don't need to do it this way, but I’m going to do it this
way just to show you how you can use the ternary operator.
So here I’m going to print out you have percent d tries percent s left.
Percent d is going to be equal number of guesses and tr
percent s, it's going to change.
It's either going to be tries or it's going to be try.
And so I’m going to change what's displayed based on
how many guesses I have left.
If the number of guesses is equal to 1.
I don't want to plan out tries, one try is left, that doesn't make sense.
I want to print out one try.
So here I’m going to use the termitary operator or more
precisely the conditional operator.
I’m going to say if number of guesses equal 1, then set number of guesses
equal to y else set it to IES.
And I’m going to print it that's what the percent s is doing,
whatever is returned here.
So this is going to return either a y an IES and display
the correct information.
So I just did that just to be a little fancy show you how
you can use the terminal, print out the right information.
After I printed out the number of tries last, then I want to
tell them to enter a guess.
So I’m going to put up with some information and just say
printf just enter a guess and this will have a line feed.
So enter a guess, and then I’m going to read in that guess using scanf, we
know how to read from the keyboard.
It's going to be an integer, so I’m going to do percent d and I’m going
to put it in the guess variable.
So I need to use the ampersand.
Now I have to do my checking.
I have to check if the guess that I’m that I’m concerned about.
So here we have another exit condition.
I’m going to use an if statement.
I’m going to say if my guess is equal to my random number,
what do I want to do?
Well, first I want to print out and tell them that
they're guessed correctly.
So I’m going to print out, congratulations, you guessed it.
What else do I want to do if they guess correctly?
I want to exit the loop.
So how can I exit the loop?
I could put break.
I could do a break, that will jump out of my loop.
Now if it wasn't equal, so I want to add an else if.
If it's not equal, I want to check to make sure they validated the number.
So before I said you could have a nested loop for validation
so that they enter again, you don't need to do that, you
can just have an f statement.
So here I want to validate the number.
If it's not equal to the random number, I want to make
sure it's between 0 and 20.
So if the guess is less than 0 or we're using a logical operator or
2 parallel bars or the guess is greater than 20 which is basically
checking for an invalid guess.
If that is the case and I’m not going to use a bracket because
I only have one statement here, brackets are optional if
you only have one statement.
So if they entered invalid guess, all I’m going to do is printf.
I’m going to print out I said the number is between 0 and 20.
And what will happen here is nothing.
It's just going to print that out and it's going to
go to the next iteration.
It's going to tell them how many tries they have left and
it's going to enter a guess.
Now if you didn't want valid numbers if they entered valid
numbers not to count as a guess, then you can have a nested loop.
But if they enter to something 25, it's still going to count as a guess.
So I just say I set a number between 0 and 20.
If they entered a valid number but it wasn't correct, that
would be the else clause.
Here, I’m just going to say, sorry, wrong number, and I’m
going to tell them whether it's greater than or lower than.
And I could use the ternary operator here, but I’m not going to.
So let's add an if here too.
Else if random is greater than guess, then what do I want to print?
I want to print out sorry percent d is wrong, percent d will be their guess.
My number is percent s is first word.
If the random is greater, my number is greater.
My number is greater than that, and I don't use any percentiles
and I just have a line feed.
Else if the random number is less than guess, then I
want to tell them it's lower.
So I can do the copy and paste the same thing.
I could say sorry, number's wrong, my number is less than that.
And again, you could actually put these 2 else ifs.
You could have one else if and you could actually make
a conditional statement here.
You could have the ternary operator, but I’m going to use 2 else ifs
and say it's less than that.
And then that's going to be it, that's going to end the loop.
And if we get outside of the loop and we didn't guess correctly and
we exceeded our number of tries, then I’m just going to tell them
that they had 5 tries and they didn't guess the number and I’m
going to tell them the number.
So a last output to the user to tell them that they didn't win the game
and tell them what the number was.
So you have had 5 tries and failed, the number was random number.
Okay, so does that make sense?
We're doing a lot of things here.
We're using if statements, else statements in a loop and we're
implementing the number game.
And then the last thing is we return from the main function.
So let's go ahead and compile and what should you have been doing
throughout this whole exercise?
You should have been testing and working in small chunk.
You probably should have started working first just get
the random number to work then test and compile and test.
Then start your loop.
Make sure your loop runs for only the number of guesses, 5 times max
and then get that to work and test.
Then ask the user answer some information, get
that to work and test.
Then compare to when to exit if they guess correctly, test and
then write your if statements test, that's how I would implement.
So let's build this and see if it compiles.
Hopefully, I didn't make any mistakes.
I’m going to say build.
I do have an error, I have invalid number.
It has to be random number, not random because that's
the name of my variable.
And this time I have 0 errors 2 warnings.
I have a percent d expect some matching int.
I forgot the to put out the random number or the guess.
The guess here and the guess and the other one.
I want to tell them that their guess sorry guess was wrong, don't have to
do this, but just a little nice thing.
Now let's build it because you always want to fix your warnings.
You want to have 0 warning 0 errors.
Now when we run this, we have to test.
And there's many different test cases you want to run here.
You'll want to run multiple test cases because remember it's never
good to just test one thing.
So here it says this is a guessing game, I have chosen a number
between 0 and 20, which you must guess, you have 5 tries left.
So I’m going to put in 16.
Sorry, 16 is wrong, the number is less than that.
I’m going to try having it 8.
8 is wrong, less than that.
I’m going to do 4.
4 is wrong, my number is greater than that.
Oh I made a mistake, I didn't read that first.
So it's greater than 8.
Let's try 12.
I only have 2 tries left.
I guess the right with 12.
So it says congratulations.
But you also want to test a failure.
You want to test a few things here, but here let me test and
I’m going to say my guess and I’m just going to put in 0.
0 is wrong.
I’m going to keep putting in 0, and there I guess 5 times I was wrong.
It says you have had 5 tries and failed.
The number that you wanted to guess was 3.
So that works.
Now let's test to make sure what happens if we enter invalid numbers.
if I enter 45.
I said the number is 0 and 20.
So that's another test case and it counted as a try.
If I didn't want that account as a try, it could have a nested loop.
I think that's about it.
We've tested all the cases.
We tested the cases where you guessed successfully.
We've tested the case where you didn't guess successfully.
And we tested the case where you entered an invalid number.
Maybe the only other test would be to make sure that it was saying
greater than and lower than, which I think we tested in the
first one would actually told us to enter lower or greater than.
So let's try that.
So on this time when I enter 20, it says less than which is good.
Now I’m going to enter 10 and I guessed it.
So I guess they quick.
But we should see less than and greater than in the same output.
Try it again, and the random number is working because I’m
generating different numbers each time I run the program.
20 is wrong.
Now let's try 19.
Let's try 1 and it's greater than that.
So I see greater and less than.
So I’m pretty good sure it's working.
Looks pretty good to me.
One thing you should have noticed there was a little bug in the
program I was kind of testing you.
So when you ran it because we have this break in here, it's still
going to print out that I failed even though when I succeeded.
So you should have noticed that in the previous output,
I guess the right number.
It’s printed out congratulations.
But then it also printed out I failed, that doesn't seem right.
So the way to fix that is you don't have a breakthrough,
you want to have a return.
And so now when it returns it's not going to print
out on line 39 if you win.
It's not going to print out that you failed.
So one last test and you should have caught this earlier, but when
we build and we guess correctly, it's not going to display an error.
So we can save run and when we run this one, you have 5 guesses.
I’m going to say 10, less than that.
We're going to say 6, and I guessed it.
And you'll notice there it says congratulations.
We don't see that extra output that says I also failed.
So you want to make sure you add that return on line 30 so
that the program runs correctly.
You could add another if statement if you wanted to so that doesn't print
out the printf, could have had a break here and then at printf you could
have checked how many count guesses you had to also determine if it was
less than 5 then congratulations.
But a little bug there, and hopefully you noticed it in
testing, the return will fix it.
So a neat little program.
I hope this helped you learn a lot about the looping and all
of your decision statements and really problem solving.
Hopefully, you got this out and designed this before
you actually started writing code, that's always important.
Hope this helps.
I'll provide the source code so you can compare it and see and analyze it.
Thank you.