Lucian Negru

Lucian Negru

  • NA
  • 3
  • 1.3k

Guess the number C#

Apr 10 2014 7:28 AM
Hi. I am pretty new to C# and trying to get this game to work. I got a textbox where I type the number that I have in mind and the computer must guess it using bisection algorithm but I don't understand why the loop won't work since  the condition is true. Here's my code. The biggeest and the minimum have their values set to "0" and "100".
 
private void btnGuess_Click(object sender, EventArgs e)
{
bool guess = true;
Convert.ToInt32(txtNumber.Text);
nrToGuess = int.Parse(txtNumber.Text);
DialogResult dialogResult;
 
do
{
average = minimum + biggest / 2;
dialogResult= MessageBox.Show("Is your number" + " " + average+ " ? ", "Question", MessageBoxButtons.YesNo);
if (dialogResult== DialogResult.No && average > nrToGuess)
{
biggest = average;
average = biggest + minimum / 2;
}
else if (dialogResult == DialogResult.No && average < nrToGuess)
{
minimum = average;
average = biggest + minimum / 2;
}
else if (dialogResult == DialogResult.Yes)
{
dialogResult=MessageBox.Show("Congratulations! You guessed the number!");
break;
}
else
{
Application.Exit();
}
}
while (guess);
 
I would really appreciate any help given. Thank you very much! 

Answers (2)