C# GUI program help Create a game similar to Hangman named GuessAWord in which a
ID: 3796368 • Letter: C
Question
C# GUI program help
Create a game similar to Hangman named GuessAWord in which a player guesses letters to try to replicate a hidden word. Store at least eight words in an array, and randomly select one to be the hidden word. Initially, display the hidden word using asterisks to represent each letter. Allow the user to guess letters to replace the asterisks in the hidden word until the user completes the entire word. If the user guesses a letter that is not in the hidden word, display an appropriate message. If the user guesses a letter that appears multiple times in the hidden word, make sure that each correct letter is placed. Create a GUI version of the game. Include a start button that selects the random word and performs other startup tasks before you reveal the game interface. After the startup tasks are complete, remove the Start button from the form.
Here is the code that I used for the same exercise in a console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string userGuessString;
int correctGuesses = 0;
char userGuess;
int count = 0;
int y, t = 0;
Random r = new Random();
string[] word = { "pen", "phone", "water", "flower", "cups", "fish", "coffee", "mouse" };
string randomWord = word[r.Next(0, word.Length)];
WriteLine(randomWord);
string[] tempRandom = new string[randomWord.Length];
string[] answerCopy = { randomWord };
WriteLine("Hidden word:");
WriteLine();
for (int x = 0; x < randomWord.Length; ++x)
{
Write("* ");
tempRandom[x] += "_";
}
WriteLine();
WriteLine();
WriteLine("Guess a letter");
do
{
userGuessString = ReadLine();
userGuess = Convert.ToChar(userGuessString);
for (y = 0; y < randomWord.Length; ++y)
{
if (userGuess == randomWord[y])
{
correctGuesses += 1;
t += correctGuesses;
break;
}
}
if (correctGuesses > 0)
{
WriteLine("You guessed right, {0} is one of the letters!", userGuess);
correctGuesses = 0;
WriteLine("Guess the next letter");
}
else
{
WriteLine("Wrong Guess!!!");
WriteLine("Guess Again");
}
count++;
if (count > randomWord.Length)
{
WriteLine("**** GAME OVER ****");
Write("Correct Answer was ");
Write(randomWord);
break;
}
else
continue;
} while (t != randomWord.Length);
if (count == t)
{
WriteLine("CONGRATS!! You got it");
Write("Word is ");
Write(randomWord);
}
else
Write("**** GAME OVER ****");
}
}
}
Explanation / Answer
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace GuessAWord { public partial class Form1 : Form { int randomNumber; Random ranNumberGenerator; string ranWord; string[] words; const int MAXWORDS = 6; public Form1() { words = new string[MAXWORDS]; ranNumberGenerator = new Random(); words[0] = "pen"; words[1] = "phone"; words[2] = "water"; words[3] = "flower"; words[4] = "fish"; words[5] = "coffee"; words[6] = "mouse"; InitializeComponent(); } private void startButton_Click(object sender, EventArgs e) { randomNumber = ranNumberGenerator.Next(0, 6); ranWord = words[randomNumber]; mysteryLabel.Text = ""; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.