f 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
Microfost Visual C# 7th edition
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
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)
{
Trending now
This is a popular solution!
Step by step
Solved in 2 steps