none
Need help with creating hangman (Syntax help, not cheating) RRS feed

  • Question

  • So my current assignment is this:

    "Create a game similar to Hangman in which a player guesses letters to try to replicate a hidden word. Store at least eight words in an array, and randomly select one to be the hidden word. (The statements needed to generate a random number are shown in the Exercises in the Decision Making and Looping chapters.) Initially, display the hidden word using asterisks to represent each letter. Allow the user to guess letters to replace the asterisks in the hidden word until the user completes the entire word. If the user guesses a letter that is not in the hidden word, display an appropriate message. If the user guesses a letter that appears multiple times in the hidden word, make sure that each correct letter is placed. Figure 6-27 shows typical games in progress in a consolebased application and in a GUI application. Save the file as GuessAWord.cs. Hint: If you create the GUI version of the game, you might want to include a Start button that selects the random word and performs other start-up tasks before you reveal the game interface. After the start-up tasks are complete, you can remove the Start button from the form."

    I have been searching the internet so far for a way to even begin to formulate a plan.....

    My code is nothing worth looking at yet, but I wanted to ask first (to get this thread started):

    How can I replace each letter in a word with an asterisk?

    Thursday, November 1, 2018 11:18 PM

All replies

  • There are probably a gajillion different ways to go about this, but this is they way I would probably do it (just thinking out loud).

    Start with a word for the user to guess (from the random list or whatever). Then create a string to display, which has the same number of letters and is set to all asterisks. Display that string. Get the user's guess. Search the word to guess for occurrences of the given letter. If you find the letter in the word, replace the asterisk in the display word at that location with that letter. Display the string, get the user's guess etc. , repeating until there are no more asterisks in the display word.

    Thursday, November 1, 2018 11:56 PM
  • As I said above:

    How would I go about switching the characters with asterisks?

    Friday, November 2, 2018 12:58 AM
  • Here's one way.

             string aster = "**********";
             char[] asterArray = aster.ToCharArray();
             asterArray[5] = 'X';
             aster = new string(asterArray);
    
             // At this point aster will be "*****X****".
    

    Friday, November 2, 2018 1:15 AM
  • Manually inputting the # of asterisks required is easy, what I mean is:

    Can you create a way that will automatically replace the word with asterisks, depending on the # of letters the word has.

    Friday, November 2, 2018 1:25 AM

  • Can you create a way that will automatically replace the word with asterisks, depending on the # of letters the word 

    String.Replace Method
    https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.7.2

    - Wayne

    Friday, November 2, 2018 1:38 AM
  • Can you create a way that will automatically replace the word with asterisks, depending on the # of letters the word has.

    string str = "User Password";
    string stars = "";
    stars = stars.PadLeft(str.Length, '*');
    

    - Wayne

    Friday, November 2, 2018 1:55 AM
  • Check this example too:

       string word = "someword";

       string asterisks = new string( '*', word.Length );

     


    Friday, November 2, 2018 5:47 AM