C# number guessing game 🔢

Поделиться
HTML-код
  • Опубликовано: 8 фев 2025
  • C# number guessing game tutorial example explained
    #C# #number #game

Комментарии • 46

  • @BroCodez
    @BroCodez  3 года назад +56

    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();
    }
    }
    }

  • @krishnavarma7395
    @krishnavarma7395 3 года назад +8

    Love your work!! Very helpful

  • @condorcandi
    @condorcandi 3 года назад +5

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

  • @felipe9187
    @felipe9187 Месяц назад

    thank u for the tutorials! :3 this was the most fun one so far

  • @Hddh22
    @Hddh22 Год назад

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

  • @DjorgusPlaying
    @DjorgusPlaying 10 месяцев назад

    Thank you soooooooo much for this guide!!

  • @techsupportguyofficial
    @techsupportguyofficial 2 года назад +4

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

  • @farpurple
    @farpurple 3 месяца назад +1

    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!");
    }
    }
    }
    }

  • @camol121
    @camol121 3 года назад +1

    Very nice and clear tutorial, thanks :)

  • @TheDryDragon
    @TheDryDragon 2 года назад +8

    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");
    }
    }
    }

  • @Chronicles-qf6dz
    @Chronicles-qf6dz Месяц назад

    How does the guesses work? How does it know what you're asking because you just place guesses++ just trying to find clarity here

  • @ahmedahmed-x8j
    @ahmedahmed-x8j 6 месяцев назад

    Awesome Work!!

  • @chefdave7704
    @chefdave7704 2 года назад

    This was very helpful. Thanks.

  • @kellyncodes7421
    @kellyncodes7421 2 года назад

    Thanks man for this turtorials

  • @spartanranger
    @spartanranger 3 года назад

    Hell yeah brother, thanks for the video.

  • @bengtrust
    @bengtrust 3 года назад +2

    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();
    }
    }
    }

    • @bengtrust
      @bengtrust 2 года назад +3

      @@muratanl2215 Ahh bro thanks :) already solved it thought but I appreciate you actually answered!

    • @miklo5240
      @miklo5240 2 года назад

      i have the same problem, how did you solve it?

    • @bengtrust
      @bengtrust 2 года назад

      @@miklo5240 Sorry bro this was so long ago and I havent coded for a while, hope you solve it

  • @timoneri8812
    @timoneri8812 2 года назад

    Thank you Bro ! 🙂

  • @Klisterkillen
    @Klisterkillen 2 года назад +4

    why the response = ""; ?

  • @garshafatemianaraki9041
    @garshafatemianaraki9041 5 месяцев назад

    😮😮

  •  3 года назад

    Thanks Bro!

  • @A2XNANO
    @A2XNANO 4 месяца назад

    the while(guess != number) the number part is red and everything else is ok what do i do

  • @Livemusik01
    @Livemusik01 3 года назад

    Thank you :)

  • @lfigure1718
    @lfigure1718 2 года назад

    underrated.

  • @InsightfulGuy
    @InsightfulGuy 2 года назад

    Thanks Man

  • @augischadiegils.5109
    @augischadiegils.5109 3 года назад

  • @whitedinamo
    @whitedinamo 3 года назад

    lesson check😇

  • @simik4830
    @simik4830 4 месяца назад

    commenting for the algorithm!

  • @astergod
    @astergod 2 года назад

    cucumber guessing game

  • @crackrokmccaib
    @crackrokmccaib 11 месяцев назад

    Too

  • @definitelynotchris4776
    @definitelynotchris4776 4 месяца назад

    i guess 62

  • @The_Ancient_Guardian
    @The_Ancient_Guardian Год назад

    batata frita

  • @Drea730
    @Drea730 2 года назад

    random comment 😃

  • @boran5486
    @boran5486 10 месяцев назад

    I don't understand sshit from this