Jump to content
  • Advertisement
Sign in to follow this  
duHOSSval

Need help with a C# windows program

This topic is 3801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a guessing game, where you guess a number between 1 and 100 and if the guess is too low, the screen turns pink, and if the guess is too high the screen turns blue. For some reason the random number is random every guess, and my counter is not working. Can someone put me in the right direction? thanks. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Programming_Assignment_4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void yourguess_TextChanged(object sender, EventArgs e) { } private void Guess_Click(object sender, EventArgs e) { Random r = new Random(); int answer = r.Next(0, 10); int counter = 0; int guess; bool playing = true; guess = int.Parse(yourguess.Text); while (guess < 101) { ++counter; if (guess < answer) { this.BackColor = System.Drawing.Color.Pink; this.yourguess.Clear(); //counter++; break; } else if (guess > answer) { this.BackColor = System.Drawing.Color.Blue; this.yourguess.Clear(); //counter++; break; } else if (guess == answer) { string name = ("Congradulations. You guessed " + answer + "in " + counter + " tries"); MessageBox.Show(name); //counter++; this.yourguess.Clear(); break; } } //for (int count = 0; guess < answer; count++) //{ // for (int count1 = 0; guess > answer; count1++) // { // this.BackColor = System.Drawing.Color.Blue; // this.Low.Visible = false; // this.High.Visible = true; // } // this.BackColor = System.Drawing.Color.Pink; // this.Low.Visible = true; // this.High.Visible = false; //} //if (guess < answer) //{ // this.BackColor = System.Drawing.Color.Pink; // this.Low.Visible = true; // this.High.Visible = false; //} //else if (guess > answer) //{ // this.BackColor = System.Drawing.Color.Blue; // this.Low.Visible = false; // this.High.Visible = true; //} //else //{ // string name = "Congradulations!"; // MessageBox.Show(name); //} } private void High_Click(object sender, EventArgs e) { } private void Low_Click(object sender, EventArgs e) { } } }

Share this post


Link to post
Share on other sites
Advertisement
Your question doesn't make much sense. What do you mean that "the random number is random every guess"? A guess is a guess, and has nothing to do with randomness.

Perhaps I could infer the meaning of your question from your source code if you properly formatted it with source tags, as described in the faq.

Share this post


Link to post
Share on other sites
Sorry. I mean that for every guess the random number changes, for example I typed in 4 and it was right after like 5 tries. Here is my code properly formatted. Thanks.

[/using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Programming_Assignment_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{


}

private void label1_Click(object sender, EventArgs e)
{

}

private void yourguess_TextChanged(object sender, EventArgs e)
{

}

private void Guess_Click(object sender, EventArgs e)
{
Random r = new Random();
int answer = r.Next(0, 10);
int counter = 0;
int guess;
bool playing = true;

guess = int.Parse(yourguess.Text);


while (guess < 101)
{
++counter;


if (guess < answer)
{

this.BackColor = System.Drawing.Color.Pink;
this.yourguess.Clear();
//counter++;
break;
}
else if (guess > answer)
{

this.BackColor = System.Drawing.Color.Blue;
this.yourguess.Clear();
//counter++;
break;
}
else if (guess == answer)
{
string name = ("Congradulations. You guessed " + answer + "in " + counter + " tries");
MessageBox.Show(name);

//counter++;
this.yourguess.Clear();
break;
}

}


}

private void High_Click(object sender, EventArgs e)
{

}

private void Low_Click(object sender, EventArgs e)
{

}
}
}]

Share this post


Link to post
Share on other sites
Since this is homework, I can't give you a direct answer, but we can give you really good hints...

Here is where you are randomly generating an answer:
Random r = new Random();
int answer = r.Next(0, 10);



That code appears to be executed every time the user clicks the button to submit a guess. In other words, it is performing exactly the way you wrote it.

Now, can you think of any ways you could rewrite your code to prevent it from executing that code and generating that random number every time the button is clicked?

MORE:

You are resetting the counter every time also. The way your program works right now, you are giving the user a single guess to get the correct answer.

Share this post


Link to post
Share on other sites
That would help at least superficially. Have you covered classes and class members in your schoolwork? Remember that the form itself is a class. Perhaps you could give it some data members to keep up with those things.

Share this post


Link to post
Share on other sites
You can put it in any number of places. You just have to make sure you are storing the number, rather than regenerating it every time.

I'm a bit baffled by the fact that you were given this assignment without covering member variables and other object oriented ideas.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!