1. Modify the code so that the guess word can be any length (utilize the length
ID: 3739559 • Letter: 1
Question
1. Modify the code so that the guess word can be any length (utilize the length method for this).
2. Add code to display the number of guesses left.
3. Add code to determine if the user has already used a letter and prompt user when they have already guessed the letter. Do not include the letter in the number of incorrect guesses.
4. Add code to display the letters the user has already guessed. Display the information immediately before prompting the user to enter another letter.
//Simulates the Hangman Game: Part 1 - CS Version before modifications
using System;
public class Hangman
{
public static void Main()
{
//declare variables
String origWord = "";
String letter = "";
bool dashReplaced = false;
bool gameOver = false;
int numIncorrect = 0;
String guessWord = "-----";
//get the original word from player1
do
{
Console.WriteLine("Enter the original word: ");
origWord = Console.ReadLine();
} while (origWord.Length != 5);
//convert the original word to uppercase
origWord = origWord.ToUpper();
//create a character array called updated word with same number of characters as original word
char[] updatedWord = new Char[5];
//initialize all elements in the updatedWord character array to dashes
for (int i = 0; i < 5; i++)
{
updatedWord[i] = '-';
}//end for
//clear the window
Console.Clear();
//begin game
Console.WriteLine("WORD GUESS GAME");
//allow player 2 to guess a letter; the game is over when either the word
//is guessed or player 2 makes 10 incorrect guesses
while (gameOver == false)
{
dashReplaced = false;
//display guessWord with dashes
Console.WriteLine("Guess this word: " + guessWord);
//get a letter from player 2, then convert the letter to uppercase
Console.WriteLine("Enter a letter: ");
letter = Console.ReadLine();
letter = letter.ToUpper();
/*search for the guessed letter in the original word
*if player correctly guessed a letter, replace dash in character array with
*guessed letter
*Repeat until all instances of the guessed letter are placed in entire
*character array, then assign character array to guessWord string variable
*/
for (int x = 0; x < 5; x++)
{
if (origWord.Substring(x, 1) == letter) //letter found in original word
{
//replace the appropriate dash with guessed letter
//in the appropriate location
updatedWord[x] = Convert.ToChar(letter);
//indicate that a replacement was made
dashReplaced = true;
}//end if
}//end for
//updatedWord character array with replaced letters assigned to string guessWord
guessWord = new String(updatedWord);
//determine whether a replacement was made
if (dashReplaced == true)
{
//if the word does not contain any dashes, then the user guessed
//the word, so the game is over
if (guessWord.IndexOf('-') == -1) //check to see if any dashes remain
{
gameOver = true;
Console.WriteLine("Yes, the word is " + guessWord);
Console.WriteLine("Great guessing! You win!");
}//end if
}
else //processed when no dash was replaced
{
//update the counter variable
numIncorrect++;
// determine whether player made 10 incorrect guesses
if (numIncorrect == 10)
{
//the game is over
gameOver = true;
Console.WriteLine("Sorry, you lose. You made too many incorrect guesses.");
Console.WriteLine("The word is " + origWord);
}//end if
}//end if
} //end while
Console.ReadLine(); //pauses system until any key pressed
}//end main
}//end class
Explanation / Answer
static void Main(string[] args)
{
//declare variables
String origWord = "";
String letter = "";
bool dashReplaced = false;
bool gameOver = false;
int numIncorrect = 0;
String guessWord = "-----";
char []enteredChars=new char[50];
int l = 0;
int c=0;
string w="";
//get the original word from player1
Console.WriteLine("Enter the original word: ");
origWord = Console.ReadLine();
l = origWord.Length;
//convert the original word to uppercase
origWord = origWord.ToUpper();
//create a character array called updated word with same number of characters as original word
char[] updatedWord = new Char[l];
//initialize all elements in the updatedWord character array to dashes
for (int i = 0; i < l; i++)
{
updatedWord[i] = '-';
}//end for
//clear the window
Console.Clear();
//begin game
Console.WriteLine("WORD GUESS GAME");
//allow player 2 to guess a letter; the game is over when either the word
//is guessed or player 2 makes 10 incorrect guesses
while (gameOver == false)
{
dashReplaced = false;
//display guessWord with dashes
Console.WriteLine("Guess this word: " + guessWord);
// TO DISPLAY NUMBER OF INCORRECT GUESSES
Console.WriteLine("Remaining Guesses is " + (10 - numIncorrect));
//DISPLAYS THE USER ALREADY ENTERED CHARACTERS
Console.WriteLine("You have Already entered Characters "+ w);
Console.WriteLine("Enter a letter: ");
letter = Console.ReadLine();
//get a letter from player 2, then convert the letter to uppercase
letter = letter.ToUpper();
enteredChars[c++] = Convert.ToChar(letter);
w = new string(enteredChars);
/*search for the guessed letter in the original word
*if player correctly guessed a letter, replace dash in character array with
*guessed letter
*Repeat until all instances of the guessed letter are placed in entire
*character array, then assign character array to guessWord string variable
*/
for (int x = 0; x < l; x++)
{
if (origWord.Substring(x, 1) == letter) //letter found in original word
{
//replace the appropriate dash with guessed letter
//in the appropriate location
updatedWord[x] = Convert.ToChar(letter);
//indicate that a replacement was made
dashReplaced = true;
}//end if
}//end for
//updatedWord character array with replaced letters assigned to string guessWord
guessWord = new String(updatedWord);
//determine whether a replacement was made
if (dashReplaced == true)
{
//if the word does not contain any dashes, then the user guessed
//the word, so the game is over
if (guessWord.IndexOf('-') == -1) //check to see if any dashes remain
{
gameOver = true;
Console.WriteLine("Yes, the word is " + guessWord);
Console.WriteLine("Great guessing! You win!");
}//end if
}
else //processed when no dash was replaced
{
//update the counter variable only if it is not entered by user
numIncorrect++;
// determine whether player made 10 incorrect guesses
if (numIncorrect == 10)
{
//the game is over
gameOver = true;
Console.WriteLine("Sorry, you lose. You made too many incorrect guesses.");
Console.WriteLine("The word is " + origWord);
}//end if
}//end if
} //end while
Console.ReadLine(); //pauses system until any key pressed
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.