Reputation: 21
I'm new to coding and C#; So, my apologies for the generic question.
I've created a basic random number guessing game; However, when I try to put if statements around the correct answers, it appears to fail.
If the guess is correct on the first go, I want the statement to say "congrats it took you 1 try"; If the guess is correct on the third go, I want the statement to say "congrats it took you 3 tries".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int guess = 0;
string welcome = "Guess a number between 1 and 10";
int num = rand.Next(1, 10);
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
Console.WriteLine(welcome);
int i = 0;
while (guess != num && guessCount < guessLimit)
{
try
{
guess = Convert.ToInt32(Console.ReadLine());
if (guess > num)
{
Console.WriteLine("Too High");
guessCount++;
}
if (guess < num)
{
Console.WriteLine("Too Low");
guessCount++;
}
}
catch
{
Console.WriteLine("Guess must be a number");
i--;
}
i++;
}
if (guess == num && guessCount != 1)
{
Console.WriteLine("Congrats, it took you " + i + " tries");
}
if (guess == num && guessCount == 1)
{
Console.WriteLine("Congrats, it took you " + i + " try");
}
if (guess != num && !outOfGuesses)
{
Console.WriteLine("GAME OVER, you have lost");
}
Console.ReadLine();
}
}
}
Upvotes: 2
Views: 171
Reputation: 236328
Increment guessCount
just after you read input. Otherwise it will stay 0
after the correct guess from first try.
guess = Convert.ToInt32(Console.ReadLine());
guessCount++;
Also you can re-organize your conditional logic
if (guess == num) // all successfull results go here
{
if (guessCount == 1) // show constant message when its 1st try
{
Console.WriteLine("Congrats, it took you 1 try");
}
else // Use string interpolation $ to show number of tries
{
Console.WriteLine($"Congrats, it took you {guessCount} tries");
}
}
else // you can get here only when player is out of guesses
{
Console.WriteLine("GAME OVER, you have lost");
}
Variables i
and outOfGuesses
should be removed.
Upvotes: 1
Reputation: 1205
Although algorithm to build this game could have been better, since you are new I do not want to make things complicated for you by making too many changes. Modified code to follow your algorithm so that it is easy for you to understand. Made some changes to last three if conditions in terms of ordering of condition checking and condition itself. It seems to be working. Please refer to code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int guess = 0;
string welcome = "Guess a number between 1 and 10";
int num = rand.Next(1, 10);
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
Console.WriteLine(welcome);
int i = 0;
while (guess != num && guessCount < guessLimit)
{
try
{
guess = Convert.ToInt32(Console.ReadLine());
if (guess > num)
{
Console.WriteLine("Too High");
guessCount++;
}
if (guess < num)
{
Console.WriteLine("Too Low");
guessCount++;
}
}
catch
{
Console.WriteLine("Guess must be a number");
i--;
}
i++;
}
if (guess != num && guessCount == guessLimit)
{
Console.WriteLine("GAME OVER, you have lost");
}
if (guess == num && guessCount == 0)
{
Console.WriteLine("Congrats, it took you " + i + " try");
}
if (guess == num && guessCount > 0)
{
Console.WriteLine("Congrats, it took you " + i + " tries");
}
Console.ReadLine();
}
}
}
Upvotes: 0