Samet Kilictas’s Blog

Tag: Random Function

Tutorial #2 – Basic Guess Game in C#

by Samet Kilictas on Nov.10, 2008, under C#, Programming

Here is my new tutorial for you about C#. In this tutorial i am going to make a little game about guessing result of some number summation, multiplation, division and addition. And i am going to focus on handling the enter keypress event for textfield.

Here as you see from the screenshot of the main window a user should guess the result of this operation. Textbox1 equals the random number “96″, textbox2 equals the random number “73″ and there is an randomly operator which is “-” . User should guess this result and should write down the guess into the textbox3 field. When user press enter button textbox3 handles this event.

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) //Char 13 handles the enter key
{
Console.WriteLine(total);  //Here i can check the result from console for testing purposes
if (total.ToString() == textBox3.Text)
{
StringBuilder dogru_str = new StringBuilder();
dogru_str.Append(textBox1.Text);
dogru_str.Append(" " + label1.Text + " ");
dogru_str.Append(textBox2.Text + " = ");
dogru_str.Append(total + "\n");
dogru_str.Append("Your answer was correct...");
MessageBox.Show(dogru_str.ToString(), "Result", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

(continue reading…)

VN:F [1.0.6_327]
Rating: 0.0/10 (0 votes cast)
Leave a Comment :, , , , , more...

Tutorial #1 – Random Function in C#

by Samet Kilictas on Nov.09, 2008, under C#, Programming

Since there are some other ways as well. Here is my little function which can generate a random integer

private int RandomNumber(int min, int max)
{
Random random = new Random();
int rand = random.Next(min, max);
return rand;
}

I need to convert this interger number to a string. And this function takes to parameters minimum & maximum. That helps you to determine the interval of this random numbers.

//Determine about operator
String[] operators = { "+", "-", "/", "*" };
int rand3 = random.Next(1, 5);
//Console.WriteLine(rand3.ToString());
switch (rand3.ToString())
{
case "1":
label1.Text = "+";
total = rand1 + rand2;
break;
case "2":
label1.Text = "-";
total = rand1 - rand2;
break;
case "3":
label1.Text = "/";
total = rand1 / rand2;
break;
case "4":
label1.Text = "*";
total = rand1 * rand2;
break;
default:
label1.Text = "Error";
break;
}

(continue reading…)

VN:F [1.0.6_327]
Rating: 0.0/10 (0 votes cast)
Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...