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 = Convert.ToInt32(Console.ReadLine()); 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(); } } }
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 = 10000000; 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 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine($"Guess: {guess}"); } if (guess > number) { Console.WriteLine($"{guess} is to high!"); if (alg) { if (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"); } } }
I hope that is not too outdated.. Just 3 years, my first comment i think on whole playlist yet. my solution: using System; class Program { public static void Main(string[] args) { Random rand = new Random(); int number, attempts, guess; char playingResponse; bool playing = true; bool valid; int min = 1; int max = 100; while (playing) { number = rand.Next(min, max + 1); attempts = 0; guess = 0; Console.WriteLine("Guess random number between "+min+" - "+max+":"); do { do { Console.Write("> "); valid = int.TryParse(Console.ReadLine(), out guess); } while (!valid); attempts++; if (guess > number) { Console.WriteLine("" + guess + " is too high!"); } else if (guess < number) { Console.WriteLine("" + guess + " is too low!"); } } while (guess != number); Console.WriteLine("You guessed number number " + number + " in " + attempts + " attempts!!!"); Console.Write(" Play again? (Y/n): "); playingResponse = Console.ReadKey().KeyChar; Console.WriteLine(" "); if (!"yY ".Contains(playingResponse)) { playing = false; Console.WriteLine("Have a nice day!"); } } } }
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 = Convert.ToInt32(Console.ReadLine());
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; } }
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 = Convert.ToInt32(Console.ReadLine());
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();
}
}
}
Hey bro , hope you doing well
I think I can optimiz your code ^_^
Much appreciated!
thanka bro
❤
❤
thank u for the tutorials! :3 this was the most fun one so far
Heya, thanks. That while loop helped me with two assignments.
Love your work!! Very helpful
you're an absolute mad man bro code. i was stock on this for a week. continue with changing lives. you're the best
Remeber this kids "Public static void main(String args[])
Awesome Work!!
Thank you soooooooo much for this guide!!
Very nice and clear tutorial, thanks :)
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 = 10000000;
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 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Guess: {guess}");
}
if (guess > number)
{
Console.WriteLine($"{guess} is to high!");
if (alg)
{
if (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");
}
}
}
Nice!
I hope that is not too outdated.. Just 3 years, my first comment i think on whole playlist yet.
my solution:
using System;
class Program
{
public static void Main(string[] args)
{
Random rand = new Random();
int number, attempts, guess;
char playingResponse;
bool playing = true;
bool valid;
int min = 1;
int max = 100;
while (playing)
{
number = rand.Next(min, max + 1);
attempts = 0;
guess = 0;
Console.WriteLine("Guess random number between "+min+" - "+max+":");
do
{
do
{
Console.Write("> ");
valid = int.TryParse(Console.ReadLine(), out guess);
} while (!valid);
attempts++;
if (guess > number)
{
Console.WriteLine("" + guess + " is too high!");
}
else if (guess < number)
{
Console.WriteLine("" + guess + " is too low!");
}
} while (guess != number);
Console.WriteLine("You guessed number number " + number + " in " + attempts + " attempts!!!");
Console.Write("
Play again? (Y/n): ");
playingResponse = Console.ReadKey().KeyChar;
Console.WriteLine("
");
if (!"yY
".Contains(playingResponse))
{
playing = false;
Console.WriteLine("Have a nice day!");
}
}
}
}
underrated.
This was very helpful. Thanks.
😮😮
Thanks man for this turtorials
Hell yeah brother, thanks for the video.
Thank you Bro ! 🙂
Thanks Man
the while(guess != number) the number part is red and everything else is ok what do i do
Thanks Bro!
why the response = ""; ?
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 = Convert.ToInt32(Console.ReadLine());
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();
}
}
}
@@muratanl2215 Ahh bro thanks :) already solved it thought but I appreciate you actually answered!
i have the same problem, how did you solve it?
@@miklo5240 Sorry bro this was so long ago and I havent coded for a while, hope you solve it
Thank you :)
commenting for the algorithm!
lesson check😇
❤
cucumber guessing game
i guess 62
Too
too low*; too high*
batata frita
random comment 😃
I don't understand sshit from this