Making a number guessing game object-oriented
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Making a number guessing game object-oriented

Threaded View

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Making a number guessing game object-oriented

    I am trying to learn object oriented concepts. I have code for a simple number guessing game using windows forms. I am trying to make object oriented. I have had some success with console applications, but with windows forms I am increasingly confused. I realize I methods like initializegame(), gameWon(), GameLost(), FinishGame() but I'm having a hard time creating objects, and calling them, etc... Anybody have any suggestions? Also, is there a code formatting tool for this forum?

    Code:
    Namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Random number = new Random(); // This is declaring number; our randomizer
            int randomNumber; // Our randomized number
            int guessedNumber; // Our guessed number
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                randomNumber = number.Next(1, 10);
    
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text != "")
                {
                    try
                    {
                        guessedNumber = int.Parse(textBox1.Text);
                    }
                    catch
                    {
                        MessageBox.Show("Please enter an integer from 1-10", "Error!");
                        textBox1.Clear();
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                guessedNumber = int.Parse(textBox1.Text);
                bool win = false;
                bool wrong = false;
                if (textBox1.Text == "")
                {
                    MessageBox.Show("Please enter an integer from 1-10!", "Error!");
                    textBox1.Clear();
                }
                else
                {
                    if (guessedNumber >= 1 && guessedNumber <= 10)
                    {
                        if (guessedNumber == randomNumber)
                        {
                            win = true;
                            if (win == true)
                            {
                                if (MessageBox.Show("You have won! Would you like to play again ?", "You win!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                {
                                    randomNumber = number.Next(1, 10);
                                    label3.Text = "4";
                                    textBox1.Clear();
                                }
                                else
                                {
                                    this.Close();
                                }
                            }
                            else
                            {
                                // Do nothing
                            }
                        }
                        else
                        {
                            wrong = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter an integer from 1-10!", "Error!");
                        textBox1.Clear();
                    }
                }
                if (wrong == true)
                {
                    label3.Text = (int.Parse(label3.Text) - 1).ToString();
                    if (label3.Text == "0")
                    {
                        if (MessageBox.Show("You have lost! The randomized number was " + randomNumber + ". Would you like to play again ?", "You lost!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            randomNumber = number.Next(1, 10);
                            label3.Text = "4";
                            textBox1.Clear();
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        if (guessedNumber > randomNumber)
                        {
                            MessageBox.Show("Lower!", "Wrong!");
                            textBox1.Clear();
                        }
                        else
                        {
                            MessageBox.Show("Higher!", "Wrong!");
                            textBox1.Clear();
                        }
                    }
                }
                else
                {
                    // Do nothing
                }
     }
    
            private void progressBar1_Click(object sender, EventArgs e)
            {
    
            }
        }
     }
    Last edited by GremlinSA; December 7th, 2012 at 02:06 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center