Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Hey guys im new here and need some help with my coding-random guessing game (http://www.go4expert.com/forums/hey-guys-im-help-coding-random-guessing-t19873/)

cokeiscool 24Oct2009 01:45

Hey guys im new here and need some help with my coding-random guessing game
 
Code:

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

namespace _._
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                int randomNumber = GenerateNumber(1, 21);
                int counter = 1;
                while (true)
                {
                    Console.Write("Enter a number between 1 and 20(0 to quit):");
                    int input = Convert.ToInt32(Console.ReadLine());

                    if (input == 0)
                        return;
                    else if (input < randomNumber)
                    {
                        Console.WriteLine("Too low, try again.");
                        ++counter;
                        continue;
                    }
                    else if (input > randomNumber)
                    {
                        Console.WriteLine("Too high, try again.");
                        ++counter;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("You guessed it! The number was {0}!", randomNumber);
                        if (counter == 1)
                            Console.WriteLine("And you guessed it right away! Bonus points!\n");
                        else
                            Console.WriteLine("It took you {0} {1}.\n", counter, counter == 1 ? "try" : "tries");
                        break;
                    }
                }
            }

        }
        static int GenerateNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
    }
}

This code is for a guessing game, the user inputs a number and the computer states if its right or wrong

no I got the basics but I need to do more, I need an option to ask the user how many games he wants, and to only allow the user 6 tries before he or she loses, also I need the numbers in the range to get smaller and smaller after each guess

im so stuck on this, any help would be great
I tried
Code:

if (counter == 6)
                        Console.WriteLine("You have attempted 6 times, you lost");
                    break;

and that didnt work

also tried
Code:

int guess = 0;
    for( guess = 0; guess < 6; guess++ )
    {
          .
          .
          .
          if( input == random )
            break;
    }
    if( guess < 6 )
        Console.WriteLine("It took you {0} {1}.\n", guess+1, (guess + 1) == 1 ? "try" : "tries");
    else
        Console.WriteLine("You lost.");

and that didnt work either

Todilo 6Dec2009 15:21

Re: Hey guys im new here and need some help with my coding-random guessing game
 
Do not use the continue: statement in the
else if (input > randomNumber)
and the
else if (input < randomNumber)
block

There is here no reason to use continue since you are not trying to skip past anything. If you write your IF-statements correctly they will not collide somehow, and since you have, dont worry :D.

When trying to solve problems like this, use the debugger, step through the program step by step F11 and you will easily find the solution.


All times are GMT +5.5. The time now is 08:18.