Sign in to follow this  
NotSlyPee

C# Stop Counting Points

Recommended Posts

Hi, i'm doing a simple game with C#, but i have a problem. To make things clear, y - points , x - time. The problem is that if answer is correct you can keep pressing check answer and it will give you points. How could i avoid that? Here's the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MathGame
{
    public partial class Form1 : Form
    {
        Random randomNo = new Random();
        int no1;
        int no2;
        string problemType = "+";
        Random randomNumber = new Random();

        public Form1()
        {
            InitializeComponent();
            Answer.KeyDown += new KeyEventHandler(Answer_KeyDown);
            Answer.KeyDown += new KeyEventHandler(NextQuestion_KeyDown);
        }
        private void atsitiktinisRadimas()
        {
        darkart:
            int tempNumber = randomNumber.Next(4);
            if (tempNumber == 0) { goto darkart; }

            if (tempNumber == 1)
            {
                radioButton1.Checked = true;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
            }


            if (tempNumber == 2)
            {
                radioButton1.Checked = false;
                radioButton2.Checked = true;
                radioButton3.Checked = false;
            }

            if (tempNumber == 3)
            {
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = true;
            }

        }

        private void StartGame_Click(object sender, EventArgs e)
        {
            Clock = new Timer();
            Clock.Interval = 1000;
            Clock.Start();
            Clock.Tick += new EventHandler(Timer_Tick);
            StartGame.Visible = false;
            CheckAnswer.Visible = true;
            Question.Visible = true;
            Answer.Visible = true;
            NextQuestion.Visible = true;
            Feedback.Text = "";
            Feedback.Visible = true;
            label1.Visible = true;
            Gen_Question();
        }

        private void Gen_Question()
        {
            no1 = randomNo.Next(100);
            no2 = randomNo.Next(100);

            if (radioButton1.Checked)
            {
                problemType = "+";
            }
            else if (radioButton2.Checked)
            {
                problemType = "-";
            }
            else if (radioButton3.Checked)
            {
                problemType = "*";
            }

            Question.Text = no1 + problemType + no2;
        }

        int y = 0;

        private void CheckAnswer_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (problemType == "+")
            {
                result = no1 + no2;
            }
            else if (problemType == "-")
            {
                result = no1 - no2;
            }
            else if (problemType == "*")
            {
                result = no1 * no2;
            }

            if (Answer.Text == result.ToString())
            {
                Feedback.Text = "Correct";
            }
            else
            {
                Feedback.Text = "Incorrect";
            }
                if (Feedback.Text == "Correct")
                {
                    y = y + 50;
                    if (15 > x)
                    if (x > 1)
                    {
                        y = y + 25 - 50;
                    }
            }
            this.label4.Text = "" + y;
        }

        private void NextQuestion_Click(object sender, EventArgs e)
        {
            x = 30;
            Clock.Start();
            this.label1.Text = "" + x;
            atsitiktinisRadimas();
            Gen_Question();
            Answer.Text = "";
            Feedback.Text = "";
        }
        int x = 30;

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (x == 1) { Clock.Stop(); }
            x = x - 1;
            this.label1.Text = "" + x;
            if (x == 0) { stop(); }
        }
        private void stop()
        {
            this.Question.Text = "Time's Up";
        }

        private void Answer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int result = 0;
                if (problemType == "+")
                {
                    result = no1 + no2;
                }
                else if (problemType == "-")
                {
                    result = no1 - no2;
                }
                else if (problemType == "*")
                {
                    result = no1 * no2;
                }

                if (Answer.Text == result.ToString())
                {
                    Feedback.Text = "Correct";
                }
                else
                {
                    Feedback.Text = "Incorrect";
                }
                if (Feedback.Text == "Correct")
                {
                    y = y + 50;
                    if (15 > x)
                    if (x > 1)
                    {
                        y = y + 25 - 50;
                    }
            }

            this.label4.Text = "" + y;

            }
        }

        private void NextQuestion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                x = 30;
                Clock.Start();
                atsitiktinisRadimas();
                this.label1.Text = "" + x;
                Gen_Question();
                Answer.Text = "";
                Feedback.Text = "";
            }
        }
    }
}

Share this post


Link to post
Share on other sites
Stuff that jumps out instantly (taken a little time to type so some may have already been covered):

0. Use [source] tags instead of code ones - you get a nice scrolling box.
1. You don't need to declare two instances of Random - you only need one.
2. Don't do this:
        darkart:

int tempNumber = randomNumber.Next(4);

if (tempNumber == 0) { goto darkart; }

Instead, just do int tempNumber = randomNumber.Next(1, 5);
3. Don't use a big if..else if..else if..else if block. Use a switch statement instead.
4. Use better variable names, in general. randomNumber, tempNumber tell you very little (and in fact, randomNumber is arguably misleading since it isn't a (pseudo-)random number itself; it's the generator.). Things like label4 are particularly bad. You explain - "to make things clear, y - points , x - time" - so why not call them that!
5. You're not seeding your random number generators.
6. Don't use a string to store problemType. Use an enum instead.
7. Definitely don't intersperse declarations of data members with those of the function members! It's highly confusing.
8. You don't need to initialize result to 0 in CheckAnswer_Click since you immediately overwrite this value.
9. this.label4.Text = "" + y; is semantically equivalent to this.label4.Text = y.

10. Your actual problem. Have a flag bool answerChecked or similar that you set to false every time you give a new question, and then set to true the first time the answer is checked. If it's set to true when the user clicks to check the answer, just bypass checking the answer and/or giving them points. I didn't spend much time examining the logic in your code, so this idea may need a little alteration to work for you (and, indeed, it might well be possible to refactor your code such that no flag is needed).

EDIT: Oops. Forgot to re-escape the tag when I edited.
EDIT: And again.

Share this post


Link to post
Share on other sites
Quote:
10. Your actual problem. Have a flag bool answerChecked or similar that you set to false every time you give a new question, and then set to true the first time the answer is checked. If it's set to true when the user clicks to check the answer, just bypass checking the answer and/or giving them points. I didn't spend much time examining the logic in your code, so this idea may need a little alteration to work for you (and, indeed, it might well be possible to refactor your code such that no flag is needed).
Thanks for your tips. I didn't quite understood how to do first part of code with flagging. (tip 10) I would do something like
if (GenQuestion)
bool answerChecked = false
(didn't used syntax, just showed the idea which is wrong)



Share this post


Link to post
Share on other sites
Sort of pseudo code:

// ...
bool answerChecked = true; // No point in checking answer before we've even given a question!

// ...
void GenerateQuestion()
{
answerChecked = false;

// Generate a question
}

void CheckAnswer()
{
if (answerChecked)
return; // Stop the user cheating!

// Check answers

// Give points

answerChecked = true;
}



Does that help? If not, I'll try to fit it into your code as an example.

EDIT: Fixed typo

Share this post


Link to post
Share on other sites
Ok, it's great but it doesn't work on void Answer_KeyDown for some reason. When i try to insert a number into answer field it just automatically shows 'incorrect' message while using your code on void Answer_KeyDown. What to do?

[Edited by - NotSlyPee on April 6, 2007 1:18:58 PM]

Share this post


Link to post
Share on other sites
So your Answer_Keydown looks something like this (you may have altered the structure of the code - I've highlighted the two areas I'm talking about in particular)? If you've changed the structure of the code significantly, you might be better posting your full code again (in [source] tags :-) ) and I'll make the changes with comments to explain. (Also note that I've only made the change you need to fix the multiple-point problem below - if you do post the full code as-is, I'll refactor to demonstrate)

        private void Answer_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int result = 0;
if (problemType == "+")
{
result = no1 + no2;
}
else if (problemType == "-")
{
result = no1 - no2;
}
else if (problemType == "*")
{
result = no1 * no2;
}

if (Answer.Text == result.ToString())
{
Feedback.Text = "Correct";
}
else
{
// ===========================
/* You might be letting them have multiple chances - if not
* then you want something like this.
*/

answerChecked = true; // No second chances!
Feedback.Text = "Incorrect";
// ===========================
}
if (Feedback.Text == "Correct")
{
// ===========================
if (!answerChecked)
{
y = y + 50;
if (15 > x)
if (x > 1)
{
y = y + 25 - 50;
}
answerChecked = true;
}
// ============================
}

this.label4.Text = "" + y;

}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this