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:

Experiment5X
  • TTG Senior
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:

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.

The following 2 users thanked Experiment5X for this useful post:

Slit (02-01-2012), Soap141 (10-25-2011)
#2. Posted:
VELVETKNIFE
  • Junior Member
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
#3. Posted:
Slit
  • Wise One
Status: Offline
Joined: Feb 01, 201212Year Member
Posts: 554
Reputation Power: 25
Status: Offline
Joined: Feb 01, 201212Year Member
Posts: 554
Reputation Power: 25
Thanks This helped with my project!
#4. Posted:
iSequL
  • Challenger
Status: Offline
Joined: Sep 22, 201113Year Member
Posts: 111
Reputation Power: 4
Status: Offline
Joined: Sep 22, 201113Year Member
Posts: 111
Reputation Power: 4
Broken link bro, Can you fix it?<3
#5. Posted:
Experiment5X
  • TTG Senior
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.
#6. Posted:
JoshUS
  • TTG Contender
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:
#7. Posted:
Z61
  • TTG Fanatic
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
JoshUS wrote
Shadeflame wrote Thanks This helped with my project!


Gravediggers gonna dig... :facepalm:

Yep, the dig is going to hopefully be gone soon.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.