C# Programming: A. Write a GUI program named GuessAWordGUI for a game that lets
ID: 669615 • Letter: C
Question
C# Programming:
A. Write a GUI program named GuessAWordGUI for a game that lets a player guess letters 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 with 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 not in the hidden word, display the appropriate message. If the user guesses a letters that appears multiple times in the hidden word, make sure each correct letter is placed. Use a Start button that selects the random word and starts the game. After start-up is complete, remove the Start button from the form.
Explanation / Answer
Comments added
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 myForm : Form
{
//variables declared
int randomNumber;
Random randomSelect;
string randomWord;
string[] names;
const int wordsArraySize = 8;
public myForm()
{
//creating array of names
names = new string[wordsArraySize - 1];
randomSelect = new Random();
names[0] = "rose";
names[1] = "lilly";
names[2] = "marigold";
names[3] = "jasmine";
names[4] = "sunflower";
names[5] = "aster";
names[6] = "azalea";
names[7] = "bluebell";
InitializeComponent();
}
private void startButton_Click(object sender, EventArgs e)
{
//select random word from declared array
randomNumber = ranNumberGenerator.Next(0, wordsArraySize - 1);
randomSelect = names[randomNumber];
GuessLabel.Text = "";
for (int i = 0; i < randomSelect.Length; i++)
{
GuessLabel.Text += "*"; ///replacing word characterts with asterisk
}
resultLabel.Text = "";
guessButton.Enabled = true;
userTextbox.Select();
}
private void guessButton_Click(object sender, EventArgs e)
{
int smallwordLength = 0;
//processing entered data with our original key word
if (randomSelect.Length < userTextbox.Text.Length)
{
smallwordLength = randomSelect.Length;
}
else
{
smallwordLength = userTextbox.Text.Length;
}
//checking how many letters matched
for (int i = 0; i < smallwordLength; i++)
{
if (randomSelect[i] == userTextbox.Text[i])
{
char[] charletters = GuessLabel.Text.ToCharArray();
charletters[i] = randomSelect[i];
GuessLabel.Text = charletters.ToString();
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.