I have been studying C#, and I am trying to model the Random number guessing game.
1 User inputs the number of try.
2 Computer generates random number.
3 If numbers doesn't match user lose one try
and if they match user gets two bonus try.
Is my program reasonable or is there anything I need to improve?
As player goes to next round, I also want to increase random number range by 100 to increase difficulty.For example, first round 1-100 next round 1-200 and so on. How can I do that? Any help would be appreciated.
public class GuessGame
{
public class Game
{
public int attemp { set; get; } = 0;
public override string ToString()
{
return $"The player's attemp <{attemp}>";
}
}
public static void Test()
{
Console.WriteLine("Lets play the Low or High Game");
var game = new Game();
game.attemp = InputAttempt();
PlayGame(game);
}
public static void PlayGame(Game game)
{
Console.WriteLine(game);
var comNum = GenerateComNum();
Console.WriteLine($"This is for test: ComNum is {comNum}");
do
{
var userNum = GenerateUserNum();
CompareNum(userNum, comNum, game);
Console.WriteLine(game);
} while (game.attemp >0 );
}
static void CompareNum(int userNum, int comNum,Game game)
{
if (userNum < comNum)
{
Console.WriteLine("User num is too Low try again");
Console.WriteLine();
game.attemp--;
}
else if(userNum > comNum)
{
Console.WriteLine("User num is too High try again");
Console.WriteLine();
game.attemp--;
}
else if(userNum == comNum)
{
Console.WriteLine("You got the answer!!");
Console.WriteLine();
game.attemp += 2;
PlayGame(game);
}
}
static int GenerateUserNum()
{
int userNum;
Console.Write("Please enter the num btw 1-100\t\t");
while (!Int32.TryParse(Console.ReadLine(), out userNum))
{
Console.WriteLine("Invalid num please input whole num");
}
return userNum;
}
static int GenerateComNum()
{
Random rnd = new Random();
var comNum = rnd.Next(1,101);
return comNum;
}
static int InputAttempt()
{
var attempt = 0;
Console.Write("How many Attemps would you like?\t");
while (!Int32.TryParse(Console.ReadLine(), out attempt))
{
Console.WriteLine("Invalid num please input whole num");
}
return attempt;
}
}