2 Replies - 9013 Views - Last Post: 11 May 2010 - 11:18 AM Rate Topic: -----

#1 Guest_Luc*


Reputation:

C# Hangman Hang-up

Posted 11 May 2010 - 10:42 AM

Below is the code as it's written. I am trying to incorporate the word bank to replace the commented out mystery word. I am at a complete loss as to what to do to make this code work. Also, when the word is figured out, the program doesn't end. It keeps going and still displays the fully printed out word.

Any help/suggestions?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hangman
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Welcome to Hangman! Please enter your guess: ");
            //string mysteryWord = "mysteryword";

            string[] wordBank = new string[10];
            wordBank[0] = "sheep";
            wordBank[1] = "handful";
            wordBank[2] = "ihateprogramming";
            wordBank[3] = "seventy";
            wordBank[4] = "watermelon";
            wordBank[5] = "squishy";
            wordBank[6] = "litter";
            wordBank[7] = "pineapple";
            wordBank[8] = "startrek";
            wordBank[9] = "starwars";
            Random randGen = new Random();
            string mysteryWord = randGen.ToString [wordBank [0,9]];

            char[] guess = new char[mysteryWord.Length];

            for (int p = 0; p < mysteryWord.Length; p++)
                guess[p] = '*';

            while(true)
            {
                Console.Write("Please enter your guess: ");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j=0; j < mysteryWord.Length; j++)
                {
                    if (playerGuess == mysteryWord[j])
                        guess[j] = playerGuess;
                }
                Console.WriteLine(guess);
            }

        }
    }
}



Is This A Good Question/Topic? 0

Replies To: C# Hangman Hang-up

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: C# Hangman Hang-up

Posted 11 May 2010 - 11:03 AM

You mean here?
string mysteryWord = randGen.ToString [wordBank [0,9]];
?

Generate a random number between 0 and 9, and use the generated number as the index to the array
var idx = randGen.Next(0, 9);
string mysteryWord = wordBank[idx];

Was This Post Helpful? 1
  • +
  • -

#3 Guest_Luc*


Reputation:

Re: C# Hangman Hang-up

Posted 11 May 2010 - 11:18 AM

Thank you very much. Works like a charm
Was This Post Helpful? 0

Page 1 of 1