Guessing Game

Updated on June 21, 2017 in [D] Game Dev. gossip
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on June 20, 2017

I made Number Guessing game. It is console app. Can you give me some tips that I could code better Here is the code. 

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

namespace GuessRadnomNumber
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();

int randomSleep = random.Next(1, 5) * 1000;
int numberToGuess = random.Next(1, 100);
int userGuess;
int numberOfGuesses = 0;

bool isGuessed = false;

void ComputerAI()
{
Console.WriteLine(“I am thinking of a number between 0 and 100…”);
Thread.Sleep(randomSleep);
Console.WriteLine(“Guess the number.”);
}

void Game()
{
while (isGuessed == false)
{
userGuess = Int32.Parse(Console.ReadLine());

if (userGuess != numberToGuess)
{
if (userGuess > 100 || userGuess <= 0)
{
Console.WriteLine(“Enter number between 0 and 100.”);
}

else if (userGuess > numberToGuess)
{
Console.WriteLine(“Try lower”);
numberOfGuesses++;
}

else if (userGuess < numberToGuess)
{
Console.WriteLine(“Try higher.”);
numberOfGuesses++;
}
}
else
{
Console.WriteLine(“You guessed it.”);
isGuessed = true;
Console.WriteLine(“It took you {0} turns to guess the number.”, numberOfGuesses);
}
}
}

ComputerAI();
Game();

}
}
}

  • Liked by
Reply
2 on June 20, 2017

You put methods in methods.
This is valid code in C# since the latest version, but I’d advise you not to use that except for some special cases.

Most other languages don’t support this.
I recommend moving them inside the class and out of the Main.

The code itself seems ok.
Next step would be to learn about creating classes and using them.

Devoted
on June 21, 2017

It’s a thing since C# 7. A pretty awesome thing, actually.

Show more replies
  • Liked by
Reply
Cancel