![]() ![]() If the character is invalid, we show a message and skip the rest of the loop ( continue here will put us to the top of the loop again). ![]() We are only checking for single letters from a to z. For this, a Regex (declared higher) is used. We also need to validate that the given character is valid.We also need to make sure it's in lowercase as this will avoid the same character in different casings from being submitted. Now we read an input character and assign it to a value.If there aren't any (value is 0), the player won and we can break out of the loop. After the loop, we check if there are any characters left.In this process, we also count all the remaining letters (everytime _ is written, this value gets incremented, telling us how far the player is from finding the word or if he's done).If yes, we write out the letter and if not, we write the blank character ( _). We compare if the letter has been submitted (using the letters array).We loop through each letter of the choosen word.Inside the loop, the following happens:.The main game loop runs as long as the lives have not reached 0.We also store a count of how many lives are left.We initialize an empty array that will contain all the letters that have been submitted.A word is selected from the list of words (value assigned to choosenWord).Hopefully this can be useful to others as well. This was done as an example for a friend who's learning programming. This is a simple hangman game written in C# (.NET 6). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |