You are viewing our Forum Archives. To view or take place in current topics click here.
C# Number Guessing Game
Posted:
C# Number Guessing GamePosted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Well, I got bored and made this. Basically you just generate a number, max and minimum set by user, and keep guessing until you get it right. On each guess it will tell you if you guessed too high or too low, which is pretty obvious from the picture.
Picture:
[ Register or Signin to view external links. ]
Code:
Download: [ Register or Signin to view external links. ]
If you think its a virus, don't download it.
Picture:
[ Register or Signin to view external links. ]
Code:
using System;
using System.Windows.Forms;
namespace NumberGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int randomNumber = 0;
int attempts = 0;
private void button2_Click(object sender, EventArgs e)
{
try
{
Random r = new Random();
randomNumber = r.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text));
textBox1.Enabled = true;
label1.Text = "Guess a number.";
attempts = 0;
}
catch { MessageBox.Show("Don't be an idiot, enter a number."); }
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (int.Parse(textBox1.Text) > randomNumber) label1.Text = "Too high.";
else if (int.Parse(textBox1.Text) < randomNumber) label1.Text = "Too low.";
else
{
label1.Text = "You won.";
textBox1.Enabled = false;
label2.Text = "Attempts: 0";
textBox1.Text = String.Empty;
MessageBox.Show("You won in " + attempts +" attempts, press generate to play again.", "Winner!");
attempts = 0;
label2.Text = "Attempts: " + attempts.ToString();
return;
}
attempts++;
label2.Text = "Attempts: " + attempts.ToString();
}
catch { MessageBox.Show("Don't be an idiot, enter a number."); }
}
}
}
Download: [ Register or Signin to view external links. ]
If you think its a virus, don't download it.
#2. Posted:
Status: Offline
Joined: Aug 26, 201014Year Member
Posts: 63
Reputation Power: 2
Status: Offline
Joined: Aug 26, 201014Year Member
Posts: 63
Reputation Power: 2
awesome dude, thanks for the code as well
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Feb 01, 201212Year Member
Posts: 554
Reputation Power: 25
Thanks This helped with my project!
- 0useful
- 2not useful
#4. Posted:
Status: Offline
Joined: Sep 22, 201113Year Member
Posts: 111
Reputation Power: 4
Broken link bro, Can you fix it?<3
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
iSequL wrote Broken link bro, Can you fix it?<3
I deleted the project off my computer. Also this was posted like 9 months ago.
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: May 20, 201014Year Member
Posts: 3,135
Reputation Power: 136
Status: Offline
Joined: May 20, 201014Year Member
Posts: 3,135
Reputation Power: 136
Shadeflame wrote Thanks This helped with my project!
Gravediggers gonna dig... :facepalm:
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
JoshUS wroteShadeflame wrote Thanks This helped with my project!
Gravediggers gonna dig... :facepalm:
Yep, the dig is going to hopefully be gone soon.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.