Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The problem for this week is: This is your final modification to the project. Mo

ID: 3771775 • Letter: T

Question

The problem for this week is: This is your final modification to the project. Modify the design and program of the program so that the word is stored as an array. You can use code such as the following: char[] word = "happy".ToCharArray(); Where "happy" is replaced with your name. Add a for loop to display each letter in the word character array. If it has been guessed correctly, display the correct letter, if not display the special character. It's C# and I have the following code:

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

namespace hangman_week_6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the hangman game! ");
Char guessedletter1 = 'h';
Char guessedletter2 = 'a';
Char guessedletter3 = 'n';
Char guessedletter4 = 's';
Char guessedletter5 = 'e';
Char[] guessed = new char[26];
int guessedIndex = 5;
Char Input;
int InvalidCount;
bool IsGuessed = false;
InvalidCount = 0;
InitializeGuessedArray(guessed, guessedIndex);
for (int i = 0; i < 10; i++)
{
Input = GetInput();
if (!CheckInput(guessed, Input, guessedletter1, guessedletter2, guessedletter3, guessedletter4, guessedletter5) == true)
{
InvalidCount = InvalidCount + 1;
}
DisplayGuessedArray(guessed, guessedIndex);
if (CheckifGuessed(guessed, guessedIndex))
{
IsGuessed = true;
break;
}
}
DisplayFinalMessage(IsGuessed, InvalidCount);
Console.Write("Press any key to continue...");
Console.ReadKey();
}
private static void DisplayFinalMessage(bool IsGuessed, int InvalidCount)
{
Console.WriteLine("");
if (IsGuessed)
Console.WriteLine("Congratulations! You have figured out the word! Your score is: " + InvalidCount.ToString());
else
Console.WriteLine("Your score is: " + InvalidCount.ToString());
}

private static bool CheckifGuessed(char[] guessed, int guessedIndex)
{
bool IsCompleted = true;
for (int i = 0; i <= guessedIndex - 1; i++)
{
if (guessed[i] == '*')
{
IsCompleted = false;
}
}
return IsCompleted;
}
private static void DisplayGuessedArray(char[] guessed, int guessedIndex)
{
for (int i = 0; i <= guessedIndex - 1; i++)
{
Console.Write(guessed[i]);
}
Console.WriteLine("");
Console.WriteLine("");
}
private static bool CheckInput(char[] guessed, char Input, char guessedletter1, char guessedletter2, char guessedletter3, char guessedletter4, char guessedletter5)
{
bool IsValid = false;
if (Input == guessedletter1)
{
guessed[0] = Input;
IsValid = true;
}
if (Input == guessedletter2)
{
guessed[1] = Input;
IsValid = true;
}

if (Input == guessedletter3)
{
guessed[2] = Input;
IsValid = true;
}

if (Input == guessedletter4)
{
guessed[3] = Input;
IsValid = true;
}

if (Input == guessedletter5)
{
guessed[4] = Input;
IsValid = true;
}
if (IsValid == false)
{
Console.WriteLine("You guessed incorrectly!");
return false;
}
else
{
Console.WriteLine("You guessed a letter correctly!");
return true;
}
}
private static char GetInput()
{
Console.Write("Enter a letter please: ");
ConsoleKeyInfo ckInfo = Console.ReadKey();
Console.WriteLine("");
return ckInfo.KeyChar;
}
private static void InitializeGuessedArray(char[] guessed, int guessedIndex)
{
for (int i = 0; i <= guessedIndex - 1; i++)
{
guessed[i] = '*';
}
}
}
}



Explanation / Answer

Complete Program:

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

namespace hangman_week_6
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the hangman game! ");

            char[] word = "hanse".ToCharArray();

            Console.WriteLine("Letters in the word character array of correct letters:");
            for (int i = 0; i < word.Length; i++)
                Console.WriteLine(word[i]);
            Console.WriteLine();

            Char[] guessed = new char[26];
            int guessedIndex = 5;
            Char Input;
            int InvalidCount;
            bool IsGuessed = false;
            InvalidCount = 0;
            InitializeGuessedArray(guessed, guessedIndex);
            for (int i = 0; i < 10; i++)
            {
                Input = GetInput();
               
                if (!CheckInput(guessed, Input, word) == true)
                {
                    InvalidCount = InvalidCount + 1;
                }
                DisplayGuessedArray(guessed, guessedIndex);
                if (CheckifGuessed(guessed, guessedIndex))
                {
                    IsGuessed = true;
                    break;
                }
            }
            DisplayFinalMessage(IsGuessed, InvalidCount);
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }

        private static void DisplayFinalMessage(bool IsGuessed, int InvalidCount)
        {
            Console.WriteLine("");
            if (IsGuessed)
                Console.WriteLine("Congratulations! You have figured out the word! Your score is: " + InvalidCount.ToString());
            else
                Console.WriteLine("Your score is: " + InvalidCount.ToString());
        }

        private static bool CheckifGuessed(char[] guessed, int guessedIndex)
        {
            bool IsCompleted = true;
            for (int i = 0; i <= guessedIndex - 1; i++)
            {
                if (guessed[i] == '*')
                {
                    IsCompleted = false;
                }
            }
            return IsCompleted;
        }

        private static void DisplayGuessedArray(char[] guessed, int guessedIndex)
        {
            for (int i = 0; i <= guessedIndex - 1; i++)
            {
                Console.Write(guessed[i]);
            }
            Console.WriteLine("");
            Console.WriteLine("");
        }
      
        private static bool CheckInput(char[] guessed, char Input, char[] word)
        {
            bool IsValid = false;

            for(int i = 0; i < word.Length; i++)
            {
                if (Input == word[i])
                {
                    guessed[i] = Input;
                    IsValid = true;                   
                }
            }            

            if (IsValid == false)
            {
                Console.WriteLine("You guessed incorrectly!");
                return false;
            }
            else
            {
                Console.WriteLine("You guessed a letter correctly!");
                return true;
            }
        }

        private static char GetInput()
        {
            Console.Write("Enter a letter please: ");
            ConsoleKeyInfo ckInfo = Console.ReadKey();
            Console.WriteLine("");
            return ckInfo.KeyChar;
        }

        private static void InitializeGuessedArray(char[] guessed, int guessedIndex)
        {
            for (int i = 0; i <= guessedIndex - 1; i++)
            {
                guessed[i] = '*';
            }
        }
    }
}

Sample Output:

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote