Im coding a game in python where i have a word bank that the computer randomly draws from and then displays to the user. The user then has to guess single letters in a hangman like fashion.what i would like to happen is when the computer draws the word, before it displays it the word gets anonymized, like a password. either displaying the amount of characters in asterisk or hyphen or something and then if the guess is right it reloads the word and displays the correct character in place of the asterisk. any suggestions?
so say the computer randomly draws 'apple'. it will display ***** then user is prompted to guess, say they guess 'a' a**** prompted again 'v' incorrect! a****
I wrote up a quick hangman game in java- where it does print out asterisks, then updates them to the proper letter.. I know you don't do much java & sorry that I did a terrible job documenting- but let me know if you wanna know how I did a certain process. http://pastebin.com/mJdwj4zj
Once I know arrays, I'll be able to make a version for any length of word :D
You want to know how to turn the asterisks into the correct letter if the user guesses it? I did that by making all of the letters in the word into char variables-- I then create a string variable holding asterisks- the same length as the hidden word. then a series of if statements, which tests each letter of the word for the letter the user entered-- and for all the letters that correspond, I call forth the setCharAt method of the StringBuilder class in java- this replaces the proper asterisk with the letter that you guessed... though I'm not sure about the Python equivalent of these commands.
First of all, after you get the word, create another string and populate it with as many asterisk characters as the length of the word is. Then it depends on how do you prompt the user's guess. Do they have to click on a virtual keyboard, or do they have to type the letters by hand? I'm guessing that your approach will implement the latter input method. In that case, you should hardcode every letter (lowercase only) in an array, so that every letter of the alphabet has its keycode assigned to it. Then listen to the key press event, and when it is triggered, make sure you get the keycode of the just-pressed key. Search for this keycode (that you have received) in the hardcoded array and get the corresponding letter. Then search for this letter in the initial word, and if it exists, remember its position. Replace the asterisk at this position in the second string with the found letter. Basically, the algorithm is: Wait for the user to press a key, figure out which (if any) letter it was, search for it in the initial word and if it exists, replace the n-th asterisk in the hidden placeholder with this very letter, n being the position of the letter.
Join our real-time social learning platform and learn together with your friends!