Guess The Number

numberGuess

This is a simple number guessing game. The user has to guess a random number from 1 to 10. If the number guessed is too high or too low the user is notified. I’m using 2 classes to demonstrate simple OOP coding.

NumberGuess Class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NumberGuess
{
    public partial class NumberGuess : Form
    {
        private Guess _myGuess;
        
        //Class variable to hold the random number generated.
        int randomNumber = 0;

        public NumberGuess()
        {
            InitializeComponent();
            
            //Creating a new Guess object to use.
            _myGuess = new Guess();
            
            //Calling the method to generate a random number from the Guess class.
            randomNumber = _myGuess.getAndSetRandomNumber();

            //Test code can be removed later. Checking to see what the random number is.
            Console.WriteLine(randomNumber);
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void guessButton_Click(object sender, EventArgs e)
        {   
            //Set the status label to black.
            statusLabel.ForeColor = Color.Black;
            
            //Holding tha value of the entered number to guess.
            int guess = 0;
            bool isError = false;

            //Getting the value of what the user typed in.
            if (!int.TryParse(guessTextBox.Text, out guess))
            {
                isError = true;
                statusLabel.ForeColor = Color.Red;
                statusLabel.Text = "Status: Only enter number values between 1 and 10.";
                Console.WriteLine(guess);    //Test code to see the value of guess.
                Console.WriteLine(isError);    //Test code to see the value if isError.
            }

            if (!isError)
            {
                //If the number guessed is smaller than the random number.
                if (guess > randomNumber)
                {
                    statusLabel.ForeColor = Color.Red;
                    statusLabel.Text = _myGuess.numberHigh();
                }
                
                //If the number guessed is lower than the random number.
                if (guess < randomNumber)
                {
                    statusLabel.ForeColor = Color.Blue;
                    statusLabel.Text = _myGuess.numberLow();
                }
                
                //If the number guessed is the same as the random number.
                if (guess == randomNumber)
                    statusLabel.Text = _myGuess.correctNumber();
            }
        }

        private void restartButton_Click(object sender, EventArgs e)
        {
            //Setting a new random number to guess.
            randomNumber = _myGuess.getAndSetRandomNumber();

            //Setting the status text color to black.
            statusLabel.ForeColor = Color.Black;

            //Informing the user to proceed.
            statusLabel.Text = "Status: Ok guess a new number.";
            Console.WriteLine(randomNumber);    //Test code to display the new random number.
        }
    }
}

Guess Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumberGuess
{
    public class Guess
    {
        private int _minimumNumber = 1;
        private int _maximumNumber = 10;
        private int _targetNumber = 0;
        private Random _randomNumber = new Random();
        private string _numberToHigh = "Status: Too large.";
        private string _numberToLow = "Status: Too small.";
        private string _numberCorrect = "Status: Correct!";

        public string NumberCorrect
        {
            get { return _numberCorrect; }
            set { _numberCorrect = value; }
        }

        public string NumberToHigh
        {
            get { return _numberToHigh; }
            set { _numberToHigh = value; }
        }

        public string NumberToLow
        {
            get { return _numberToLow; }
            set { _numberToLow = value; }
        }

        public int MinimumNumber
        {
            get { return _minimumNumber; }
            set { _minimumNumber = value; }
        }

        public int MaximumNumber
        {
            get { return _maximumNumber; }
            set { _maximumNumber = value; }
        }

        public int TargetNumber
        {
            get { return _targetNumber; }
            set { _targetNumber = value; }
        }

        public Random RandomNumber
        {
            get { return _randomNumber; }
            set { _randomNumber = value; }
        }

        
        //Getting the minimum number, maximum number, and setting the random number based
        public int getAndSetRandomNumber()
        {
            return _targetNumber = _randomNumber.Next(MinimumNumber, MaximumNumber);
        }

        //Getting the message display if the number guessed is higher than the random number.
        public string numberHigh()
        {
            return NumberToHigh;
        }

        //Getting the message display if the number guessed us lower than the random number.
        public string numberLow()
        {
            return NumberToLow;
        }

        //Message display if the number guessed is the same as the random number.
        public string correctNumber()
        {
            return NumberCorrect;
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s