Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

super compact rock-paper-scissors game

Discussion in 'Scripting' started by PvTGreg, Feb 8, 2015.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Hi i was looking around the internet for some basic csharp projects to try and i came across lots of psuedocodes for rock-paper-scissors they were all very long winded with lots of if statements i thought to my self isn't that a bit much? so i started making my own from scratch and came up with this. i know its nothing amazing but its a pretty compact rock paper scissors game and im proud that i made it. they choice for the player is taken from 3 buttons

    Code (CSharp):
    1.  void RPS()
    2.         {
    3.             Random rnd = new Random();
    4.             pcChoice = rnd.Next(1, 4);
    5.             label1.Text = "Computer Choice : "+pcChoice.ToString();
    6.             //Have I Drawn?
    7.             if (choice == pcChoice)
    8.             {
    9.                 MessageBox.Show("Draw");
    10.             }
    11.          
    12.             //Have I Won?
    13.             else  if (choice == 1 && pcChoice == 3 || choice == 2 && pcChoice == 1 || choice == 3 && pcChoice == 2)
    14.             {
    15.                 MessageBox.Show("You Win");
    16.             }
    17.             else //Welp ive lost
    18.             {
    19.                 MessageBox.Show("You Lose");
    20.             }
    21.          
    22.  
    23.         }
     
    Last edited: Feb 9, 2015
    chelnok likes this.
  2. Alanisaac

    Alanisaac

    Joined:
    Jan 25, 2015
    Posts:
    15
    Awesome! You absolutely should be proud: any time you can take code and make it more readable/maintainable, that's a big win!

    If you're up for it, here are some things that might challenge you further:
    • Where's the choice variable in the script? If I copy and paste this into my class, for example, how do I set what my choice is vs the computer's? Could you post the full class, or an explanation of how to set choice?
    • If you're up for an additional challenge, check this out: Is there a way that a developer could know which number rock, paper and scissors each are? Maybe take a look at enums and see if you could use one of those to represent your choices
     
  3. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    the 3 choices are set in 3 buttons
    button 1 sets choice to 1 and calls the rps function
    button 2 sets choice to 2 and calls the rps function
    button 3 sets choice to 3 and calls the rps function

    i was just gonna use some if statements for displaying rock,paper or scissors from the computer so if the computer picks 1 it will display rock and so on

    ill read more on enums and see if i can implement it
    thanks for your reply