Coding Challenge Create Hangman Game

Video

Transcript (Unedited)

what's going on good people this video will be a coding challenge video the challenge is actually a little bit more difficult than what i'm used to this particular challenge is to create a hangman game and as you see here there is some criteria of the to do items and pretty much what the objective is on how to build out this game this core example starts off with python but of course like in convention we will do this in c-sharp using.net core let's get into it all right i have visual studio code already pulled up and all of the existing or prior coding challenges are available on github in a repo called coding challenge link will be provided to that repository to get started we're going to create a net core console application and i'm going to name it hang hangman no sense of overcomplicating the name and once that project is created i'm going to go into the hangman directory and dot net core uh get ignore and that's going to create a get ignore file uh oh sorry i forgot the new keyword so it's dot net new cool dot net new get ignore mess that up too all right let's try this one more time dot net new get ignored all right there we go so next i'm going to open the repository or excuse me open the project and i'm going to create the programs here open the program cs file and then we're going to remove this statement as well as update the namespace to my personal convention uh you can update the namespace to your particular convention if you have one for your applications and while we're at it we're gonna go ahead and let visual studio code build out the necessary files to build and launch the application all right next thing up is we're going to take the word list that is provided in the example code over here and we're going to type it in here um let's see if we can get both of these kind of yeah that works all right and the word list is an array now mind you this is in python so i'm going to stick with an array with this but i'm going to name the variables according to the c sharp syntax so we got aardvark got baboon got camel and that will be the end of that and then we have our to do one which randomly choose a word from the word list and assign it to a variable uh let's see if we can stretch this out a little bit or assign it to a variable called chosen word all right to do two says to ask the user to guess a letter and assign their answer to a variable called guess and that probably should be in quotation marks make guess i think that's the non-quotation mark version of it lowercase and to do number three says to check if the letter the user guest with the guess in parentheses is one of the letters in the chosen word all right so a little bit complex a lot of comparisons that will have to be done to make this doable um i'm gonna go ahead and make this wrap so i don't have that horizontal scroll bar and that should be it okay i'm gonna do format and we'll trim any trailing spaces before we get started that being said we need to randomly choose a word from chosen word so we're going to create a random object and using the random class we will place that there and then we need to get a random and i misspelled answer here we will need to get one of these three words at random from the list so we will call a integer or define a integer wordless [Music] ant i think that's we'll stick with that name may not be the best name for it but we'll stick with it and we'll do a random dot next with zero being the lowest number and the max being word list dot leave this link yes all right and so that make sure that and this here will ensure that the wordless.length that is will ensure that we are not getting a word that is outside of the index of the array that is defined up here on line 9. next we need to assign the word to a variable called chosen word so create a string called chosen word and set it equal to word list with word list ant embraces and that will get aardvark baboon and i misspelled baboon babin isn't i don't believe is a word but it might be hard baboon and camel will be placed into the chosen word item all right task number two is to ask the user to guess a letter and assign their answer to a variable called guess make guess lower case so we're going to do a string of inputs input is equal to console dot three line and we're gonna check that to to lower and we're going to up here console.writeline and set this to enter a letter there we go all right and make it lower case so we've done that and the variable needs to go into actually it needs to go into guess instead of into a input and since it does say one letter what we could do is actually use a char called guess and because chart will only hold one letter or number or character and so that will minimize and make this a little bit easier to manage to get um so it does not like that so we may have to take this to lower out so let's go ahead and do that there and then uh really okay so uh well maybe not since it does not like that being done that way unless i can convert this read line to a char which i do not believe is possible i can do a char array and then get the first item out of that array so i may do that instead um yeah so to get the chart array and then we'll put that into a um char open close and then that'll be called yes that should be sufficient and then for each um var guess letter we're going to guest letter in guess we're going to do and actually that may not be the right way to do that so we'll do for it i equals 0 i less than um guess dot length i plus plus technically you should only be accepting one letter in here um so what i'm gonna do is i'm gonna take whatever the user types in but then what i'm gonna do is actually only handle or process the first character that's in the full input so if they type 50 characters i'm only going to count the first one and that will prevent it from trying to compare a lot at once or um having too many values trying to come through at once all right so we want the first item out and so we want to set the we also want to change this lowercase because that's the other thing that we have to do with this so guess i equals um guess i dot two doesn't have a two lower case on here which is uh i believe is because the lower case only applies to a string and so what may have to do is to temporarily convert this to a string and make it lower case then set it back to a car [Music] so we'll create a string uh for now and then do temp equals temp dot to lower there we go and then we will set the oh and also need to convert this dot to string and then let's see can we do guess i is equal to temp i may have to do converts on this one dot char 2 char yeah and [Music] no it's not what i want convert dot sh [Music] oh i had to convert the um so temp dot string all right two two string two char array i'm sorry all right so now that we've converted everything back from upper to lower we converted back to our array and this may not actually even need to be done this way um because [Music] we're trying to do a single charge to a whole thing that may be a problem so i need to do a um let's do a char array that is actually so if i read this and then do temp plus equals now that resets every time so that's not going to work all right so i need a temp string outside of this yes so we'll do temp there do a temporary thing here set that to no value actually set that to an empty string to start with and then yes i said to string equals 10 plus equals temp and no actually i need to change that to a lower and then add it to the variable and then outside of that to attempt to back to guess and handle that so uh essentially this for loop will loop over every item in the array in the character or the guess array and from that change all of the values to lowercase equivalence and then turn around and put that into the char array i'm thinking that's probably the best route to do this may actually want to change this up though to be honest because it probably doesn't need to be a char array probably just needs to be just getting the first one so if we get the first um item out of here and put that in then we won't need to convert everything we just need to convert the zero index uh assuming that the zero index is well assuming that there's something that has been typed in because if there's nothing that's been typed in then that doesn't matter um so we can just do that instead of looping through the entire list of items so we'll do a string temp let's move this here and change adjust the indents um better yet i'm thinking well since we're only supposed to store the guess and that variable name guess um what i'm gonna do is leave that there so we'll call this a uh char i can't change a chart to a chart array so we'll change it to a string for the um wait guess and no so we'll call this input then we'll do input of zero is assigned to the string guess and then from here yes equals guess dot to lower and we can do away with that now that seems to be a little bit more simplified for what we're trying to do all right so now that we have the first character out of our string we actually probably want to check to make sure that the string is actually has a value here so i want to do input dot length is greater well yeah greater than zero then we want to do the stuff inside the if condition otherwise when you go to run this and the length is equal to zero then it will throw an exception for an index out of bounds and so obviously knowing that you don't want to code that in all right so that should do that particular part all right so the to do for number three is to check if the letter the user guest is one of the letters in the chosen word so to do that we'll do if um chosen word dot contains and guess yes okay and actually guest needs to come outside of this uh if statement so we'll do string guess equals to an empty string and so if that contains then console right line the letter you guess is contained in the word all right and obviously there's some more logic that needs to go in here um but i'm just putting these statements here for now so that we know what should go in each of these statements the letter and then i can do a uh the replacement string here is not contained in the word and then we'll do guess as that value then we'll come back up here guess and letter there we go is contained in the word got it all right so now that we have this existing logic well this logic in place what we want to do is to go ahead and build it and make sure that what we have is running thus far and also that it will build thus far but you will know though is that this will not repeat um once i put a letter in so if i type a letter um we'll do a capital p and for some reason i have issues with this working in visual studio code so let me open a terminal and run this application all right so i want to do dot net run from here and it should present the same information of asking for a letter to be entered i'm gonna do an uppercase p and it says the letter p is not contained in the word um we do not know which word that it picked so we probably want to add that piece of information to the output as well as the application uh that we're running because that's not also being output so we'll say welcome to hangman will be a capital h and we want to say [Music] all right so we have the chosen word so we can do console.writeline and say that the word has and we'll do a replacement there characters and with that we'll do chosen word dot length for the substitution value so now if we come back over to our terminal and run this again before enter a letter shows up on the screen it should say the word that we're looking for and so given that we only have three words it's pretty obvious which word that we're looking for and so the last time we typed p and we confirmed that p was not in the word uh whichever word it was but this time we actually know where it was which word it is because we can see the words and so we can type a letter in here uh we'll go ahead and type a and actually i'm going to type uppercase a just to make sure that the lowercase comparison is doing correctly and so the letter a is contained in the word and as you see it also dropped the lowercase or got the lowercase equivalent of that so that is working i did notice that the punctuation was missing so i'm going to add that in right now while i see it all right now we need this to continue to loop over instead of exiting like it's currently doing so what i'm gonna do is to include a while loop and i'm going to wrap that while loop around this to do and for starters i'm gonna just default it to true just so that the error messages go away but i'm actually going to fix that here shortly so that it um only actually is a legit while loop condition and so what we need to do is that we need to normally in hangman you have a number of tries so to do this number of tries you need to find a variable that does the number of tries or counts the number of tries we're going to call the int tries spelling this are uh yeah we call it entries and um or guess attempts would probably be more appropriate and we're going to initialize that to seven and then so while guess attempts is greater than zero because once you hit zero then you're out and we want to see that if the guess is not uh if the word is not correct then we want to remove the guess remove one of the guest attempts okay so since we also have the guest attempts we'll want to print the number of guesses remaining to the screen so we'll do console.writeline remaining attempts and we'll do the placement of the variable in there and semicolon and actually i'm go ahead and rename this to remaining attempts because it seems i'm trying to get away from using the same uh variable because we already have guess as a variable down here so when it comes to typing you'll try to make your variables fairly distinct that way if you have for example if you have string guess 2 well when you go to type somewhere down here guess it's going to auto auto excuse me it's going to uh use the intellisense and intellisense is going to throw whichever one it thinks you might be trying to type which in most scenarios for me uh doesn't necessarily make it to the correct one so i'm going to rename that for that reason all right now if we um come over here and run this and it will build all right so we have all the information welcome to hey man the word has six characters and enter a letter so we have let's say we're looking for t letter t is not contained in the word and as we also see the remaining attempt has dropped down by one to six and if we keep doing this and if you don't put anything in there the remaining attempts remains the same which is what we want um letter is not contained in the word and if we exit the loop we need to put a condition there that checks to see what happens after we exit the loop because there should be two conditions where you exit the loop if you run out of attempts or if you get the word correct so right now we're working on the attempts portion all right so if remaining attempts is equal to zero and we want to console.writeline you have ran out of attempts you lose and i know that's kind of a crude error uh it's a crude message but that's essentially what has happened all right that being said um the next part we can work on is actually doing the comparisons for if the guess is contained in the word so how do we do that um so we need to get the uh we need to also be able to display the word to which is a little bit complex um but i'm thinking we probably need to do the display in a separate function so what i'm gonna do is i'm gonna build it out within this if condition and then take that statement once it's built out in if condition and move it over to a function where that would make sense and so i'm gonna go ahead and move this over to a function as well so we're going to extract a i believe it's extract method so that will take that to a separate method and [Music] pass that in and so if we come back up here and say exit game as the name of the function and we'll put all of the stuff that needs to be printed out to the screen inside of this exit game function and therefore you get a more concise uh loop and or main we'll continue that a little bit further and then um we could also leverage some class variables here too because we're using this function instead of passing these arguments in so what i'm going to do is actually make this remaining attempt into a [Music] if it'll let me do a cut it's not a constant um well one move it near the reference because it doesn't make sense that it's that far up uh all right now let's see if it'll let me do a not letting me do it so i'm just gonna have to force my way to do it we're going to declare a static variable with that in it all right and therefore if i have a static variable i do not need to pass the value in because the function the variable is because it's in the class it now knows what it is and so i don't have to do that um but that also means any function outside of domain can modify this value which may or may not be a good thing depending on how you're writing your code but it's fine in this case all right so now that we have our remaining temps configured correctly um the next step is to handle the correct guesses so we have it checking that if the letter is there then that is correct so we need to do a comparison of the we also need to track the letters that we already have guessed so let's do a um i want to declare another thing i want to declare another string array that says already guest letters and actually no that can't be an array so it have to be a list and the reason being that it would have to be a list is because that if it is an array that array will not uh hold the values in place so what i need to do is to do list of not word lists string and we'll call it already guest letters equals new list of string [Music] and we need to import a collection statement there for that which is [Music] system.collections.generic and let's see what else all right so we have that so we want to anytime we type a letter in we want to add that to the list of already guest letters so we're going to do already guess letters dot add and we want to add after we drop it to the lower case or convert it to lowercase then we want to add it to guess or add guess to it excuse me all right and let's see so that is correct um one other thing that i noticed that i probably want to go ahead and make it a little bit more difficult to make this a problem in the future is so the word list is already lower case but let's say somebody comes up in here and types a capital a in here instead well then we still wanted to remain lower case so i'm gonna do dot two lower on this uh setting of the word to chosen word that way that ensures that even if somebody does type a uppercase letter here that it is handled properly all right so we're adding our already guessed letters to the list and then we need to do a comparison of the already guessed letters versus the um not already guess letters what i'm thinking so um let me change this back for the sake of uh the way it should have been and already guessed letters so i want to i think we need to do a for loop on um you probably also need a variable up here that call uh that is a string that is called visible letters and we'll set that to that and then we want to set the divisible letters equal to wait 1 2 a 4 each var letter [Music] in uh actually no yeah before each in chosen word we want to set visible letters plus equals to star and that will be our masking of the letters um trying to think is that the correct that may or may not be the correct way to do it but this is one way that you can do this what i'm thinking is we may just need to cycle through each of the letters and if the letter has not already been guessed then that letter is replaced in the output before it is displayed to the screen and that way we know which letter um we don't have to keep track of the word in multiple well we may still need to do it in multiple variables won't be as difficult all right so let's see oh for each letter in chosen word uh or for each um yeah so for each letter um in already guessed honestly we want to start with the letters that are already visible and create a temporary string that will set so we'll do a um call a string display word set that the empty and then for each letter in chosen word we will do a comparison so if letter is equal to letter let's see no if trying to think already guess letters dot contains and then letter then and we'll make that a if statement i've already guessed letters contains letter uh with the letter passed in is that correct then we will add letter to the output so we'll call this uh or display so display word plus equals letter and if it does not else we'll do display word plus equals asterix and i think that's the proper way of doing that um what is wrong with this letter can i convert char to string got it so convert that to string with the parentheses that should handle that all right and so let's see what this does i want to remove this for the time being because i know i'm not going to need it and we'll come back over to our terminal window do clear and then do dot net run all right the variable visible letters is never used hmm okay that's fine i noticed i misspelled it too so uh we'll comment that out for the time being as well all right so our letter excuse me our word has eight letters uh we will have seven attempts enter a letter um so let's do an o all right so o is not included in the word six remaining attempts now i noticed something that the display is not printing out which it should be so that is a problem so let's move the display word um the display word needs to be reset every time it also needs to be visible where it could be fined [Music] so this way word contains guess as i'm looking at this i think i actually have the same condition in here or than i should because the same function is going to be called in either scenario [Music] and i need the display word to be kept in between cycles um so we'll do display word and i need another console right line because what i want to have happen is see how these are all crammed up right next to each other i don't want that to happen so what i'm gonna do is to after each attempt um so remaining attempts is six remaining attempts remaining attempts so i'm gonna add another console.writeline and just put a blank line there actually i don't even need quotes there just do it empty and that should do the same thing all right um now you could do all kinds of other kind of formatting like separating it with equals or not i'm keeping it simple so blank line will do in this particular case all right so now that we have guest letters and so chosen word contains yes then um and we may need to change this logic all together may rather need to do it this way instead of the other way um i'll leave it there for now all right so we need to reset this way word each time we come through and guess the right thing so what i'm gonna do is do display word it's equal to empty string um because if you keep appending to it then it won't be pretty after the second go around and at the end of the loop need to do console dot writeline and display work and that should be that all right so let's try this again clear the terminal run the application and let it build so now we have our space there so let's go ahead and type and oh all right so the letter o is not contained in the word got it so let's type in a it's still not printing my the actual word so that is a problem because it should be still displaying actual words so we may need to copy this here and put this there all right so let's exit and try that run again oops close ring let's run again let's put the letter o all right so now we're getting our word here so let's put the letter a and see what happens all right so we got the letter a and our attempts remained the same that's correct so we put the letter d we got our d what is it aardvark goes r we got our r and then we got a k and a v and we've spelled our word now only thing is a display word is the same as the actual contained word but we don't have a condition in our loop that would exit out that being said so we need to do if remaining attempts is greater than zero and if display word is not equal to uh nachos yeah chosen word i believe this is the correct way of doing this and then so if remaining attempts is equal to zero and then i want to put another statement here and have it say it could be an else statement here but i'm gonna do it this way if display and i don't have display word there so i probably will use an else statement here just to simplify this so if you're exiting the game that means you've done one or two things you've either gotten it wrong or you've gotten it right right line so you have won the game um and say you have yeah you have won the game that should be sufficient all right so coming back over here let's do a clear dot that run and hopefully this time uh get it right so our six letter word in our example is baboon so we can put a capital t in here for the sake of it and the letter t is not contained in the word now one thing we did not test but it should still work is if we put a number or a special character and i know you can't make letters and numbers out of special characters and there is a way to check that and make that possible but for the sake of this we are not going to add that logic we pretty much figure out if you put something in here that you shouldn't put in it's not going to work or it will be wrong so i put a pound sign in here and the letter pound granted that's not a letter um so we may need to change that output statement is not contained in a word so now that we have that there and i'm noticing something here is the original word has six asterisks here now this word has six plus six which is 12. so the condition to uh if your word is wrong is not doing something correct now i believe it's because it's not resetting the display word here so we needed to reset the display word regardless of where it's at so we need to move it up to here instead and i'm going to comment that out for the time being and then this here really needs to be a function because it is the same logic for both statements so i'm just going to go ahead and select that now do control dot and do extract method and so we're going to call this method um check this way i guess we call it um not check display let's see uh [Music] process display word all right and i know that's a mouthful but essentially that is what it's doing and so since this is the same as this down here it's going to go ahead and copy paste that there format and trim and then save so if we go back to our terminal and we'll try to run this again and make sure that that is correct all right so we have a five letter word a five-letter word in this example is camel so camel does not have an r in it so if we type r uh we get our five asterisks which we still should still have and then also have one less remaining attempt camel does have an m and we'll put it uppercase m in here for the sake of it it still should pass and it does and we still have six remaining c a let's do uh q for the sake of just because and e l camel so we have gotten the word and we have won the game um now if you want to you can take this a little bit further and actually do the drawing of the hangman part um i mentioned that earlier but i probably won't do that on this particular video because of the fact of drawing characters or ascii characters does take a little bit of effort um so let's walk back through the code one more time before we close out the video all right so we have our welcome to hangman opening here um we have our word list this word list can be longer just add more animals so dog you gotta do it in quotation marks dog elephants um ferrets uh efg so um giraffe there we go all right so we have one letter for the first one two three four five six seven letters of the alphabet that should be sufficient and then we did our two do one which is randomly choose a word from the list and assign it to a variable called chosen word so that's what we've done with these four lines here um we've had to add some additional variables here because of the fact we need to have those for our while loop and we also added the uh remaining attempts so that you know how many times you have remaining before you lose the game and then enter a letter so we need that information to prompt to the user for what for them to do something then you take that guess and process it accordingly get the first letter out of there if they put multiple characters in there it is possible you could handle that but for the sake of simplicity in this example we're only going to handle that with the by grabbing the first letter out of this string and do already guess letters with ad and um i actually come think of it i may actually have made a mistake i realize this condition may actually take [Music] um an error message i mean excuse me blank string and decrement the steps or decrement the remaining attempts but we'll have to test that again so already guess letters you add that to the guest the letters that have been guessed and then if the word is chosen contains the guess then that letter is contained in the word and we will process the display word so that is a number of arguments that are passed in and you loop over each letter in the chosen word and to see if that's there and so if if it is there you display that letter if not you show an asterisk in its place and then return this back to the caller um and then inside of that that would be it if you're doing this same set of stuff inside of the else condition you would decrement the remaining attempts and you would display the word uh regardless of you know asterisk letter combinations or just asterisks only once you get done with the while loop you will exit the game and that exit game comes down here and it determines whether you had zero remaining attempts and if you did have zero remaining attempts that means you ran out of attempts and you have lost the game um if you have remaining attempts that means you have won the game and one thing you probably want to also do here is do a console.right line and say the word was and then print out what the word was so um because usually if you lose you find out what the word is and so what we want to do is we'll probably have to set chosen word as so i set that one as a class variable i don't know if i want to do that well i'll go ahead and pass that in here as a class variable so um string um the word that's what i'll call this one here we'll wrong function all right so that there so therefore if you run out of attempts you do still find out what the word is and so now that we made that one adjustment let's do uh net run one final time so we have seven a seven character seven character word and seven remaining attempts so to enter a letter we're gonna do uh we'll put an h in there the letter h is not contained in the word um so most of the words if not mistaken all of the words well most of the words have the letter a so we can put a letter a in there all right so the letter a is in the word and i think i know what the word is based off what i've already entered letter g i r a f and there's two of those and we're gonna intentionally run out of letters even though we're so close and i kind of noticed something that well that's working correctly so if you put the same letter in twice uh as you see here i have a and i already had a in the previous go around it does not count off against your attempts because that letter already existed so that's something to keep in mind normally if you already guess a letter that's already been guessed then you have a output that says that letter has already been guessed and that logic can be added in here too as a if condition all right so continuing on let's finish out so that we run out of attempts and just gotta make sure i type letters all right so here we have it you have ran out of attempts you lose the word was giraffe and as you can kind of figure out that's kind of what it was based off the other guesses so that wraps that up for this video this video took a little bit longer than the previous coding sessions all the coding sessions won't necessarily be quick but um yeah this one hopefully you have learned a little bit um i know there are some video tutorials on hey this can be done in 10 minutes and more than likely those 10 minute videos are because of the fact they've already practiced it a number of times and they're just regurgitating that on video to have it done in a concise manner as you can see it took a decent amount of time and a lot of walking it through to get it to do what it needs to do so while you see those tutorials that may say i can do this in five minutes i can do it in 10 minutes actuality they probably practice it two or three times beforehand and then was able to go back and record it in the five to 10 minutes that you actually ended up seeing so any questions drop those in the comments this will probably not be the last of the coding challenges that i face and uh want to share with you so any questions comments comments uh the repository information will be provided so you can look at the source code for this until next time peace out

I enjoy sharing the content that you find here. I ask that you consider purchasing an item on my Amazon Wish List or you can make a donation.

Back To Top