Question

This problem is from Microsoft Visual C#: An Introduction to Object Oriented Programming (7th Edition) by...

This problem is from Microsoft Visual C#: An Introduction to Object Oriented Programming (7th Edition) by Joyce Farrell.

This problem relies on the generation of a random number. You can create a random number that is at least min but less than max using the following statements:

Random ranNumberGenerator = new Random();
int randomNumber;
randomNumber = ranNumberGenerator.Next(min, max);

Create a game similar to Hangman named GuessAWord 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.

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 a typical game in progress.

Figure 6-27 Typical execution of console-based GuessAWord program

Homework Answers

Answer #2

C Sharp Program:

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

namespace GuessAWord
{
class Program
{
static void Main(string[] args)
{
//Array that holds words from file
String[] words = {"welcome", "hello", "program", "interest", "laugh", "object", "oriented", "color"};

char play = 'y';
      
       //Random class object
       Random ranNumberGenerator = new Random();
      
int i;

       //Iterate till user wants to stop   
       while(play == 'y')
{
          
           //Getting random word
           String word;
           word = words[ranNumberGenerator.Next(0, 8)];
          
           //Constructing guessing word
           String guess="";
          
           //Adding *
           for(i=0; i<word.Length; i++)
{
               guess += "*";
           }
          
           int missCnt = 0;
           char ch;
          
//Playing game
           while(!guess.Equals(word))
{
               Console.Write("\n(Guess) Enter a letter in word " + guess + " > ");
               ch = Console.ReadLine().ToCharArray()[0];
              
               bool found = false;
              
               //Checking character
               for(i=0; i<word.Length; i++)
{
                   //Comparing
                   if(word.ToCharArray()[i] == ch)
{  
                       //Constructing string
                       guess = guess.Substring(0, i) + ch.ToString() + guess.Substring(i+1);
                       found = true;
                   }
               }
              
               //If not found
               if(!found)
{
                   Console.WriteLine("\n " + ch + " is not in the word \n");
                   //Incrementing miss count
                   missCnt += 1;
               }
           }
          
           //Printing result
           Console.WriteLine("\n The word is " + word + ". You missed " + missCnt + " times \n");
          
           //Getting user option
Console.Write("\n Do you want to guess for another word? Enter y or n> ");
play = Console.ReadLine().ToCharArray()[0];
       }

Console.Read();
}
}
}

___________________________________________________________________________________________

Sample Run:

answered by: anonymous
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions