this video shows using C plus plus how to create a game where you guess the letters in a secret phrase kind of like the game of hangman but you can put your own theme on it okay so we're starting out here I have included IO stream this is going to run in a command prompt and uh included time.h because I'm going to use the random number generator to select the random phrase that we're trying to guess so you need to seed your random number generator at the top and then a intro screen but you know do that last get uh add the I call it the bells and whistles last get the main program working first okay so now what we're going to do is uh let's select random secret phrase okay so we're going to have a string variable called secret phrase and that's going to be equal to the secret phrase that the user is going to try to guess so we need to to make a random let's first create an array of phrases so an array of string uh I'll just name it phrases and I'll just start with something small you probably want to add more than that is equal to and I'm just going to put some short phrases in and I'm at this time I'm not going to use capital letters because what I'm going to show you does not work with capital letters yet so I'm going to put like happy Monday comma great Tuesday comma awesome Wednesday uh in fact just to say screen of the real estate let's just make this three right now but you know a good program is going to have a lot more secret phrases than that okay and so now let's make ours during secret phrase equal to a random one of these right here okay so to do that we'll say phrases uh and then the index number needs to be either 0 1 or 2. so we need a random number from 0 to 2 and so we can do that by going Rand which gives us a random integer and mod it by three so that gives us a random number from 0 to 2. now at this point you should test your program a common thing to do in programs is to print the value of variables and of course they should be these prints should be taken out in the final program so I'm just going to Output secret phrases equal to and then the value of that variable okay um indel okay so we should test our program right here and so secret phrase equals happy Monday let me try it again make sure great Tuesday okay so I'm going to assume that my random selecting a random phrase works okay so now the next thing that we're going to do is let's um create guess phrase okay and so this is going to be what the the user is not going to see the secret phrase for obvious reasons but the user is going to be see the guess phrase which will be a series of placeholders to represent each letter in secret phrase that they're going to try to guess okay so um to do that let's create a variable a string variable called guess phrase and I'm going to make it equal to secret phrase to start out with okay but then I'm going to replace every letter in guess phrase with my placeholder so for example so let's just do a loop int I is equal to zero so we're going to step through every letter of guess phrase while I is less than guess phrase dot size I plus plus and so now to make it work with spaces what you can do is you can say if secret phrase at index I is equal equal to a space because if it's equal to a space I want uh the guess phrase to have that space in there also think about like Quill of Fortune you know it shows you the individual words the spaces and also punctuation so you could add punctuation in with this so if that's that if it's equal to a space then um then let's make guess phrase index I equal to the space otherwise let's make guess phrase index I equal to the placeholder so I'm going to use a period for the placeholder um now underscores run together that seems like that would be the obvious placeholder underscores but periods work good uh question marks asterisk or dashes work good okay so now let's try it out and again we need to temporarily just print the value of that variable so so let's print the value of uh guess phrase and I think the program will start making a little bit more sense now is equal to guess phrase okay so let's run it and so there it is so if let me add another space so you can see that it lines up good in fact let me just put this down here I'll print both of these out together oops wrong one uh this C out okay so I'm going to print out the secret phrase and then the guess phrase together and so you can see okay so if the secret phrase that was selected was is great Tuesday then the guess phrase this is what the user is going to see is going to be equal to all these dots okay and it shows you where the space is which helps out okay so um now that is working good so at this point I think you can probably get rid of those two c outs those were for testing and so now we need to declare some variables uh one is going to be the number of times the user guesses incorrectly and I'm going to call that bad guesses and I'm going to start it at zero now like in the game of hangman you get six bad guesses before you lose so you could just use that of course you can change that okay and um I'm going to declare a string variable called letter and this is when the users when you're asked the user to enter a letter that's the variable that you'll put it in Okay so now is the main game Loop okay so the loop that can continues executing as long as you're playing the game which will be a while loop now when do when does the game end the game ends when the player wins or the player loses so first of all you know um you know they're gonna guess letters and you're going to replace those letters in guess phrase so if guess phrase equals secret phrase then the player has won so while guest phrase is not equal to secret phrase meaning we want to keep playing the game keep looping as long as those two are not equal and also while bad guesses is say less than six if you want to use that number okay so now sometimes here's another little thing sometimes uh you'll see programmers like on a closing brace they'll put um in game Loop uh they'll put because there's going to be a lot of code in there and so when you see that closing brace and you'll know what it corresponds to since it's since it may not fit on the page okay so I'm just going to compile it make sure there's no uh uh it would be an infinite Loop right there just making sure there's no um syntax errors okay so now what we're going to do is right here would be display graphics and you can do that later so you know if you're doing the game of hangman uh then you draw the little stick figure and that's going to be based on the value of bad guesses so you should have some kind of Graphics that changes depending on the value of bad guesses and so you can look at the example programs to get some ideas there okay but do that later get the game get it playing the game before you do that so now what you should do is display the letters already guessed or the letters uh remaining I like displaying letters remaining and we'll look at that in just a second oh and so anyway yeah you should have another if you're going to do letters remaining which I like better you should have a string variable named letter remaining REM aining and make it equal to the alphabet and so as a get as a user guesses a letter we're going to remove that letter from there okay then uh so we'll put that there I I uh letters remaining so we can just see out letters remaining put some text with it I'm not going to do the entire program don't just output the alphabet let the user know what it is and then you should output the guesswork a guest phrase and again let the user know what that is don't just say you know just don't put the guess phrase put some text to go along with it okay and then you should uh we'll ask the user to enter a letter enter their guess and then CN into letter like that okay so now at this point we should test it again it won't be in an infinite Loop since we have this CN statement here let's see if we have any errors we have an error letters remaining okay let's try it again and another one uh see out guess phrase end l okay let's try it again and so there we go okay so here is the letters remaining and every time they guess a letter you'll remove that letter from letters remaining so it's not playing the game right now but you know there's no syntax errors it's asking me the enter letters okay so now um to fix to delete that letters remaining what you can do you can say if um let's go int found is equal to letters remaining uh found his uh well letters remaining dot find and we're going to search for the letter beginning at index era and so then if found is greater than negative one because negative one if it doesn't find it then replace it with a space so letters remaining dot replace at index found replace one letter with a space string okay now that should be working let's see what happens and so the error is a letters [Music] remain [Music] well okay I should have picked a shorter variable okay so now it should be deleting those letters which it is p f g a okay so you'll notice that right here it's delay again you need some text with this this is not very clean this would confuse any user what are we looking at here okay output text from what each of those things are okay so that much is working okay so now uh really these only one more part to make the game work so I should put a comment here uh remove letter from letters remain remaining okay um well I don't probably don't need that comment there okay so remove that okay now the the final there's just one more thing to get this program to work and that is to um replace all occurrences occurrences okay spell check um currencies I thought that oops oh well okay replace all occurrences that's embarrassing isn't it uh of letter in guess phrase okay so um so now that step is a little bit complicated because like if you don't find letter and secret phrase then you've got to increment bad guesses so here I cheated I've got the code right down here okay so I'm going to paste it here and then I'm going to indent it to where it needs to go right there okay so now if there's no problems it should be playing the game uh but there is a pro oh location um oh so I'm using found instead of and then found here okay secret phrase so what we're doing let's see if it works and then I'll go back and explain it okay so let's see if it's playing the game I'll do an i nothing uh a okay so see it worked it found two two A's there's probably an a I mean a y because we're doing days of the week in f w t oh Tuesday okay so it is playing the game as you can see that would be great Tuesday okay so it is playing the game which is good um definitely needs to be cleaned up a lot with some text so the user knows what's going on okay so now it will end um so uh right now it will win without saying you've won or lost so what you need to do is down here after the game loop after it exits after you've won or you've lost then that would be down here um you have won or lost so you could say if um uh what is it secret yeah guess phrase is equally equal to secret phrase then you've won so if guest phrase is equal equal to secret phrase then you've won okay hopefully you'll have a better winning screen than that and then else C out uh loser like that okay so let's try it to see if it tells me if I've won or I've lost okay so I'm going to go for a y a d n T oh it's Tuesday again so G r e s okay so it says window winner but notice it didn't finish this because it skipped it skipped out of the game Loop so you'll need to down here you'll need to display the winning word okay because it's because when you won you skipped out the game Loop so it didn't display your guess phrase so make sure you you display the guess phrase or the secret phrase either one if you've won or if you've loosed lost okay and so let's check it to see when we've lost to see if it's working and um so I'll just go through the alphabet e f g h i okay so I got to bad guesses so loser okay so that's working okay so now now that you have the program working now you can make it look really nice with an intro screen you could put asciard on it with um with display your graphics right here and you're displaying the graphics that could be that could be based on bad guesses so you could say if bad guesses is equal equal to zero meaning you haven't then you could display that ASCII art okay else if bad uh guesses is equal equal to one then display that ASCII art okay so that's how you would do some ASCII art in there okay uh now uh for I guess I'll go ahead and that's the basic game I'll go ahead and show uh for adding color um for adding color uh color now works the same color now works on Mac on PC command prompt Mac terminal and also online GDB which is an online compiler that runs on Linux so the same colors work there and so what I'm going to do is I'm just going to put these in I'm going to paste some ANSI Escape sequences here and if you're on my online book this is on chapter 21 uh so these online Escape sequences uh will change the command prompter terminal color okay uh one note though on Windows uh you may need to do a register setting so uh of course I have I have that listed in my chapter 21 bells and whistles if you're on my online book how to do that okay so this is the default color right there and and also CLS clears the screen so the the good place to clear the screen would be right here and to clear the screen you just see out that ANSI escape sequence which is CLS and now it will look like it before it was scrolling down the screen now it will stay on the screen okay at least that should be Wednesday okay so now it stays up here on the screen which uh helps the looks and for doing the other colors you uh remember the default gray colors just default like that so like for example letters remaining uh if you wanted to Output letters remaining in a different color uh then you could like output purple and then go back to the default color otherwise everything past it forever will be in purple okay so you can see so now letters remaining is in purple okay so that's how you can change colors there okay so I think I wasn't really going to go into doing the actual ASCII art you can look at some of the example programs to see what it's like uh you could put asciard in there an intro screen or of course you could um I would do an oh one more thing for ASCII art uh uh if you just Google ASCII text then that is a great tool right there ASCII text let me make it a little smaller for doing ASCII art right here okay so I Googled ASCII text and clicked on the first link great tool they put together here like uh if you just want a nice uh easy to read font you can just do this one called big and so then you could you know put whatever you want your name your credits uh whatever you want or or actually your theme you know hopefully you'll think up some theme to make your ASCII art and your secret phrases and stuff like that okay and then of course this can just be copied and pasted and then put into the C out statements after you fix the backslashes and quotations if it has any of those okay so anyway so that would be good on your interest screen another thing to keep your main program which is already kind of big to keep it kind of smaller uh I would suggest putting uh some of these things like the intro the winning and the losing screens in a separate function so here you so because it keeps your um it keeps your main program cleaned up okay and so you know you could put that down here void because I'm just going to have it display the screen not return anything intro screen not sending it so it's just gonna um be my intro screen oh and one more thing you could do on your intro screen first of all this wouldn't display because your CLS would clear it out so to have it stay up on the screen you can do um press enter to begin or something like that let me do that there and there uh and then if you want the user to be able to press enter to begin you can do a CN dot ignore so that allowed otherwise it expects you to enter something so CN dot ignore okay so uh oh okay and since I put my interest screen down here I need my function Prototype at the top of the program so I'll just put my function prototype here which is basically just the header line of the function okay and so now this will be my intro screen you know ASCII Arc colors all sorts of nice things put your name on it your theme tell the user what to do and then press enter to begin and now we're in the game and so you can do the same thing with the losing and the winning screens okay I think that's all I was going to cover in this so I know that's a lot but it's a fun program and things that you can do you can do a whole lot of customization and make something pretty nice and I'll put a link to those example programs down below