tags:

views:

236

answers:

4

Hi , Im designing a game on visual C# the game must contain two text boxes and two bottons and a lable

in the first text bos I should enter the range of numbers the program should randomly generate. ---- and its activated by the botton.

the second text box is the place where i enter the guesses ,---- also there is a botton to read the number.

I will have a limit of ten guesses , each is assgined a color.

for example the game will start with the green and if my guess was wronge the color wil change to a darker color ( dark green ,darker ..... red etc) and if I guessed the right answer the screen will get back to the green with a lable that say you won !! or if I finished my guesse it show me Game Over !!

Now Im not that xpert in Visual C# so I find it hard where to write the code ... and how to activate actions as the color changing thing

BUT i understand that i need to use the random function and know how to set the range as you will see in the code below but Now Im stuck I don't know how to continue,,, Some one please guide me and help me.

private void button1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int range;
        range = int.Parse(textBox1.Text);
        System.Random RandNum = new System.Random();
        int Magicnumber = RandNum.Next(0, range);
        int numberofguesses = 0;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int usersguess ; 
        usersguess = int.Parse(textBox2.Text);

        if (usersguess == Magicnumber) ;
        { 
            // I dont know what to write here
        }                        
        { 
            if (usersguess != Magicnumber); 
        { 
            // I dont know what to write here
        } 
        }
    }
}

}

I know this stupid but Im not pro and I would like to learn from you guys and I belive this is the right place to ask ....

thanx in advance

A: 

Hmmm this seems like a homework type question but anyway (Since you did not mention whether this is a winforms or asp.net app i am going to assume winforms) .

For starters you need to move the declaration of

int Magicnumber = RandNum.Next(0, range);

and make it a member variable so that it can be accessed by all the functions in your form.

Secondly in your button click you can set the back color of a button to show the colors changing or you can use images , i would go with the button since you can dynamically change its color by keeping a variable that store the hex value of a color and incrementing it on every failure e.g.

Color myColor = Color.FromArgb(100,123,23,myvar); btnColor.BackColor = myColor;

By incrementing/decrementing myColor in the example above the backColor of the button will change on each failure. If the user gets the answer right then you can set the backColor to a default color of your choice.

RC1140
+1  A: 

your if statement needs a tidy up for a start:

    if (usersguess == Magicnumber) 
    { 
        // I dont know what to write here
    }                        
    else
    { 
       // I dont know what to write here
    }
Nick
You *have* to mention the semicolons after the `if`s.
Kobi
A: 

First of all, here is the better way to write your 'if' construction:

if (int.Parse(textBox2.Text) == Magicnumber)
{ 
    ...
}
else
{ 
    ...
}

If you want to count the number of user attempts to guess the number, you should store it in a variable. And this variable should be reset to zero any time you start new game.

private int count = 0;

private void button2_Click(object sender, EventArgs e)
{
    ...

    if (int.Parse(textBox2.Text) == Magicnumber)
    { 
        // User wins
        ...
    }
    else
    { 
        // Wrong answer
        count++;
        ...
    }
}

Then, if you want to change the background color of some WinForms control, use BackColor property (its value should depend of count). This example changes background color of TextEdit instance:

textEdit1.BackColor = Color.DarkGreen;

The Color here is System.Drawing.color class that may represent any color and have a lot of named values (http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx, http://msdn.microsoft.com/en-us/library/aa358802%28VS.85%29.aspx). I strictly recomed you to begin with reading some good book on C# before trying to solve any programming tasks.

dreikanter
if... semicolon... no...
Kobi
Thanks for noticing. Fixed.
dreikanter
+4  A: 

(This started as a comment, but nobody mentioned it, and I got carried away...)

Please note that adding a semicolon after the if is a mistake (this is probably why else didn't work for you, and you've made this strange if-if anti pattern).
This is the proper way to write an if statement (else is optional, by the way):

if(key == 'Q')
{
   LaunchMissiles();
}
else
{
   GivePeaceAChance();
}

Now, what you've got there is quite different:

if(key == 'Q');
{
   LaunchMissiles();
}

See that semicolon after the if? That means that if(key=='Q'); and LaunchMissiles(); are two different statements, rather then a condition. This is the same as:

if(key == 'Q') DoNothing();
LaunchMissiles();

Despite it's appearance, the curly braces won't help you here - you can group a bunch of statements in curly braces, but it has no effect on flow control (unless immediately after an if, or loop, of course).

Kobi
+1 I didn't know about the if(key == 'Q') DoNothing(); behaviour - I just assumed it wouldn't compile!
Nick
@Nick it does compile, and gives a small message: **Possible Mistaken empty statement**. However, it will *not* compile if you have an `else` clause after the statement: **Invalid expression term 'else'**
Kobi