Clouds Clouds Airship Belthil Tower Nether Temple
📚 Learn more about The Shire's history on our Timeline. 🕰

Need Help With C#

Options
0031benjy
0031benjy Confirm Email, Retired Staff
edited March 2014 in Off Topic
Well I recently started learning C# for my course, my most recent assignment requires me to create a program (which will probably seem very basic to some) but have the code written in an efficient way, I have wrote a Rock-Paper-Scissors style thing and I need help:
1) Shortening it where possible
2) Making it so if the user enters anything other than an integer into the command prompt it will return a simple 'Console.Write("Error!");' instead of crashing.

Code is on Pastebin at: http://pastebin.com/LTU4scaY


Any help is much appreciated! 

Comments

  • Lord_Drekar
    Options
    well, it appears to be some form of language. I have no idea what.. sorry I can't help :(
  • PsYcHoX88
    Options
    Hi Ben!

    You do have a lot of repetitive code, I would firstly make some private functions.

    1) A message function that you can provide variables answer and decision as parameters and make it do all the print lines, since I see only a small part of big chunks of text changes due to those variables. You can literally replace all those 9 blocks with one function and some small ifs.

    2) You can put an else after all your if statements to determine invalid input and do your Console.Write.

    Any questions feel free to ask me, like clarifying :) 
  • 0031benjy
    0031benjy Confirm Email, Retired Staff
    Options
    Mmhmm, thanks a lot, I'll look into it, I believe Functions is contained within the specifications for one of my pass criteria so I better get to grips with them, thanks again Psycho, helps a lot!
  • PsYcHoX88
    PsYcHoX88 Member
    edited March 2014
    Options
    And you mentioned possibility of using arrays, I could see it as an answer Array so that answers[1] = Rock, answers[2] = Paper, answers[3] = Scissors.. but I would recommend using 0 1 2, since Arrays are zero-based, meaning some of your code would change :P so you would do like ...printline answers[answer] and it would say "rock" and you can change what it says in your top code when you set the arrays, hopefully i haven't lost you :P
  • Maghion
    Maghion Member, Wiki Editor
    Options

    I always wanted to learn coding but I failed advanced java so bad that everytime I set my mind to try to learn a coding language, I get discourage.

    :/

     

  • 0031benjy
    0031benjy Confirm Email, Retired Staff
    Options
    Mags, as I'm still very new to it I can't say much, but I've found it very much like learning French or Spanish (but more fun), it is a lot easier with tutors and people to help you. It's definitely daunting starting out, but if you stick with it, and make sure you understand what you're doing before you move on, you'll get it eventually.

    I recommend using an IDE too, I use Visual Studio as that supports C++, C#, Visual Basic and the sorts, but if you want to learn Java you can download (for free I think) Eclipse, I'm not to sure about Java, but I know Microsoft's Virtual Academy has loads of "Getting Started With:"s for Microsoft Based Programming Languages.

    Also most languages are fundamentally based around C, so if you learn one language, it becomes significantly easier to learn another since despite having different wording in places, it'll likely share the same procedures and structure.

    Don't get discouraged, it's a brilliant (and for some a very fun) skill to have! Good luck!

    (Note: I'd help, but I myself am learning, maybe one day... xD) 
  • PsYcHoX88
    PsYcHoX88 Member
    edited March 2014
    Options
    Hey Ben, so I was bored right now and did your assignment the way I would have done it (or was trying to tell you how to do it :P) I changed a lot of stuff.

    So I tested it, and commented my code, and I cut it down to like 15% of your original code length :P

    I used an Array, a Method, and a Try/Catch.

    http://pastebin.com/kxAVBNTV

    The FormatException catches if invalid numbers AND if you put invalid characters instead of numbers :)
  • 0031benjy
    0031benjy Confirm Email, Retired Staff
    Options
    I wish I knew how to use Methods and Try/Catch, look useful for many situations, the only alternative to check for an integer I could think of is TryParse, which is what I added to my submitted one, still considerably longer though, nice work!
  • PsYcHoX88
    Options
    <3
  • Dracolllich
    Options
    My 2 cents... I program in Flash so this will just be in broad application:

    - Identify items that will always happen
         You have "You have Choosen:" The AI has Chososen:" and "You have:" in every segement of code.... is there a way to program it only once?

    - 1 always = Rock, 2 always = Scissors, and 3 always = Paper
         In Flash, you can declare a constant  ROCK == 1 SCISSORS = 2 PAPER = 3
         Reading through lines of code to debug, the constants help making reading easier
         You never have to rely on your memory to remeber which number SCISSORS was
         Also, since its created at the start of the program, if ROCK ever needs to be 2 for what ever reason, you only have to change the constant instead of finding all references through out the program

    So, in general terms, I would approach it like this:

    input = int;
    compChoice = int;
    compChoice = random number => 1 but <= 3;

    constant ROCK = 1;
    constant PAPER = 2;
    constant SCISSORS = 3;

    write "User please enter a number: 0 to exit, " ROCK " for rock, " PAPER " for paper, or " SCISSORS " for scissors"

    input = user input;
    check  (if input <0 or >3, goto write line)
    check (if input = 0 exit program)

    write "You have selected:"
         if (input = ROCK) then write "Rock"
        if (input = SCISSORS) then write "Scissors"
        if (input = PAPER) then write "Paper" 

    write "The AI has selected:"
        if (compChoice = ROCK) then write "Rock"
        if (compChoice = SCISSORS) then write "Scissors"
        if (compChoice = PAPER) then write "Paper"

    write "You have:"
       if (input = compChoice) then write "Drawn"
       else if ( input = ROCK and compChoice = PAPER) then write "Lost"
        else if (input = ROCL and compChoice = SCISSORS) then write "Won"
       else if (input = PAPER..... etc. for the other 4 outcomes.
              this  would be best with a case statement now that I think about it,


    Hopefully this gives you a little bit of food for thought