Public Group

# C# Stop Counting Points

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

## 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();
}
{
darkart:
int tempNumber = randomNumber.Next(4);
if (tempNumber == 0) { goto darkart; }

if (tempNumber == 1)
{
}

if (tempNumber == 2)
{
}

if (tempNumber == 3)
{
}

}

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;
Question.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);

{
problemType = "+";
}
{
problemType = "-";
}
{
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;
}

{
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;
Gen_Question();
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;
}

{
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();
this.label1.Text = "" + x;
Gen_Question();
Feedback.Text = "";
}
}
}
}

##### 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 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 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 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 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 on other sites
Many thanks. I implemented your code to mine and everything works perfectly as a wanted now. :)

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 27
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001790
×