C# number guessing game 🔢

preview_player
Показать описание
C# number guessing game tutorial example explained

#C# #number #game
Рекомендации по теме
Комментарии
Автор

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
bool playAgain = true;
int min = 1;
int max = 100;
int guess;
int number;
int guesses;
String response;

while (playAgain)
{
guess = 0;
guesses = 0;
response = "";
number = random.Next(min, max + 1);

while (guess != number)
{
Console.WriteLine("Guess a number between " + min + " - " + max + " : ");
guess =
Console.WriteLine("Guess: " + guess);

if (guess > number)
{
Console.WriteLine(guess + " is to high!");
}
else if (guess < number)
{
Console.WriteLine(guess + " is to low!");
}
guesses++;
}
Console.WriteLine("Number: " + number);
Console.WriteLine("YOU WIN!");
Console.WriteLine("Guesses: " + guesses);

Console.WriteLine("Would you like to play again (Y/N): ");
response = Console.ReadLine();
response = response.ToUpper();

if (response == "Y")
{
playAgain = true;
}
else
{
playAgain = false;
}
}

Console.WriteLine("Thanks for playing! ... I guess");

Console.ReadKey();
}
}
}

BroCodez
Автор

Heya, thanks. That while loop helped me with two assignments.

condorcandi
Автор

This was great, after watching the next few videos and then going back to this one, i was able to make the computer guess. It consistently guesses a number between 1 - 10.000.000 in only 22-24 turns. Here is my code if you want to check it out:

using System;

namespace GuessingGame
{
class Program
{
static void Main(string[] args)
{
Random random = new();
bool playAgain = true;
bool alg;
int min = 1;
int max =
int newMin;
int newMax;
int guess;
int number;
int range;
int guesses;
String? response;

while (playAgain)
{
guess = 0;
guesses = 0;
newMin = min;
newMax = max;
number = random.Next(min, max + 1);

Console.WriteLine("Would you like to see an AI in action? Y/N: ");
response = Console.ReadLine();

if (response == "Y" || response == "y")
{
alg = true;
// get the amount of numbers possible
range = max - min + 1;
guess = range / 2 + min;
}
else
{
alg = false;
}

while (guess != number)
{
Console.WriteLine($"Guess a number between {min} and {max}:");
if (alg)
{
Console.WriteLine($"AI guess: {guess}");
}
else
{
guess =
Console.WriteLine($"Guess: {guess}");
}

if (guess > number)
{
Console.WriteLine($"{guess} is to high!");
if (alg)
{
(newMax > guess) { newMax = guess; }
range = newMax - newMin + 1;
guess = range / 2 + newMin;
}
}
else if (guess < number)
{
Console.WriteLine($"{guess} is to low!");
if (alg)
{
if (newMin < guess) { newMin = guess; }
range = newMax - newMin + 1;
guess = range / 2 + newMin;
}
}
guesses++;
}
if (alg) { guesses++; }

Console.WriteLine($"Number: {number}");
Console.WriteLine("YOU WIN!");
Console.WriteLine($"Guesses: {guesses}");

Console.WriteLine("Would you like to play again (Y/N)");
response = Console.ReadLine();

if (response == "Y" || response == "y")
{
playAgain = true;
}
else
{
playAgain = false;
}
}
Console.WriteLine("Thanks for playing! ... I guess");
}
}
}

TheDryDragon
Автор

Remeber this kids "Public static void main(String args[])

techsupportguyofficial
Автор

you're an absolute mad man bro code. i was stock on this for a week. continue with changing lives. you're the best

Hddh
Автор

Thank you much for this guide!! <3<3

DjorgusPlaying
Автор

Very nice and clear tutorial, thanks :)

camol
Автор

Hell yeah brother, thanks for the video.

spartanranger
Автор

Anyone know why when I say yes the whole thing doesnt play again? Here's my code:
using System;

namespace random
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
bool playAgain = true;
int min = 1;
int max = 100;
int guess;
int number;
int guesses;
String Response;
Console.WriteLine("I am thinking of a number between 1-100, can you guess it?");
Console.WriteLine("Enter your guess:");
while (playAgain)
{
guess = 0;
guesses = 0;
number = random.Next(min, max + 1);
Response = "";


while (guess != number)
{


guess =

if(guess < number)
{
Console.WriteLine("The number I'm thinking of is higher than that, guess again");
Console.WriteLine("Enter another guess:");
}
else if(guess > number)
{
Console.WriteLine("The number I'm thinking of is lower than that, guess again");
Console.WriteLine("Enter another guess:");


}
guesses++;
}
Console.WriteLine("The number I was thinking of was " + number + "!");
Console.WriteLine("You win!");
Console.WriteLine("Would you like to play again? Answer with yes or no.");
Response = Console.ReadLine();
if(Response == "yes")
{
Console.Write("Ok, lets go!");
playAgain = true;


}

else
{
Console.WriteLine("Aaah ok :[ Thanks for playing tho...");
playAgain = false;
}
}




Console.ReadKey();
}
}
}

bengtrust
Автор

I don't understand sshit from this

boran