I am supposed to create a hangman game in c#. This is what I have so far. The st
ID: 672391 • Letter: I
Question
I am supposed to create a hangman game in c#. This is what I have so far. The start button will make the word appear in the text back as *. I am unable to make a letter replace the * in the textbox. How do I do this? Can Some one help fix any mistakes in this code?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HangMan
{
public partial class Form1 : Form
{
string wordToGuess;
string wordToGuessU;
string input;
char guess;
int incorrectG = 3;
int lettersRevealed = 0;
private bool startButtonClicked = false;
string[] wordBank = { "PinkFloyd", "DoctorWho", "Leobardo", "Triangle", "BioShock", "Destiny", "WoodStock", "Pyramid", "Wolverine", "Monkey" };
List<char> cGuess = new List<char>();
List<char> wGuess = new List<char>();
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
startButtonClicked = true;
Random random = new Random((int)DateTime.Now.Ticks);
wordToGuess = wordBank[random.Next(0, wordBank.Length)];
wordToGuessU = wordToGuess.ToUpper();
StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
for (int i = 0; i < wordToGuess.Length; i++)
displayW.Text = Convert.ToString(displayToPlayer.Append('*'));
}
private void button2_Click(object sender, EventArgs e)
{
if (startButtonClicked == true)
{
if (string.IsNullOrEmpty(insertGuess.Text))
{
MessageBox.Show("Please enter a guess.");
}
else
{
for (int c = 0; c < insertGuess.Text.Length; c++)
{
if (char.IsNumber(insertGuess.Text[c]))
{
MessageBox.Show("Enter a letter not a digit.");
}
else if (insertGuess.Text.Length > 1)
{
MessageBox.Show("1 letter at a time.");
}
else
{
input = insertGuess.Text;
guess = input[0];
if (wordToGuess.Contains(guess))
{
cGuess.Add(guess);
for (int i = 0; i < wordToGuess.Length; i++)
{
if (wordToGuessU[i] == guess)
{
displayW.Text = Convert.ToString(wordToGuess[i]);
lettersRevealed++;
}
}
if (lettersRevealed == wordToGuess.Length)
{
Form3 f3 = new Form3();
f3.ShowDialog();
Close();
}
}
else
{
wGuess.Add(guess);
incorrectG--;
MessageBox.Show(" incorrect, please guess again.");
if (incorrectG == 0)
{
Form2 f2 = new Form2();
f2.ShowDialog();
Close();
}
}
}
}
}
}
else
{
MessageBox.Show("Press Start to play.");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void displayW_TextChanged(object sender, EventArgs e)
{
}
}
}
Explanation / Answer
using System.Threading.Tasks; does not exist in the name space.
That line needs to be corrected, the rest of the code is fine and it works OK
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.