C# number guessing game 🔢

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ธ.ค. 2024

ความคิดเห็น • 46

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +50

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

    • @ademlouaar1793
      @ademlouaar1793 3 ปีที่แล้ว +3

      Hey bro , hope you doing well
      I think I can optimiz your code ^_^

    • @devval1428
      @devval1428 ปีที่แล้ว

      Much appreciated!

    • @matin1099
      @matin1099 ปีที่แล้ว

      thanka bro

    • @ErwinJulambri
      @ErwinJulambri 10 หลายเดือนก่อน

    • @ErwinJulambri
      @ErwinJulambri 10 หลายเดือนก่อน

  • @felipe9187
    @felipe9187 3 วันที่ผ่านมา

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

  • @condorcandi
    @condorcandi 2 ปีที่แล้ว +4

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

  • @krishnavarma7395
    @krishnavarma7395 3 ปีที่แล้ว +8

    Love your work!! Very helpful

  • @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

  • @techsupportguyofficial
    @techsupportguyofficial 2 ปีที่แล้ว +4

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

  • @ahmedahmed-x8j
    @ahmedahmed-x8j 4 หลายเดือนก่อน

    Awesome Work!!

  • @DjorgusPlaying
    @DjorgusPlaying 8 หลายเดือนก่อน

    Thank you soooooooo much for this guide!!

  • @camol121
    @camol121 3 ปีที่แล้ว +1

    Very nice and clear tutorial, thanks :)

  • @TheDryDragon
    @TheDryDragon ปีที่แล้ว +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");
    }
    }
    }

    • @jkovert
      @jkovert ปีที่แล้ว

      Nice!

  • @farpurple
    @farpurple หลายเดือนก่อน +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!");
    }
    }
    }
    }

  • @lfigure1718
    @lfigure1718 2 ปีที่แล้ว

    underrated.

  • @chefdave7704
    @chefdave7704 2 ปีที่แล้ว

    This was very helpful. Thanks.

  • @garshafatemianaraki9041
    @garshafatemianaraki9041 3 หลายเดือนก่อน

    😮😮

  • @kellyncodes7421
    @kellyncodes7421 2 ปีที่แล้ว

    Thanks man for this turtorials

  • @spartanranger
    @spartanranger 3 ปีที่แล้ว

    Hell yeah brother, thanks for the video.

  • @timoneri8812
    @timoneri8812 2 ปีที่แล้ว

    Thank you Bro ! 🙂

  • @InsightfulGuy
    @InsightfulGuy 2 ปีที่แล้ว

    Thanks Man

  • @A2XNANO
    @A2XNANO 2 หลายเดือนก่อน

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

  •  3 ปีที่แล้ว

    Thanks Bro!

  • @Klisterkillen
    @Klisterkillen 2 ปีที่แล้ว +2

    why the response = ""; ?

  • @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

  • @Livemusik01
    @Livemusik01 3 ปีที่แล้ว

    Thank you :)

  • @simik4830
    @simik4830 2 หลายเดือนก่อน

    commenting for the algorithm!

  • @whitedinamo
    @whitedinamo 2 ปีที่แล้ว

    lesson check😇

  • @augischadiegils.5109
    @augischadiegils.5109 3 ปีที่แล้ว

  • @astergod
    @astergod ปีที่แล้ว

    cucumber guessing game

  • @definitelynotchris4776
    @definitelynotchris4776 2 หลายเดือนก่อน

    i guess 62

  • @crackrokmccaib
    @crackrokmccaib 10 หลายเดือนก่อน

    Too

  • @MW-ow8zh
    @MW-ow8zh 11 หลายเดือนก่อน +2

    too low*; too high*

  • @The_Ancient_Guardian
    @The_Ancient_Guardian ปีที่แล้ว

    batata frita

  • @drea7363
    @drea7363 ปีที่แล้ว

    random comment 😃

  • @boran5486
    @boran5486 8 หลายเดือนก่อน

    I don't understand sshit from this