3 Replies - 1871 Views - Last Post: 23 February 2012 - 08:48 AM Rate Topic: -----

#1 cheztaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 12

Creating a guessing game that increases in difficulty

Posted 18 February 2012 - 09:27 AM

To start off with i have only just started learning c# (2 weeks), i'm using visual studios professional 2010 and i'm using a console application.
I have to create a guessing game which i have done but i want to add more features to it;
*increase in difficulty with each round.
*The player can only have 5 tries before it is game over.
*The player is timed.
*There is a scoring system.

At the moment i am concentrating on creating the different levels and i have a problem with the amount of attempts the player can take.
I have introduced a for loop: for(int iCounter = 0; iCounter < 5; iCounter++)
I experimented with placing this in numerous places in the code but where i have placed it now seems to be the only place it has worked.
I want the game to display a failed message if the 5th turn is incorrect. I have tried to change the numbers in the if statement and the for loop to numbers above and below 5 however nothing works.
When the player reaches the 5th turn they can enter a guess for a 6th time and then a failed message is displayed.

Also i have experimented with a for loop to increase the difficulty.
I would like the maximum randomly generated number to be increased by 10 with each turn.
But increase the number of attempts by 2 with each turn. I have tried to look through the books i have and on the internet but the code carries on with the first level or doesn't work at all.

I haven't looked into timers or scoring yet but if anybody would like to give me any tips that would be helpful.

Apologies if code isnt tagged properly (first time on the forum, however i think i've done it correctly)

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

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)//Starting a while loop so the program will keep going until the user guesses the correct number.
            {
                for (int iCounter = 0; iCounter < 5; iCounter++)/* for loop is created so the player can only have 5 attempts before failing.
                                                                
                {


                    Random winningNumber = new Random();//declaring and assigning the winning number to the random number
                    int returnValue = winningNumber.Next(1, 11);//Converting the random number into an integer
                    int userNumber = 0;//starts user number guess at the integer 0
                    int numberOfGuesses = 0;//starts the guess count at integer 0
                    while (userNumber != returnValue)//starts a while loop
                    {
                        Console.WriteLine("I am thinking of a number between 1 and 10\nTake 5 or less guesses to find out the number?");
                        userNumber = int.Parse(Console.ReadLine());// converts user input into an integer
                        numberOfGuesses++;//counts number of guesses the user has
                        if (iCounter > 5)//If the player takes more than 5 tries they fail the game


                        {//NEED TO MAKE THE FAILED MESSAGE COME UP AFTER THE FIFTH TURN THAT HAS FAILED!!!!!


                            Console.WriteLine("You have failed to guess the number i am thinking in 5 or less attemps\nGame Over\nWould you like to start again?yes/no");
                            string sUserNewChoice;//creating new variable for nre choice
                            sUserNewChoice = Console.ReadLine();
                            if (sUserNewChoice == "no")
                                Environment.Exit(0);//if the player does not want to play again the console will exit
                            else if (sUserNewChoice == "yes")
                                Main(args);// if the player does want to player again the game will start from the very beginning.
                            else
                            {
                                Console.WriteLine("Please choose yes or no");
                                return;
                            }
                        }



                        if (userNumber == 0)//if statement returning to the beginning of the code if 0 is entered.
                            return;
                        else if (userNumber < returnValue)//else if statement to command the appropriate action if a user inputs a number lower than the winning number.
                        {
                            Console.WriteLine("The number i am thinking of is higher than " + userNumber + ". You have taken " + numberOfGuesses + "\nGuess again (Press Enter to try again)");
                            Console.ReadLine();
                            Console.Clear();
                            ++iCounter;
                            continue;//continues the loop

                        }
                        else if (userNumber > returnValue)// else if statement to command the appropriate action if a user inputs a higer number than the winning number.
                        {
                            Console.WriteLine("The number i am thinking of is lower than " + userNumber + ". You have taken " + numberOfGuesses + "\nGuess again (press Enter to try again)");
                            Console.ReadLine();
                            Console.Clear();
                            ++iCounter;
                            continue;
                        }

                    }
                    Console.WriteLine("Well done! The answer was " + returnValue + "\nYou took " + numberOfGuesses + " guesses.");
                    Console.ReadLine();
                    Console.Clear();

                    string userChoice;//creates a new variable based on user text input
                    Console.WriteLine("Would you like to play the next level? yes/no");
                    userChoice = Console.ReadLine();
                    Console.Clear();
                    while (userChoice == "no")
                        if (userChoice == "no")//if statement responding to users input
                        {
                            Console.WriteLine("Thank you for playing\nGoodbye");
                            Environment.Exit(0);// Exits the whole program rather than breaking the loop
                        }
                        else if (userChoice == "yes")
                            continue;//continues to read the code
                }
            }
   
        }
    }
}



I will appreciate anyone that helps me to solve this problem.

my code is neater in visual studios than it appears on here, sorry if it is hard to read.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a guessing game that increases in difficulty

#2 cheztaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 12

Re: Creating a guessing game that increases in difficulty

Posted 18 February 2012 - 10:08 AM

ignore the /* on line 14 as that has been closed with a */ in my actual code i just deleted some comments
Was This Post Helpful? 0
  • +
  • -

#3 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Creating a guessing game that increases in difficulty

Posted 18 February 2012 - 09:17 PM

Okay, I haven't actually read your code, I'm being a bit lazy and I don't generally read much that requires any scrolling(sorry). But I'm just making a few suggestions as to how to do that. Okay, try doing this, set up an integer for the maximum number of tries. I read a few lines and see that you do have a variable for the which try the player is on. So, instead of using a for loop, try using a while loop instead. A for loop will run until an integer is greater than the other, so it would be hard to code in a segment for breaking away if they guess correct. Also try making a bool type to tell whether or not they guess it right, and if they do, make convert it from false to true. The while loop previously mentioned should have two conditions, one is that the bool mentioned earlier is false and the other is that the user guess is less than the max guesses. Anyway, I'm rambling on about this, just try to write out the logic on a piece of paper first, this program is really just some simple logic.
Was This Post Helpful? 1
  • +
  • -

#4 cheztaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-February 12

Re: Creating a guessing game that increases in difficulty

Posted 23 February 2012 - 08:48 AM

View Postsuperkb10, on 18 February 2012 - 09:17 PM, said:

Okay, I haven't actually read your code, I'm being a bit lazy and I don't generally read much that requires any scrolling(sorry). But I'm just making a few suggestions as to how to do that. Okay, try doing this, set up an integer for the maximum number of tries. I read a few lines and see that you do have a variable for the which try the player is on. So, instead of using a for loop, try using a while loop instead. A for loop will run until an integer is greater than the other, so it would be hard to code in a segment for breaking away if they guess correct. Also try making a bool type to tell whether or not they guess it right, and if they do, make convert it from false to true. The while loop previously mentioned should have two conditions, one is that the bool mentioned earlier is false and the other is that the user guess is less than the max guesses. Anyway, I'm rambling on about this, just try to write out the logic on a piece of paper first, this program is really just some simple logic.


Thanks i've completely changed my code which is a lot neater and simpler to read using some of your advice.

Still struggling on increasing the maximum number of the random generator with each level without having to code each level seperately. I have tried to use a for loop but it doesnt make a different to my code or breaks the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1