Final Project in C++ Programming | Lecture # 17

Learn C++ Language
Lecture # 17 – Final Project  in C++  

Learn C++ Language

Please wait a little bit to Load

This tutorial we’re going to start our hang man game now and all the previous tutorials we learned about everything that we need to know to build this game. So that being said let’s go ahead and prototype all the functions that we we know we’re going to need.

So one of the functions that we’re going to need is to get a word from a World Bank and return it. So meaning basically we need a function that opens a file looks inside the file and grabs a word and then uses that is the word that we’re going to try to use and use that is a word that we’re going to use for our hanging. So to do that we’re just going to call a function with the return type as string. We’ll just call it get word and we won’t give it any parameters because that’s that’s just going to do its own thing. 

Go inside a word bank and get us a word. And since it’s working with files let’s go ahead and include the library that we’re going to need for that that function which does include efts during file stream OK. Now we have that we’re also going to want a function that prints the board the board that we’re going to need.
And when I say board I mean the man. So we’re going to want a function that prints out kind of a representation of how many lives that the user has left and to do that we’re just going to call it void return type because it’s not non-return anything. It’s just going to print out on screen something and we’re just going to call it print board and this is going to take an integer value and that integer value is basically just going to be the amount of lives that we have live because depending on the amount of lives that the user has left is going to depend how much Athar of the man is drawn. So that’s what that parameter is all about.
And speaking of printing our board we also need a function that prints blanks you know prints the amount of blanks for the word that gets returned.
And so to do that we just kind of call a function that also since all it is is printing something it’s just going to be void or turn type we’ll just call it print blanks and we’re going to give that we’re going to give that function to arguments and they’re both going to be of type string. Because the the first parameter is going to be a the word that we get returned here. And the second parameter is going to be the letters that the user has already guessed and that’s how I can determine what blanks to print and what letters to print and we’ll go over all this in the you know as we go through these tutorials you’ll see exactly how it’s going to work. OK and let’s go ahead and make another function that generates random number because we’re going to use the random number that we generate to actually decide what word to grab out of our word bank. And we are did a random number generator Tauriel as you know. So this should be fairly familiar to you we’ll just call it generate random number and semi-colon.
So these are prototypes that we’re going to use. If I remember a function that we might need or if we decide we want to create another function we will. But for now these are all the ones that I can think of off the top of my head that we’re going to need. So that being said let’s go ahead and set up these functions.
So I’ll say no string get word and set up our brackets. And then we’ll set up a void print Burrard and we’ll call it lives for the parameter void print blanks and that’s going to take two parameters. First home we’ll just call it a chosen word.
And the second line we will call letters guest you’ll see exactly why we’re calling him that later.

And the last one was our random number generator and that one didn’t take any parameters. So there we go. So this is the basic skeleton. Now I also want to go ahead and add some stuff to our main function while we’re here. And the way our main function is going where we’re going to go ahead and initial II this are our use your lives we’ll call it easier lives to 7. And then basically we’re going to say wow user lives is greater than zero. We want to do this anything inside this league and basically this leap is going to allow the that’s going to allow the user to keep guessing letters as long as the lives are greater than zero and that will set a break statement in some way if the if the word gets guessed correctly then we’ll do that.
So but that’s how we’re going to set up our main for now and we’ll add more stuff later. And the other thing that I want to go ahead and do is since we it’s kind of fresh in our mind is go ahead make our random number generator while we’re in this tutorial and to do that all we do is say you know string or my bad I was had.
I had that and get word on my mind. But the random number generator would just have to include two robberies if you remember one was a standard library a. And the other one was Tom A. So there we are those are the two Iberis we need Dalia’s. Go ahead make our random number generator. We’re just going to say as grand time no. And remember this was the this is the function that we need according to C++ dot com and the reference that we used and then we’re going to say we’re pretty much just going to say return and you know there’s two ways to do this. You could say return Ray and percent and then I don’t know how many words we’re going to have in our bank will just say we’re going to have 10 for now ten plus one. And this right here we’re a turning integer value a random integer value right here. If we do that. So that’s what we’ll do there and we might have to come back later and change it. In fact just for to avoid the confusion we’re going to do is say random number equals Rand and make this integer random number equals Rand musculo operator 10 Plus 1 and we’ll say return random number.
There we go. And if you see we go up here and we just comment this out real quick. I actually can’t do that sorry. This is another way to comment. By the way is what the star backslash just like that. That’s a new way of doing it just to kind of explain that. So let’s go ahead and test a random number we’ll say returned generate random number and just make sure it’s given this random number and it’s not. Let’s see what’s reference generate random number random number it’s going to make sure things are right. There we go. We called it random number generator.
We’re probably screaming at me whenever I called it that. So we call it SHINAR a random number. There we go. And it says processor turn 30 and let’s read it one more time processor turn three. Just coincidence. There we go process return.
So we’re using a random number every time. And while we’re in this tutorial I want to go ahead and I want to make a word bank and bag. I think I mentioned this one and we have employees it takes to delete that. Real quick
Eric and let’s just make this into a new document. Texstar comment or just call it wordlist RTX T. We’re going to open it.
Wordlist tigs T. There we go. Open. I will just say no give it a header word list.
We’ll give it a couple of no words. So say giraffe rhino.
Really. You know try cricket grasshopper buzzard just think random you know words off the top of my head. There’s not really a theme going on here.
Let’s see I mean that’s one two three four five six seven eight. So let’s get to more we’ll just say no. Day two and one really good word. Well save Lennox.
There we go. So that’s ten words. Not really a overall theme there but the 10 words we use for World Bank for now. 
And let’s go ahead and in this Tauriel go ahead and make our get word function since you know input and falling output it’s kind of a thing we’ve been practicing for a while so go kind of quick. So we need to go ahead make our variable call it if string format that it will get better if Stream we’ll call it input file then wordlist RTX T and we’ll know pretty much since this is returning a string warno will say you know if I input file we’ll print out an error but we won’t return because this is  a string return time so we won’t actually be able to return a an integer value here anyways.
But we’ll say you know air negative six. No word list not found that will that will let us know at least if the word list wasn’t found and then we’ll say let’s go ahead and make another variable here we’ll call it temp word and then we’ll just say Propagandhi at temp where we go. And actually we’re going to need an array.
So we’ll say a string array we’ll call it wordlist or yeah we’ll get called wordlist and just go ahead make a constant value Konst in word list size equals 10 and you can put a comment here in the code to let you know you know change the word list size here.
Just to let yourself know later you know if you make a bigger word maynt. Now if you want change you to a hundred. I have to do is add 100 right there will say a word list. Word list size equals one just initialize the whole thing to blank strings and then since we’re going to need a Forli we’ll use this index variable set to zero.

And they’ll tell you the word. But if your lives is greater than zero then obviously you broke out of this while loop in a different way which was via the flag. So let’s say use your lockset three is set to true print blank shrine’s no Brint no blanks were printed basically meaning that all the letters were guessed correctly.
  Flag is still true. When you break out of this while loop Well then you’re breaking out of this while loop. Why usually lives just three nephews lives just three minutes greater than zero than this code is going to get ran instead of this code is going to say you win. And that’s basically the functionality of of this code it’s actually rather simple because basically we just created one more variable and we just we basically just you have to think about it a little bit.
You know we came down here and we just set the flag to false. If one blank got printed basically a blank it all gets printed then the flag is going to be false and
they will never be able to break out the while loop B the if statement that checks that flag is true. You won’t be able to break out and the only other way break out is if Laza is equal to zero. And if logicals is zero then you lose. And so that that’s how this works. And if we run it I want to go ahead and show our finished product when we run it we’ll go ahead and we’ll.
So this first word just as we have 10 words and I’ve kind of ran into a few towns and I know what it is is Linux say no let me get a couple of letters wrong so it’s starting to jar me. And you can see but if but if we get it all the letters right it’s all set. I mean you X and you can see it’s telling us the letters that we’ve guessed which is something I want to add in and then says you when the word was Linux process returns 0 and you can see that that’s how the game works.

Check Also

ACCA F2 Management Accounting Lecture 86 – Performance Measurement – Introduction

ACCA F2 Management Accounting  Lecture # 86 – Performance Measurement – Introduction Please wait to …