Need help in getting code to work properly please help. in c# Modify the design
ID: 3665738 • Letter: N
Question
Need help in getting code to work properly please help.
in c# Modify the design and program to test if each letter the user entered is one of the letters in the word. If the letter guessed is in the word, display a message to the user that they guessed correctly, if not display a message that the user guessed incorrectly. Add a score variable that will keep track of the number of incorrect guesses. If the user guesses incorrectly, increment the score variable. example of program out put is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace week4project
{
class Program
{
static void Main(string[] args)
{
char sletter;
string secretword;
Console.WriteLine("Welcome to the hangman game !");
Console.WriteLine("*****");
Console.WriteLine("Please Enter a letter");
secretword=Console.ReadLine();
char letter1 = 'h',letter2 = 'a' ,letter3 = 'p' ,letter4 = 'p' ,letter5 = 'y';
sletter = char.Parse(secretword);
Console.WriteLine();
switch (sletter)
{
case 'h':
Console.WriteLine("Enter a letter please :");
Console.ReadLine();
break;
case 'a':
Console.WriteLine("Enter a letter please :");
Console.ReadLine();
break;
case 'p':
Console.WriteLine("Enter a letter please :");
Console.ReadLine();
break;
case 'y':
Console.WriteLine("Enter a letter please :");
Console.ReadLine();
break;
default:
Console.WriteLine("you guessed incorrectly.");
Console.ReadLine();
break;
}
}
}
}
Explanation / Answer
Here is the code i can help you with
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int correctcounter = 0;
int wrongcounter = 0;
char l1 = 'j';
char l2 = 'o';
char l3 = 'h';
char l4 = 'n';
char l5 = 's';
char[] correctletters = { 'j', 'o', 'h', 'n', 's', 'o', 'n' };
char[] wordToReveal = {'*', '*', '*', '*', '*', '*', '*'};
char[] guessedletters = new char[10];
Console.WriteLine("Welcome to the Hangman Game!");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("You have 10 tries to guess the word right.");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("The word is " + string.Concat(wordToReveal) + ".");
Console.WriteLine();
Console.WriteLine("Guessed letters: [{0}]", string.Join(",", guessedletters));
Console.WriteLine();
Console.WriteLine("Your total wrong guesses:{0}.", wrongcounter);
Console.WriteLine();
Console.WriteLine("Please enter a letter");
Console.WriteLine();
guessedletters[i] = Convert.ToChar(Console.ReadLine());
if (guessedletters[i] == l1 || guessedletters[i] == l2 || guessedletters[i] == l3 || guessedletters[i] == l4 || guessedletters[i] == l5)
{
Console.WriteLine();
Console.WriteLine("You guessed correctly!");
for (int j = 0; j < correctletters.Length; j++)
{
if (guessedletters.Contains(correctletters[j]))
{
wordToReveal[j] = correctletters[j];
}
}
correctcounter++;
}
else
{
Console.WriteLine();
Console.WriteLine("You guessed incorrectly");
wrongcounter++;
}
if (correctcounter == 5)
{
Console.WriteLine();
Console.WriteLine("You guessed the word, [{0}]. You WIN!", string.Concat(correctletters));
break;
}
if (wrongcounter == 10)
{
Console.WriteLine();
Console.WriteLine("You LOSE! The word was [{0}]. You LOSE!", string.Concat(correctletters));
break;
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Please hit enter to end the program");
Console.ReadLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.