C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,159 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




C#.Net (Visual Studio 2005)

 

C#.Net (Visual Studio 2005), Guess the Number Application

st_dames

23 Nov, 2008 - 08:56 PM
Post #1

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 3

size=5]My name is Tyrone Dames, I am currently majoring in CIS (Networking) at Tarleton University, Texas. I need assistance with the newGame method to start a new game in Guess the Number application. I am not a programmer, my major requires computer programming courses; I am beginner at programming. When the new game button is clicked; it clears the appropriate fields; however, it does not reset the game. My codes are listed below:

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

namespace Deluxe_Number_Guesser
{
    public partial class guessGameForm : Form
    {
        // Declare variables
        int numOfTries;
        int guessNumber;
        int answer;
        bool done = false;

        public guessGameForm()
        {
            InitializeComponent();
        }

        private void guessGameForm_Load(object sender, EventArgs e)
        {
            // Initialize variables
            numOfTries = 0;
            guessNumber = 0;

            // Generate randon number
            Random rand = new Random();

            // Initialize number generate, 1 - 20; ensures a new number is generated
            // each time the form is loaded
            answer = rand.Next(1, 20) + 1;

        }

        public void submitButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (nxtGuessTxtBox.Text == "")
                {
                    MessageBox.Show("Number is required", "Entry Error");
                    nxtGuessTxtBox.Focus();
                }
                else
                {
                    guessNumber = Convert.ToInt32(nxtGuessTxtBox.Text);

                    if (guessNumber == 0 || guessNumber >= 21)
                    {
                        MessageBox.Show("Enter a number between 1 to 20", "Entry Error");
                        nxtGuessTxtBox.Focus();
                        nxtGuessTxtBox.SelectAll();
                    }
                    else
                        while (!done && numOfTries < 4)
                        {
                            if (guessNumber == answer)
                            {
                                messageLabel.Text = "You guessed correctly";
                                randNumberLabel.Text = "Play again";
                                numOfTries++;
                                done = true;
                                break;
                            }
                            else if (numOfTries >= 3)
                            {
                                messageLabel.Text = "You exceeded guess";
                                numOfTries++;
                                randNumberLabel.Text = "The number is " + answer;
                                nxtGuessTxtBox.Focus();
                                nxtGuessTxtBox.SelectAll();
                                done = true;
                                break;
                            }
                            else if (guessNumber > answer)
                            {
                                messageLabel.Text = "You guessed too high";
                                highGuessTxtBox.Text = guessNumber.ToString();
                                numOfTries++;
                                nxtGuessTxtBox.Focus();
                                nxtGuessTxtBox.SelectAll();
                                break;
                            }
                            else if (guessNumber < answer)
                            {
                                messageLabel.Text = "You guessed too low";
                                lowGuessTxtBox.Text = guessNumber.ToString();
                                numOfTries++;
                                nxtGuessTxtBox.Focus();
                                nxtGuessTxtBox.SelectAll();
                                break;
                            }
                            
                        }numOfGuessTxtBox.Text = numOfTries.ToString();
                }
            }

            catch (FormatException)
            {
                MessageBox.Show("Enter numeric value", "Entry Error");
                nxtGuessTxtBox.Focus();
                nxtGuessTxtBox.SelectAll();  
            }
            
        }

        private void newGameButton_Click(object sender, EventArgs e)
        {
            // Clear appropriate fields from form
            this.nxtGuessTxtBox.Text = "";
            this.highGuessTxtBox.Text = "";
            this.lowGuessTxtBox.Text = "";
            this.numOfGuessTxtBox.Text = "";
            this.messageLabel.Text = string.Empty;
            this.randNumberLabel.Text = string.Empty;
                
            // Reset the variable, answer to a random integer, 1 to 20
            // generate randon number
            Random rand = new Random();
            answer = rand.Next(1, 20) + 1;

            numOfTries = 0;
            guessNumber = 0;

            // Set focus to guess text box
            this.nxtGuessTxtBox.Focus();
        }
      
        private void exitButton_Click_1(object sender, EventArgs e)
        {
            // Terminate program

            this.Close();
        }
    }
}


User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: C#.Net (Visual Studio 2005)

23 Nov, 2008 - 09:16 PM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,324



Thanked: 454 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Do you mean that the random number is not changing? I would suggest making the rand variable a member of the class and seeding it with the time when the form is loaded:
csharp
Random rand = new Rand(DateTime.Now.Ticks);


This post has been edited by JackOfAllTrades: 23 Nov, 2008 - 09:16 PM
User is offlineProfile CardPM
+Quote Post

st_dames

RE: C#.Net (Visual Studio 2005)

24 Nov, 2008 - 05:05 AM
Post #3

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 3

QUOTE(JackOfAllTrades @ 23 Nov, 2008 - 09:16 PM) *

Do you mean that the random number is not changing? I would suggest making the rand variable a member of the class and seeding it with the time when the form is loaded:
csharp
Random rand = new Rand(DateTime.Now.Ticks);


[quote]Thanks for replying; the random number changes during the first loop (numOfTries <=4); however, it does not change after I select new game or indicate if the number is high or low or total the number of tries. I attached the program.



Attached File(s)
Attached File  Deluxe_Number_Guesser.zip ( 71.07k ) Number of downloads: 14
User is offlineProfile CardPM
+Quote Post

althaz

RE: C#.Net (Visual Studio 2005)

25 Nov, 2008 - 06:36 PM
Post #4

New D.I.C Head
*

Joined: 25 Nov, 2008
Posts: 4

You didn't set the "done" variable back to false (and that is required for the while loop to execute).

Also, instead of:
CODE

if (guessNumber == 0 || guessNumber >= 21)    //Your code


You should use:

CODE

if (guessNumber < 0 || guessNumber > 20)    //Better code :)


The former allows negative numbers to be input.
User is offlineProfile CardPM
+Quote Post

st_dames

RE: C#.Net (Visual Studio 2005)

26 Nov, 2008 - 03:32 AM
Post #5

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 3

QUOTE(althaz @ 25 Nov, 2008 - 06:36 PM) *

You didn't set the "done" variable back to false (and that is required for the while loop to execute).

Also, instead of:
CODE

if (guessNumber == 0 || guessNumber >= 21)    //Your code


You should use:

CODE

if (guessNumber < 0 || guessNumber > 20)    //Better code :)


The former allows negative numbers to be input.


[quote]I am truly grateful for the advice. I made the change, however; when I click new game, it does not allow 4 tries as the loop indicates. I can only enter one number for the new game and the number of tries state 0.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:52PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month