Question: Modify the design and program to allow for iteration. Increase the number of guesses to 10 to solve the word. Display the word to the
Modify the design and program to allow for iteration. Increase the number of guesses to 10 to solve the word. Display the word to the user with each letter as a special character such as ********. Create an array of correct letters guessed such as: char[] guessed = new char[26];
You will need counter also to keep track of how many letters are in the guessed array. You do not need to keep track of incorrectly guessed letters.
CODE IS BELOW:
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace CourseProjectWeek7 { // begin CourseProjectWeek7 class HangmanGame { // begin class HangmanGame
static void Main(string[] args) { // begin Main() // Variable Declaration const string WELCOME = "Welcome to the Course Project: Hangman Game!"; const string RULE = "Please enter ONE letter at a time, until youve guessed the correct word."; const string SCORE = "Your score is tallied by the number of incorrect guesses."; const string SCORE2 = "Therefore, the lower your score the better!"; const string SIDE_NOTE = "Side Note: case doesnt matter T and t are the same";
string userInput = ""; char userGuess; int numLives = 10; // Player gets 10 tries bool win = false; int lettersRevealed = 0; int tallyScore = numLives; // Number of incorrect guesses
// Heading Console.WriteLine("**************************************************"); Console.WriteLine(WELCOME); Console.WriteLine(RULE); Console.WriteLine(SCORE); Console.WriteLine(SCORE2); Console.WriteLine(SIDE_NOTE);
Random randomizer = new Random((int)DateTime.Now.Ticks); // Randomizer for puzzle words
string[] puzzleWords = { "Natsu", "Happy", "Lucy", "Gray", "Erza", "Wendy" }; // Fairy Tail puzzle names/words
string guessMe = puzzleWords[(randomizer.Next(0, puzzleWords.Length))]; string guessMeUppercase = guessMe.ToUpper();
List
// Game Start Console.WriteLine("**************************************************"); Console.WriteLine("Let's begin!"); Console.WriteLine("**************************************************");
// Display randomized puzzle word to be guessed StringBuilder displayToPlayer = new StringBuilder(guessMe.Length); for (int i = 0; i < guessMe.Length; i++) displayToPlayer.Append('*');
// Begin userInput analysis while (!win && numLives > 0) { // begin while Console.WriteLine("Please enter a letter."); userInput = Console.ReadLine().ToUpper(); userGuess = userInput[0];
if (correctGuesses.Contains(userGuess)) { // begin if Console.WriteLine("Whoops! You've already tried '{0}', and it was correct!", userGuess); continue; } // end if else if (incorrectGuesses.Contains(userGuess)) { // begin else if Console.WriteLine("Whoops! You've already tried '{0}', and it was incorrect!", userGuess); continue; } // end else if if (guessMeUppercase.Contains(userGuess)) { // begin if correctGuesses.Add(userGuess); for (int i = 0; i < guessMe.Length; i++) { // begin for loop - incrementation if (guessMeUppercase[i] == userGuess) { // begin if displayToPlayer[i] = guessMe[i]; lettersRevealed++; } // end if } // end for loop - incrementation } // end if if (lettersRevealed == guessMe.Length) { // begin if win = true; } // end if else { // begin else incorrectGuesses.Add(userGuess); Console.WriteLine("Aww, sorry! There's no '{0}' in it! Try again!", userGuess); numLives--; } // end else Console.WriteLine(displayToPlayer.ToString()); } // end while
if (win) { // begin if Console.WriteLine("Yay! Congratulations, you won!"); Console.WriteLine("Score: " + tallyScore); } // end if else { // begin else Console.WriteLine("Aww, you lost! It was '{0}'", guessMe); } // end else
// Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } // end Main() } // end class HangmanGame } // end CourseProjectWeek7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
