Howdy, Stranger!

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

Any suggestion how to make this work better?

I have watched the first 4 tutorials from YT, and i made this. Now i feel pretty finished with it but wanted some feedback. How would you do to make it better?



Code pasted here:

using System;


namespace My_Program_2

{

    class Program

    {

        static void Main(string[] args)

        {

            Random numberGen = new Random();

            int milliseconds = 500;

            int num01;

            int gen01;


            

            Console.WriteLine("Stone, Scissors, Paper/1,2,3");

            Console.WriteLine("Samma regler som vanligt fast mot en dator.");

            System.Threading.Thread.Sleep(milliseconds);

            Console.WriteLine("1");

            System.Threading.Thread.Sleep(milliseconds);

            Console.WriteLine("2");

            System.Threading.Thread.Sleep(milliseconds);

            Console.WriteLine("3");

            Console.WriteLine("");

            


            num01 = Convert.ToInt32(Console.ReadLine());

            if(num01 == 1)

            {Console.WriteLine("You choose Stone");

            }

            if(num01 == 2)

            {Console.WriteLine("You choose scissors");

            }

            if(num01 == 3)

            {Console.WriteLine("You choose Paper");

            }



            gen01 = numberGen.Next(1,4);

            if(gen01 == 1)

            {Console.WriteLine("Compiuter choose Stone");

            }

            if(gen01 == 2)

            {Console.WriteLine("Compiuter choose scissors");

            }

            if(gen01 == 3)

            {Console.WriteLine("Compiuter choose Paper");

            }

            

            

            int result = (num01 - gen01);

            if (result == 0)

            {

                Console.WriteLine("Draw!");

            }

            if (result == -1)

            {

                Console.WriteLine("You Win!");

            }

            if (result == 2)

            {

                Console.WriteLine("You Win!");

            }

            if (result == -2)

            {

                Console.WriteLine("You Lose!");

            }

            if (result == 1)

            {

                Console.WriteLine("You Lose!");

            }

            System.Threading.Thread.Sleep(milliseconds*8);

            

        }

    }

}

Answers

  • Sl0thSl0th Member
    edited August 2020

    Greetings, I do indeed have some suggestions for you. I'll order them by how big an impact they'll have. You definitely can and should learn to do all of them intuitively.

    The first thing you can do is to simplify and optimise the part where you check who won. You have made several "separate" if statements. At first you check if result is 0. Then, no matter whether it was true or not, you go on to check if result is -1. There is no reason to make any further checks after you already have confirmed that it was 0, for example. The way to get around that is to add "else" before each "if" after the first one. "else if" works just like "if", except that it doesn't trigger if the one before did. It would looks like this:

                if (result == 0)

                {

                    Console.WriteLine("Draw!");

                }

                else if (result == -1)

                {

                    Console.WriteLine("You Win!");

                }

                else if (result == 2)

                {

                    Console.WriteLine("You Win!");

                }

                else if (result == -2)

                {

                    Console.WriteLine("You Lose!");

                }

                else if (result == 1)

                {

                    Console.WriteLine("You Lose!");

                }

  • The next thing to do is to group the results together. You can achieve the same as you have here but with less code. The way to do that is to put multiple checks together in one, which you do with what is called boolean operators. The two most commonly used are && (AND) and || (OR). You can write a check, then an operator and then another check. These two checks are then "combined" into one result. && gives true if both checks are true. || gives true if both or one of them is true. This is what it could look like in your code:

                if (result == 0)

                {

                    Console.WriteLine("Draw!");

                }

                else if (result == -1 || result == 2)

                {

                    Console.WriteLine("You Win!");

                }

                else if (result == -2 || result == 1)

                {

                    Console.WriteLine("You Lose!");

                }

    This way you only have to write what happens when you win or lose once. So what happens in the one where it checks for a victory, is that it checks if result is -1 and if result is 2. If either or both are true, then it triggers.

  • This could be taken even further to reduce your workload a little. You see, there is really no reason to do the last check, where you see if you have lost. Because if neither of the two previous checks have triggered, then we know that this final one must. You can tell the program that by writing like this:

                if (result == 0)

                {

                    Console.WriteLine("Draw!");

                }

               else if (result == -1 || result == 2)

                {

                    Console.WriteLine("You Win!");

                }

    else

                {

                    Console.WriteLine("You Lose!");

                }

    At the end of a chain of if statements, you can just write "else" by itself. It will trigger if nothing else did. So essentially you can create a chain of if statements by starting with an "if", then having a number of "if else" and ending it with an "else".

  • There are of course many other improvements that can be made. You can for example think about how you could use arrays to make it even more simple.

Sign In or Register to comment.