3 Replies - 640 Views - Last Post: 07 December 2012 - 08:50 AM Rate Topic: -----

#1 eraebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-November 12

Number Guessing Game: Applying object-orientation

Posted 07 December 2012 - 08:38 AM

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?


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)
        {

        }

        
                
    }

        }


Is This A Good Question/Topic? 0
  • +

Replies To: Number Guessing Game: Applying object-orientation

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,644
  • Joined: 02-June 10

Re: Number Guessing Game: Applying object-orientation

Posted 07 December 2012 - 08:45 AM

There is a tutorial for objects linked in my signature block. It should help.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Re: Number Guessing Game: Applying object-orientation

Posted 07 December 2012 - 08:47 AM

You design an object that is focused on the game logic, then build a GUI that interacts with it.

1 Game 3 GUIs Tutorial (VB.net)
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,644
  • Joined: 02-June 10

Re: Number Guessing Game: Applying object-orientation

Posted 07 December 2012 - 08:50 AM

In addition to working on OOP concept there are a number of basic things you could work on. Updating GUI controls names for example. Textbox1 isn't helpful to you 6 months down the line.

You have large amounts of code in the GUI event handler which is something to avoid.

You shouldn't be constantly referring to the GUI controls for there values. Controls aren't meant to be your variables for value storage. Have a read through the properties tutorial linked in my signature block. While you're there the "Where do I start?" link is an article about planning before building that might help you out.

You say you're having trouble thinking in a WinForms and OOP way. Maybe this tutorial will help.


Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1