Shahze123
Shahze123

Reputation: 43

How would i go about creating a win count and guess count for my random number guessing game C#

Hey so i've got everything that i needed for my guessing game and everything checks out and runs without issue, the only problem is i want to have a wincount and a guess count so that the user can't get more guesses than what the program dictates. currently the program will tell you how many guesses you should be getting but it will only let you guess once because of the single if statement, so how would i go about doing this?

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

namespace GuessingGame
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare variables
            Int32 currentGuess, guessCount, winCount, upperLimit, randomNumber;
            double maxGuesses;
            bool gameOver;
            char playAgain;
            Random random = new Random();



            // Display title
            Console.WriteLine("Welcome to the high/low guessing game.");
            //Request user input for upper limit
            Console.WriteLine("Enter Upper range (e.g. 100):");
            upperLimit = Int32.Parse(Console.ReadLine());

            //Generate Random Number
            randomNumber = random.Next(1, upperLimit);
            maxGuesses = Math.Ceiling(Math.Log(upperLimit, 2) - 1);

            // Begin game
            Console.WriteLine("I picked a number between 1 and {0} you get {1} chances to guess it", upperLimit, maxGuesses);

            // Begin Guessing Process

            //Guess #1
            {
                Console.WriteLine(" Enter Guess #1: ");
                currentGuess = Int32.Parse(Console.ReadLine());

                if (currentGuess == randomNumber)
                {
                    Console.WriteLine("You got it!");
                }
                if (currentGuess > randomNumber)
                {
                    Console.WriteLine("Too High");
                }
                if (randomNumber > currentGuess)
                {
                    Console.WriteLine("Too Low");
                }

                    Console.ReadLine();
                }
            }
        }
    }

Upvotes: 0

Views: 839

Answers (1)

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

       int gessNum = 0;
       do
       {
            if (gessNum++ == maxGuesses){
                Console.WriteLine("You lost");
                break;
            }
            Console.WriteLine(string.Format(" Enter Guess {0}: ", gessNum));
            currentGuess = Int32.Parse(Console.ReadLine());

            if (currentGuess == randomNumber)
            {
                Console.WriteLine("You got it!");
            }
            if (currentGuess > randomNumber)
            {
                Console.WriteLine("Too High");
            }
            if (randomNumber > currentGuess)
            {
                Console.WriteLine("Too Low");
            }
            Console.ReadLine();
        } while (currentGuess != randomNumber);

Upvotes: 1

Related Questions