Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

BRACKEYS - Solution to C# Tutorial 04 Challenge

2»

Comments

  • these is mine

    using System;


    namespace My_awesome_program

    {

        class Program

        {

            static void Main(string[] args)

            {

                Random numberGen = new Random();


                int r1 , r2;

                r1 = 0;

                r2 = 1;

                int atpts = 0;

                Console.WriteLine("Press enter to roll a die: ");


                while (r1 != r2) {


                    Console.ReadKey();

                    r1 = numberGen.Next(1, 7);

                    r2 = numberGen.Next(1, 7);

                    Console.WriteLine("Dice 1: "+ r1);

                    Console.WriteLine("Dice 2: "+ r2 +"\n");

                    atpts++;

                }


                Console.WriteLine("It took you "+ atpts + " attempts to roll two of a kind.");


                Console.ReadKey();

            }

        }

    }

  • using System;


    namespace MyAwesomeMathQuiz

    {

        class Program

        {

            static void Main(string[] args)

            {

                Random numberGen = new Random();


                int roll1 = 0;

                int roll2 = 1;

                int attempts = 0;


                Console.WriteLine("Press any key to roll both dices.");


                while(roll1 != roll2) {

                    Console.ReadKey();


                    roll1 = numberGen.Next(1, 7);

                    roll2 = numberGen.Next(1, 7);

                    Console.WriteLine("You rolled : " + roll1 + " and " + roll2);

                    attempts++;

                }


                Console.WriteLine("It took u " + attempts + (" attempts to roll same numbers!"));


               Console.ReadKey();

            }

        }

    }

  • I tried as well :D

    using System;


    namespace Best_Program

    {

        class Program

        {

            static void Main(string[] args)

            {  

                Random numberGen = new Random();

                int roll01 = 0;

                int roll02 = 0;

                int attemts = 0;

               

                Console.WriteLine("Press enter to roll the die!");


                while (roll01 != 6 || roll02 != 6)

                {

                    Console.ReadKey();

                    roll01 = numberGen.Next(1,7);

                    Console.WriteLine("Your first die rolled " + roll01);

                    roll02 = numberGen.Next(1,7);

                    Console.WriteLine("Your second die rolled " + roll02);

                    attemts++;

                }

                Console.WriteLine("It took you " + attemts + " attemts!");   

                Console.ReadKey();

            }

        }

    }

     

  • I tried too ended up good.

    using System;


    namespace Program

    {

        class Program

        {

            static void Main(string[] args)

            {

               //Program Title:

               Console.Title = "Two of a Kind";


               //Program Width:

               Console.WindowWidth = 150;


               //Font Color:

               Console.ForegroundColor = ConsoleColor.Yellow;


               //Content:

               Random numberGen = new Random();

               int dice1 = 0;

               int dice2 = 1;

               int attempts = 0;


               //Gameplay:

               Console.WriteLine("Welcome to Two of a Kind. This is a game of chance where you will have to roll two dice.");

               Console.WriteLine("If you get two if the same dice within 4 attempts, you win and will be rewarded with 100 lives.");

               Console.ForegroundColor = ConsoleColor.Red;

               Console.WriteLine("But if you don't, you will lose the number of lives equal to the attempts that you have made.");

               Console.WriteLine("GOODLUCK :)");

               Console.ReadKey();

               Console.ForegroundColor = ConsoleColor.Yellow;

               while (dice1 != dice2)

               {

                   dice1 = numberGen.Next(1, 7);

                   dice2 = numberGen.Next(1, 7);

                   Console.WriteLine("You rolled: " + dice1);

                   Console.WriteLine("You rolled: "+ dice2 + "\n");

                   Console.ReadKey();

                   attempts++;

               }

               Console.WriteLine("Press ENTER to know the number of you attempts...");

               Console.ReadKey();

               Console.WriteLine("You have made " + attempts + " attempts...");

               Console.ReadKey();

               if (attempts <= 4)

               {

                   Console.WriteLine("You have passed. Receive the blessing of IMMORTALITY!");

               } else 

               {

                   Console.ForegroundColor = ConsoleColor.Red;

                   Console.WriteLine("You have failed mortal. RECEIVE THE PUNISHMENT FOR THE WICKED!!!");

               }

               //Ending Sequence:

               Console.WriteLine("Press ENTER to know the next judgement.....");

               Console.ReadKey();

            }

        }

    }

  • love this series of tutoriasl and it's really helping me to acomplish my goal of learning to code at least a bit!!

    so i made my version and had no problem doing my two dices program and the i tried to make it like a trow dice against the computer but for some reason i still can't make it work. help!!!


    Console.WriteLine("Ok " + name + " Let's play Dice!!");

          Console.WriteLine("First you roll and then I roll, Wins the bigger number every turn");


          Random numberGen = new Random();

          int roll1 = 0;

          int roll2 = 1;

         

          Console.WriteLine("READY....Press ENTER to roll");

                

          if ( roll1 == roll2){

            Console.ReadKey();

            Console.WriteLine("Nice tie, let's do it again!!");

                   }


          while (roll1 < roll2)

          {

            Console.ReadKey();

            roll1 = numberGen.Next(1, 7);

            roll2 = numberGen.Next(1, 7);

            Console.WriteLine("You rolled: " + roll1 + " I rolled: " + roll2 );

             

            Console.WriteLine(" I win, Let's go again");

            Console.WriteLine(" PRESS ENTER");

            //Console.ReadKey();

          }

                 

            Console.WriteLine("You rolled: " + roll1 + " I rolled: " + roll2 );

             

            Console.WriteLine ("YOU WIN!!!");

            //Console.ReadKey();

            


          Console.ReadKey(); 

  • I don't understand why you set roll2 to 1

  • Victor me too idk why

    @Victor16

  • somebody explained it to me

    It's because when you tried to enter into the while loop they would both be 0 therefore (role1 != role2) would be false and the loop would never start.


    @GostGK_YT

  • Oh, I made it exactly by my own :)

  • Added a small if statement so it doesn't say "it took you 1 attempts" :)

  • I was getting the same number rolled for both dice, so I made a new variable for a new Random() numberGen and was able to get the dice to roll different numbers.


    using System;


    namespace c__Programs

    {

        class Program

        {

            static void Main(string[] args)

            {

                Random numberGen = new Random();

                Random nr = new Random();


                int roll = 0;

                int roll2 = 1;

                int attempts = 0;


                Console.WriteLine("Press enter to roll the dice");



                while(roll != roll2) {


                    Console.ReadKey();


                    roll = numberGen.Next(1,7);

                    roll2 = nr.Next(1, 7);


                    Console.WriteLine("Rolled: " + roll);

                    Console.WriteLine("Rolled: " + roll2 + "\n");


                    attempts++;

                }


                Console.WriteLine("It took you " + attempts + " to roll a pair.");



                

                //Write before closing the program.

                Console.ReadKey();

            }

        }

    }

  • here is my version because Brackeys' version didn't work for me. after a lot of fideling around it finally works and i don't know why.

    class Program

       {

           static void Main(string[] args)

           {


               Random numberGen = new Random();


               int roll1 = 0;

               int roll2 = 0;

               int attempts = 0;


               Console.WriteLine("press enter to roll the dies");


               while (true)

               {

                   Console.ReadKey();


                   roll1 = numberGen.Next(1, 7);

                   roll2 = numberGen.Next(1, 7);


                   Console.WriteLine("Roll 1: " + roll1);

                   Console.WriteLine("Roll 2: " + roll2 + "\n");


                   attempts++;


                   if (roll1 == roll2)

                   {

                       Console.WriteLine("you rolled two of a kind");

                       Console.WriteLine("It took you " + attempts + " attempts");

                       break;

                   }


               }




               //wait before closing

               Console.ReadKey();

           }

       }

  • My solution (changed it up a little bit).


    using System;
    using System.Collections.Generic;
    
    
    
    namespace yes_man
    {
        
        class program
        {
            static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Title = "this is a stupid program";
    
    
                Random theNumber = new Random();
    
    
                int roll = 1;
                int roll2 = 2;
                int attempts = 5;
    
    
                Console.WriteLine("Click enter to roll 2 dice. To win, get the same number on both rolls (1-5). You have 5 attempts.");
    
    
                while(attempts > 0 && roll != roll2)
                {
    
    
                    Console.ReadKey();
                    attempts--;
                    roll = theNumber.Next(1, 6);
                    roll2 = theNumber.Next(1, 6);
    
    
                    Console.WriteLine("Roll 1: " + roll);
                    Console.WriteLine("\nRoll 2: " + roll2);
                    Console.WriteLine("\nYou have " + attempts + " attempts left.\n\n");
    
    
                }
                if(roll == roll2 && attempts > 0)
                {
                    Console.WriteLine("Congrats! You won!");
    
    
                } else if(roll != roll2 && attempts == 0)
                {
                    Console.WriteLine("You lost! Reopen this program to try again.");
                }
    
    
    
                Console.ReadKey();
    
    
            }
        }
    }
    
  • anjalianjali Member
    edited March 21

    using System;


    namespace my_awsome_program


    {


        class Program


        {


            static void Main(string[] args)


            {

              //In this program you can enter the maximum number of times you want to try for 'two of a kind'

               Console.WriteLine("Enter the maximum attempts you want to make for 'two of a kind':");

               

               int max=Convert.ToInt32(Console.ReadLine());

              

              /*When i declared roll1 and roll2 with value=0, program wasn't entering the while loop since the codition 

              is satisfied because both roll1 and roll2 have value 0, so i tried this solution instead*/

               Random numberGen= new Random();

               int roll1=1;

               int roll2=2;

               int attempt=0;

               Console.WriteLine("Press enter to roll");

               while (roll1 !=roll2&&attempt!=max)

               {

                   

                   Console.ReadKey();

                   roll1=numberGen.Next(1,7);

                   roll2=numberGen.Next(1,7);

                   Console.WriteLine("Roll 1 : "+roll1+"\nRoll 2 : "+roll2);

                    attempt=attempt+1;

               }


               /*if the maximum attempt is reached this line prints out otherwise the else condition is satisfied*/

               if (attempt==max)

               {

                   Console.WriteLine("Sorry,You have run out of chances, Better luck next time.");

               }

               else

               {

               Console.WriteLine("Congrats! it took you "+attempt+" attempts to roll 'two of a kind'.");

               }

               Console.ReadKey();



            }


        }


    }

  • I made an attempt, at first I made the mistake of putting int roll2 = 0;

    using System;


    namespace Tutorials

    {

        class Program

        {

            static void Main(string[] args)

            // Dice Generator:

            {

                Console.ForegroundColor = ConsoleColor.Blue;

                Console.Title = "DiceMania";

                

                Random numberGen = new Random();


                int roll1 = 0;

                int roll2 = 1;

                int attempts = 0;



                Console.WriteLine("Press Enter to roll the 2 dice");

                while (roll1 != roll2){

                    Console.ReadKey();


                    roll1 = numberGen.Next(1, 7);

                    roll2 = numberGen.Next(1, 7);

                    attempts++;


                    Console.WriteLine("Dice 1: " + roll1);

                    Console.WriteLine("Dice 2: " + roll2 + "\n ");

                }

                   

            

                Console.WriteLine("It took you " + attempts + " attempts to roll two of a kind");


                // wait before closing

                Console.ReadKey();

            }

        }

    }

  • Mo_Mo_ Member

    Heres my solution for this one

    {
                Random numberGen = new Random();
    
    
                int roll = 0;
                int roll2 = 1;
                int attempt = 0;
    
    
                Console.WriteLine("Press enter to roll the dice.");
    
    
                while(roll != roll2) {
                    Console.ReadKey();
    
    
                    roll = numberGen.Next(1, 7);
                    roll2 = numberGen.Next(1, 7);
    
    
                    Console.WriteLine("Roll1: " + roll);
                    Console.WriteLine("Roll2: " + roll2);
    
    
                    attempt++;
                }
    
    
                Console.WriteLine("It took you " + attempt + " attempts to roll the same number on each die.");
                
                // Wait 
                Console.ReadKey();
            }
        }
    }
    
  • using System;


    namespace ScriptLN4

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.ForegroundColor = ConsoleColor.Green;

    Random numberGen = new Random();

    int roll = 0;

    int roll1 = 1;

    int Attempts = 0;


    Console.WriteLine("Press Enter to roll the dice");

    while (roll != roll1)

    {

    Console.ReadKey();

    roll = numberGen.Next(1, 7);

    roll1 = numberGen.Next(1, 7);

    Console.WriteLine("Roll01: " + roll);

    Console.WriteLine("Roll02: " + roll1);


    Attempts++;

    }


    Console.WriteLine("It took " + Attempts + " to roll the same number on both dice");


    Console.ReadKey();

    }

    }

    }

  • frostyfrosty Member

    using System;



    namespace lil_nigs_program

    {

        class Program

        {

            static void Main(string[] args)

            {      

                Random numberGen = new Random();


                int roll01 = 0;

                int roll02 = 1;

                int attempts = 0;


                Console.WriteLine("Press enter to roll the Dice");


                while(roll01 != roll02)

                {

                    Console.ReadKey();


                    roll01 = numberGen.Next(1, 7);

                    roll02 = numberGen.Next(1, 7);


                    Console.WriteLine("Roll 1: " + roll01);

                    Console.WriteLine("Roll 2: " + roll02 +"\n");


                    attempts++;

                }


                Console.WriteLine("It took u " + attempts + "to roll two of a kind");


                // Wait before closing

                Console.ReadKey();

            }


           

            

                

            

        }

    }

  • Added some colors :D

    using System;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random numberGen = new Random();
    
                int roll_1 = 0;
                int roll_2 = 1;
                int attempts = 0;
                
                Console.WriteLine("Press enter to roll the dice.");
    
                while (roll_1 != roll_2)
                {
                    Console.ReadKey();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    roll_1 = numberGen.Next(1, 7);
                    roll_2 = numberGen.Next(1, 7);
                    Console.WriteLine("\nRoll 1: " + roll_1);
                    Console.WriteLine("Roll 2: " + roll_2);
                    attempts++;
                }
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("It took you " + attempts + " attempts to roll the same numbers.");
                
                // wait before close
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nPress Enter to exit..");
                Console.ReadKey();
            }
        }
    }
    


  • tipkarczxtipkarczx Member
    edited April 21

    using System;


    namespace První_hra

    {

        

        class Program

        {

            static void Main(string[] args)

            {

            Random numGen = new Random();

               //variables 

               String lang; 

               String gameMode; 

               int roll01 = 0;

               int roll02 = 1;

               int attepts = 0;


                //game

               Console.WriteLine("cz/eng");

               lang = Convert.ToString(Console.ReadLine());

               

               //eng

               if(lang == "eng")

            {

               Console.WriteLine("Chose from these modes: hard/easy and write it below.");


               gameMode = Convert.ToString(Console.ReadLine());

                //hard

               if (gameMode == "hard")

               {

                    

               while (roll01 != roll02)

               {

                   Console.ReadKey();


                   roll01 = numGen.Next(1, 101);

                   roll02 = numGen.Next(1, 101);


                   Console.WriteLine("First number " + roll01);

                   Console.WriteLine("Second number " + roll02);

                  

                  Console.WriteLine("________________");

                    

                   attepts++;

               }          


                Console.WriteLine("It took you " + attepts + " attepts");

               }

                // easy

               if (gameMode == "easy")

               {

                  while (roll01 != roll02)

               {

                   Console.ReadKey();


                   roll01 = numGen.Next(1, 7);

                   roll02 = numGen.Next(1, 7);


                   Console.WriteLine("First number " + roll01);

                   Console.WriteLine("Second number " + roll02);

                  

                  Console.WriteLine("________________");

                    

                   attepts++;

               }  

                Console.WriteLine("It took you " + attepts + " attepts");         

               }

               }

                

                //cz

                if(lang == "cz")

                {

                   Console.WriteLine("Vyber si z těhle módů: lehké/těžké, a napiš je pod tenhle text,");


               gameMode = Convert.ToString(Console.ReadLine());

                //těžké

               if (gameMode == "těžké")

               {

                    

               while (roll01 != roll02)

               {

                   Console.ReadKey();


                   roll01 = numGen.Next(1, 100);

                   roll02 = numGen.Next(1, 100);


                   Console.WriteLine("První číslo " + roll01);

                   Console.WriteLine("Druhé číslo " + roll02);

                  

                  Console.WriteLine("________________");

                    

                   attepts++;

               }          


                Console.WriteLine("Dalo vám to " + attepts + " pokusů");

               }

                //lehké

               if (gameMode == "lehké")

               {

                  while (roll01 != roll02)

               {

                   Console.ReadKey();


                   roll01 = numGen.Next(1, 7);

                   roll02 = numGen.Next(1, 7);


                   Console.WriteLine("První číslo " + roll01);

                   Console.WriteLine("Druhé číslo " + roll02);

                  

                  Console.WriteLine("________________");

                    

                   attepts++;

               }   

                  Console.WriteLine("Dalo vám to " + attepts + " pokusů");       

               } 

                }   

                //Ends the program

                Console.ReadKey();

            }



        }

    }


    I made gameodes and you can chose your language

  • NexNex Member

    Assignment4:

    using System;
    
    namespace C__Assignment4
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random numberGen = new Random();
    
                int roll1 = 0;
                int roll2 = 1;
                int attempts = 0;
    
                Console.WriteLine("Press any key to roll both dies.");
    
                while(roll1 != roll2) {
                    Console.ReadKey();
    
                    roll1 = numberGen.Next(1, 7);
                    roll2 = numberGen.Next(1, 7);
    
                    Console.WriteLine("Roll#1: " + roll1);  
                    Console.WriteLine("Roll#2: " + roll2 + "\n");
                    attempts++;
                }
    
                Console.WriteLine("It took " + attempts + " attempts to get a two of a kind");
    
                //Wait before closing
                Console.ReadKey();
            }
        }
    }
    
  • using System;


    namespace C__project

    {

        class Program

        {

            static void Main(string[] args)

            {

                Random numberGen = new Random();

                

                int roll1 = 0;

                int roll2 = 1;

                int attempts = 0;


                Console.WriteLine("Press Enter to roll a die");


                while(roll1 != roll2) {

                    Console.ReadKey();


                    roll1 = numberGen.Next(1, 7);

                    roll2 = numberGen.Next(1, 7);

                    Console.WriteLine("Roll1: " + roll1);

                    Console.WriteLine("Roll2: " + roll2);

                    attempts++;

                }


                Console.WriteLine("It took you " + attempts + " attempts to roll two equal numbers");


                Console.ReadKey();

            }

        }

    }

    I feel like I've done something wrong with this thing, but it works, so hey, no need to fix :)

  • My solution:


    I did the brackey's C# course episode 4 challenge, is my solution good?


    using System;


    namespace The_dice_roll

    {

      class Program

      {

        static void Main(string[] args)

        {

          Console.WriteLine("Press any key to roll the Dice.");


          int roll01 = 0;

          int roll02 = 1;

          int attempts = 0;


          Random numberGen = new Random();


          while(roll01 != roll02) 

          {

            Console.ReadKey();

            roll01 = numberGen.Next(1, 7);

            roll02 = numberGen.Next(1, 7);

            attempts++;

            Console.WriteLine("\nYou got\nDice 1: " + roll01 + "\nDice 2: " + roll02);

          }

          Console.WriteLine("\nIt took you " + attempts + " rolls to get both dice to be the same number.");



          Console.ReadKey();

        }

      }

    }

  • epiepi Member

    using System;


    namespace fartMachine

    {

        class Program

        {

            static void Main(string[] args)

            {   


                Console.Title = "pffflarrpp";

                

                Random numberGen1 = new Random(); //Random Number Generators

                Random numberGen2 = new Random();


                int roll1 = 1;

                int roll2 = 0;

                int attempts = 0;


                Console.WriteLine("Press enter to roll the dice.");


                while (roll1 != roll2)

                {

                    Console.ReadKey();


                    roll2 = numberGen2.Next(1, 7); //Random number between 1-6

                    Console.WriteLine("You rolled " + roll2);


                    roll1 = numberGen1.Next(1, 7); //Random number between 1-6

                    Console.WriteLine("You rolled " + roll1 +"\n");

                    attempts++;


                    Console.WriteLine("Roll #" + attempts);

                }


                Console.WriteLine("You rolled a two of a kind after " + attempts +" attempts!");


                //Wait Before Closing

                Console.ReadKey();

            }

        }

    }

  • DusTVDusTV Member

    Here's what I got:

    using System;


    namespace DusProgram

    {

        class Program

        {

            static void Main(string[] args)

            {

                Console.Title = ("DusProgram");

                Console.ForegroundColor = ConsoleColor.White;

                Console.WindowHeight = 30;

         

                Random numberGen = new Random();


                int roll1 = 0;

                int roll2 = 1;

                int attempts = 0;


                Console.WriteLine("Press enter to roll the dice");


                while(roll1 != roll2) {

                  Console.ReadKey();


                  roll1 = numberGen.Next(1, 7);

                  roll2 = numberGen.Next(1, 7);

                  Console.WriteLine("Rolled a " + roll1 + " and a " + roll2 + "!");


                  attempts++;

                }


                Console.WriteLine("Attempts: " + attempts);


                // Wait before closing

                Console.ReadKey();

            }

        }

    }

    I almost did it without looking for the solution, I just didn't know you need to set the second roll integer to "1".

  • using System;


    namespace my_awesome_project

    {

      class Program

      {

        static void Main(string[] args)

        {

          // questiojn for 3 dice

          // int roll01;

          //int roll02;

          //int roll03;


          // main programmes starts throught here 

          Random numberGen = new Random();

          int roll01 = 0;

          int roll02 = 0;

          int roll03 = 0;

          int attempts = 0;

          Console.WriteLine("Process enter to Role a Dice");



    // I need help here (in while condition) , please somebody help me

          while (roll01 != roll02; roll02 != roll03; roll03 != roll01 )

          {

            Console.ReadKey();


            roll01 = numberGen.Next(1, 7);

            roll02 = numberGen.Next(1, 7);

            roll03 = numberGen.Next(1, 7);


            Console.WriteLine("Roll 1: " + roll01);

            Console.WriteLine("Roll 2: " + roll02 + "\n");

            Console.WriteLine("Roll 3: " + roll03 + "\n");


            attempts++;

          }


          Console.WriteLine("It took you " + attempts + " attempts to roll two of a kind.");


           

          Console.ReadKey();

        }

      }

    }

Sign In or Register to comment.