Any recommendations on how to use my Radio Yes Button in the Group Box called Ne
ID: 3600131 • Letter: A
Question
Any recommendations on how to use my Radio Yes Button in the Group Box called New game to reinitialize the Switch Statement to start again. See code below;
namespace HangmanAssignment
{
public partial class HangMan : Form
{
public HangMan()
{
InitializeComponent();
}
int count = 1;//create variable scope to be assessible in other controls
int number = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox_TextChanged(object sender, EventArgs e)
{
}
private void CheckBtn_Click(object sender, EventArgs e)
{
textBox.Focus();//focus on text button
string input; //string input for user entry in text box
if (number ==0)
{
Random rand = new Random();//create random number 1 -100
number = rand.Next(1,100);
}
try
{
input = textBox.Text;
if(number != int.Parse(input))//when random is selected, if it is not correct change from int to string
{ //in text box, if it is greater than user input say the number
switch(count) //display hangman pics when incorrect input are inserted.
{
case 1:
if (number > int.Parse(input))
{
displayLabel.Text = ("Should be greater than " + int.Parse(input));//pass int as string in text box
textBox.Focus();
textBox.Text = "";
hangman1.Visible = true;//if wrong number is inputted show hangman
}
else
{
displayLabel.Text = ("Should be less than " + int.Parse(input));//pass int as string in text box
textBox.Focus();
textBox.Text = "";
hangman1.Visible = true;//if wrong number is inputted show hangman
}
count = count + 1;
break;
case 2:
if (number > int.Parse(input))
{
displayLabel.Text = ("Should be greater than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman1.Visible = false;
hangman2.Visible = true;//if wrong number is inputted show hangman, turn off first image
}
else
{
displayLabel.Text = ("Should be less than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman1.Visible = false;
hangman2.Visible = true;
}
count = count + 1;
break;
case 3:
if (number > int.Parse(input))
{
displayLabel.Text = ("Should be greater than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman2.Visible = false;
hangman3.Visible = true;//if wrong number is inputted show hangman, turn off second image
}
else
{
displayLabel.Text = ("Should be less than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman2.Visible = false;
hangman3.Visible = true;
}
count = count + 1;
break;
case 4:
if (number > int.Parse(input))
{
displayLabel.Text = ("Should be greater than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman3.Visible = false;
hangman4.Visible = true;//if wrong number is inputted show hangman, turn off third image
}
else
{
displayLabel.Text = ("Should be less than " + int.Parse(input));
textBox.Focus();
textBox.Text = "";
hangman3.Visible = false;
hangman4.Visible = true;
}
count = count + 1;
break;
case 5:
if (number > int.Parse(input))
{
displayLabel.Text = ("Should be greater than " + int.Parse(input));
hangman4.Visible = false;
hangman5.Visible = true;
textBox.Focus();
textBox.Text = "";
}
else
{
displayLabel.Text = ("Should be less than " + int.Parse(input));
hangman4.Visible = false;
hangman5.Visible = true;
textBox.Focus();
textBox.Text = "";
}
count = count + 1;
break;
case 6:
if (number > int.Parse(input))
{
displayLabel.Text = ("You lose. it was " + number.ToString());
hangman5.Visible = false;
hangmanDead.Visible = true;
textBox.Focus();
textBox.Text = "";
NewGame.Visible = true;
}
count = count + 1;
break;
default:
break;
}
}
else
{
displayLabel.Text = ("Great, You got it ");//if correct select display new game group box, make hangman yay visible
hangman1.Visible = false;
hangman2.Visible = false;
hangman3.Visible = false;
hangman4.Visible = false;
hangman5.Visible = false;
hangmanDead.Visible = false;
hangmanYay.Visible = true;
NewGame.Visible = true;
textBox.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void NoBtn_CheckedChanged(object sender, EventArgs e)
{
this.Close();//if no is selected, game will close.
}
private void YesBtn_CheckedChanged(object sender, EventArgs e)
{
if (YesBtn.Checked)
{
}
}
}
}
Explanation / Answer
Providing snippet with comments and suggestions:
private void YesBtn_CheckedChanged(object sender, EventArgs e)
{
if (YesBtn.Checked)
{
//If you want, then you can reset the global count variable
//count=1;
//call the CheckBtn_Click click handler here
CheckBtn_Click(sender, e);
//in this way you can go through the switch() statement again
}
}
Suggestion for the CheckBtn_Click() event handler:
//Remove this if block without having any else block
//What you want to achieve is assign new random number every time.
if (number == 0)
{
Random rand = new Random();//create random number 1 -100
number = rand.Next(1, 100);
}
//Change it to
Random rand = new Random();
number = rand.Next(1, 100);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.