Jump to content
  • Advertisement
Sign in to follow this  
SaulMaddox

Your opinion on my C# code.

This topic is 4465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have created my first program in C# and it is a guessing game. You try to guess what number the computer is thinking of from 1-10 and is say too high and too low. Once you guess correct it says you win. It is a fairly simple program but I created it with out following some tutorial. I used my mind, or what mind I had at 6:40 this morning ;). I would like to get any programmers opinion on the code that I wrote. Such as how to make the program better or even tips on how to make the code look better. What ever you can think of would be nice. I created it with Microsoft Visual C# 2005 Express. Here it is:

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

namespace TestGuessingGame1
{
    class Program
    {
        static void Main(string[] args)
        {
            //creates a random number
            Random randomClass = new Random();
            int randomNumber = randomClass.Next(1, 11);

            Console.WriteLine("Guess what number I am thinking of between 1-10!");


            while (true)
            {

                Console.Write("Your Guess: ");

                //takes the users guess and converts it to a number
                string userGuessString = Console.ReadLine();
                int userGuessNumber = Convert.ToInt32(userGuessString);


                if (userGuessNumber != randomNumber)
                {
                    if (userGuessNumber < randomNumber)
                    {
                        Console.WriteLine("Your guess was too low. Guess again!");
                    }
                    else if (userGuessNumber > randomNumber)
                    {
                        Console.WriteLine("Your guess was too high. Guess again!");
                    }
                }
                else
                {

                    Console.WriteLine(userGuessNumber + " was the correct number! Congratulations!");
                    break;
                }
            }
        }
    }
}



Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SaulMaddox
I have created my first program in C# and it is a guessing game. You try to guess what number the computer is thinking of from 1-10 and is say too high and too low. Once you guess correct it says you win.

It is a fairly simple program but I created it with out following some tutorial. I used my mind, or what mind I had at 6:40 this morning ;).

I would like to get any programmers opinion on the code that I wrote. Such as how to make the program better or even tips on how to make the code look better. What ever you can think of would be nice.

I created it with Microsoft Visual C# 2005 Express. Here it is:


*** Source Snippet Removed ***


Thanks!


Not bad. But, how would I quit? Also, what'll happen if, I or anybody else enters anything other than a number (e.g, hitting enter or entering letters instead)? Those would cause an exception to be thrown. =)

EDIT: I modified your program to add exception handling for when the user enters anything other than numbers. Here it is:


namespace TestGuessingGame1
{
class Program
{
static void Main( string[] args )
{
//creates a random number
Random randomClass = new Random( );
int randomNumber = randomClass.Next(1, 11);

Console.WriteLine("Guess what number I am thinking of between 1-10!");

int userGuessNumber;
string userGuessString;

while ( true )
{

Console.Write("Your Guess: ");

while ( true )
{
try
{
userGuessString = Console.ReadLine( );
//takes the users guess and converts it to a number
userGuessNumber = Convert.ToInt32(userGuessString);
break;
}
catch ( System.FormatException )
{
Console.WriteLine("Wrong input, enter numbers only!");
continue;
}
}

if ( userGuessNumber != randomNumber )
{
if ( userGuessNumber < randomNumber )
{
Console.WriteLine("Your guess was too low. Guess again!");
}
else if ( userGuessNumber > randomNumber )
{
Console.WriteLine("Your guess was too high. Guess again!");
}
}
else
{

Console.WriteLine(userGuessNumber + " was the correct number! Congratulations!");
break;
}
}
}
}
}



- xeddiex

Share this post


Link to post
Share on other sites
Quote:
Original post by SaulMaddox
I used my mind, or what mind I had at 6:40 this morning ;).
Thanks!

Well done! Your code seems fine. Except for that try catch error which was fixed above. Good to see you using your mind as well. Its very important to be able to think things throuh when you do them. If you do not do that, you will end up with a big mess on your hands. Good job.

Share this post


Link to post
Share on other sites
Wow, thanks for the input. I feel good about myself now ;)

Also I didn't realize to fix the format exception all you had to do was add catch System.FormatException. That is crazy easy. Here I wouldn't have that to go about it that way. I think I would have programmed it a longer route. Would have worked.... maybe. But that is soooo easy. WOW, so easy. I love C#!


Again, thanks guys!

I was thinking about adding a simple exit, restart, yes, and no commands to the game because you suggested it. That way the user can navigate more. Should I, or should I just go on to building a form based version?

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by SaulMaddox
I was thinking about adding a simple exit, restart, yes, and no commands to the game because you suggested it. That way the user can navigate more. Should I, or should I just go on to building a form based version?

Do both. Practice makes perfect as they say [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!