Question

I'm designing a Number Guessing Game, that reads a 4 Digit number from the Console. The computer randomly generates the 4 digit number and the user tries to guess the digits and their correct order. The program returns answer in a format 0A0B or 1A3B or 4A0B e.t.c, where it returns A if the number you guessed in correct and it's position is correct too, and will return B if the number you guessed is correct but it's position is wrong.

I want to implement a hint system, which provides hints when the user types the word 'hint'. How do I limit the number of times a User can type the word 'hint' ? This is the code I have so far

static void Main(string[] args)
{
    while (true)
    {
            Console.WriteLine("********************************");
            Console.WriteLine("What number do you think it is ?");
            Console.WriteLine("********************************");
            Console.WriteLine();

            bool GameOver = false;
            int[] targetNumber = GenerateRandomNumber(); // Generates the Random Number
            while (!GameOver)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine(); // Gets an answer from User

                if (answer.ToLower() == "hint") // Designed to Provide a hint.
                {
                    int Rando = GenerateRandomNumberforHint(); // function I created
                   // Console.WriteLine("The Random Number generated was {0}",Rando);
                    if (Rando == 0)
                    {
                        Console.WriteLine("{0}XXX", targetNumber[Rando]);
                    }
                    else if (Rando == 1)
                    {
                        Console.WriteLine("X{0}XX", targetNumber[Rando]);
                    }
                    else if (Rando == 2)
                    {
                        Console.WriteLine("XX{0}X", targetNumber[Rando]);
                    }
                    else if (Rando == 3)
                    {
                        Console.WriteLine("XXX{0}", targetNumber[Rando]);
                    }
                    continue;
                }



           /* Code to check if the number is correct */     

            Console.ReadLine();
        }
    }

Do I need to create a Data Structure, and if I do, how do I implement that? I'm extremely new to C Sharp so any help would be appreciated

Was it helpful?

Solution

I think this is exactly what you want:)

int hitcounter = 0;
int lastHint = -1;
while (!GameOver)
{ 
    Console.Write("Answer: ");
    string answer = Console.ReadLine();

    if (answer.ToLower() == "hint")
    {
        if (hitcounter < 10)
        {  
                       //preveintg double hint. something like that. 
                       //you can use later array or list if you need more hint
                       int hint = generaterandomhints();
                       while (hint == lastHint)
                       { 
                           hint = generaterandomhints();
                       }

           hitcounter++;
           //write the hints
        }
        else
        {
            //write you reached the maximum of the hints
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top