C# Since the way the program is currently written requires one player to enter a
ID: 3739822 • Letter: C
Question
C#
Since the way the program is currently written requires one player to enter a word and another to play, change the program so that one player may play (without cheating!). In order to do this, add an array to the program and allow the user to enter 25 words that will populate the array. Then add a random number generator to select an index that will then be used to randomly pick one of the word from the word list array to use as the original word to guess. Note: In Unit 3, we will be modifying this program one more time to populate the array from a word list file instead of user input.)
•Add accumulators to keep track of total number of correct guesses, total number incorrect guesses and total guesses. Display these totals at the end of the game.
//Simulates the Hangman Game: Part 2 - written in C#
//Original - allows only 5 letter words
//Part 2 modifications:
// - allows any length of word
// - keeps track of letters already used
// - notifies user that letter has already been used before asking for another letter
// - repeat letter not counted in incorrect guesses
// - notifies user of how many guesses they have left when they guess incorrectly
using System;
public class Hangman
{
public static void Main()
{
//declare variables
String origWord = "";
String letter = "";
bool dashReplaced = false;
bool gameOver = false;
int numIncorrect = 0;
int guessesLeft = 10;
String guessWord = "";
int numChars = 0;
String usedLetters = "";
int usedSub = 0;
//get the original word from player1
Console.WriteLine("Enter the original word: ");
origWord = Console.ReadLine();
numChars = origWord.Length; //determine number of characters in original word
//convert the original word to uppercase
origWord = origWord.ToUpper();
//create a character array called updatedWord with same number
//of characters as original word
char[] updatedWord = new Char[numChars];
//initialize all elements in the updatedWord character array to dashes
for (int i = 0; i < numChars; i++)
{
guessWord += "-";
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 the used letters
Console.Write("Used letters: ");
//append current letter to the usedLetter string followed by a blank space
for (usedSub = 0; usedSub < usedLetters.Length; usedSub++)
{
Console.Write(usedLetters.Substring(usedSub, 1) + " ");
}//end for
Console.WriteLine();
//print out dashes for letters in word
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();
//determine whether the letter has already been enterd
usedSub = 0;
while (usedSub < usedLetters.Length && usedLetters.Substring(usedSub, 1) != letter)
{
usedSub++;
}//end while
if (usedSub < usedLetters.Length)
{
Console.WriteLine("You already guessed letter " + letter);
}
else
{
//append letter to the usedLetters string
usedLetters += letter;
/*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 < numChars; x++)
{
if (origWord.Substring(x, 1) == letter)
{
//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 variables
numIncorrect++;
guessesLeft--;
//display number of guesses left
Console.WriteLine("You have " + guessesLeft + " guesses left.");
// 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 if
} //end while
Console.ReadLine(); //pauses system until any key pressed
}//end main
}//end class
Explanation / Answer
I have altered the C# code based on the requirement given in the question
Added the below functionalities to the code
Below is the code section which i have added considering the above points
//get the 25 original words from player1
for (var i = 0; i < 25; i++)
{
Console.Clear();
Console.WriteLine("Enter "+ wordCount + " Original words");
originalWords[i] = Console.ReadLine();
wordCount--;
}
//get the original word from player1
//Console.WriteLine("Enter the original word: ");
//origWord = Console.ReadLine();
Random randomWord = new Random();
int rndWord = randomWord.Next(0, 24);
origWord = originalWords[rndWord];
The entire C# code is given below
**********************C# CODE*********************************
using System;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
//declare variables
String origWord = "";
String letter = "";
bool dashReplaced = false;
bool gameOver = false;
int numIncorrect = 0;
int guessesLeft = 10;
String guessWord = "";
int numChars = 0;
String usedLetters = "";
int usedSub = 0;
int wordCount = 25;
string[] originalWords = new string[25];
//get the 25 original words from player1
for (var i = 0; i < 25; i++)
{
Console.Clear();
Console.WriteLine("Enter "+ wordCount + " Original words");
originalWords[i] = Console.ReadLine();
wordCount--;
}
//get the original word from player1
//Console.WriteLine("Enter the original word: ");
//origWord = Console.ReadLine();
Random randomWord = new Random();
int rndWord = randomWord.Next(0, 24);
origWord = originalWords[rndWord];
numChars = origWord.Length; //determine number of characters in original word
//convert the original word to uppercase
origWord = origWord.ToUpper();
//create a character array called updatedWord with same number
//of characters as original word
char[] updatedWord = new Char[numChars];
//initialize all elements in the updatedWord character array to dashes
for (int i = 0; i < numChars; i++)
{
guessWord += "-";
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 the used letters
Console.Write("Used letters: ");
//append current letter to the usedLetter string followed by a blank space
for (usedSub = 0; usedSub < usedLetters.Length; usedSub++)
{
Console.Write(usedLetters.Substring(usedSub, 1) + " ");
}//end for
Console.WriteLine();
//print out dashes for letters in word
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();
//determine whether the letter has already been enterd
usedSub = 0;
while (usedSub < usedLetters.Length && usedLetters.Substring(usedSub, 1) != letter)
{
usedSub++;
}//end while
if (usedSub < usedLetters.Length)
{
Console.WriteLine("You already guessed letter " + letter);
}
else
{
//append letter to the usedLetters string
usedLetters += letter;
/*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 < numChars; x++)
{
if (origWord.Substring(x, 1) == letter)
{
//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 variables
numIncorrect++;
guessesLeft--;
//display number of guesses left
Console.WriteLine("You have " + guessesLeft + " guesses left.");
// 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 if
} //end while
Console.ReadLine(); //pauses system until any key pressed
}
}
}
************************END******************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.