-1

I am creating a rock paper scissors game in Pascal for my computer science class at university.

I have the code where I type 0, 1, or 2 to represent rock, paper, or scissors and that is fully random.

I was wondering if there was a way to make the letters r,p,s random to stand for rock, paper, or scissors instead of using the numbers.

I have tried to randomize this game with r,p and s. Does the random "command" only work for numbers, and if so, is their a command to randomize letters?).

Thank you for taking the time to read this! If you have any questions please feel free to ask me, thanks!

ps. I don't know if you know what I am talking about but I tried to post the code for over half an hour at 5 am and it still isn't formatted correctly and wont let me post it, sorry I'm new to this.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • You can add the code unformatted and someone will hopefully edit it so that it is formatted as code – thg Feb 12 '18 at 10:05
  • shortcut method: 1) paste your code at the bottom of the edit window 2)highlight it and 3) hit "ctrl + k" – John Coleman Feb 12 '18 at 10:26
  • 1
    As far as your question goes -- of course you can randomly pick letters. If you know how to randomly pick the numbers 0,1,2 just use these as indices in an array whose entries consist of the 3 letters. If you haven't had arrays yet, just use an `if ... else if ... else` construct based on the value of the randomly generated number. – John Coleman Feb 12 '18 at 10:37
  • This site does not prevent you from posting code simply because it's not formatted. See any of the thousands of posts that are edited every day to add formatting because they were posted without it. In addition, there is complete help for formatting displayed when you're typing your post, as well as extensive markup help shown when you click the help **?** in the toolbar. Saying *the site won't allow it* is simply untrue. – Ken White Feb 12 '18 at 21:11

1 Answers1

0

You can define an enum and then get the highest value in the enum with HIGH(). Then convert to integer using ORD, and add one, to get the number of elements in the enum. Call random, and cast back to enum type.

Free Pascal allows to write enum values to the console as demonstrated, this might not work with e.g. Delphi.

Type 
    TRPSType = (Rock,Paper,Scissors);

var i : integer;

begin  
 randomize;
  for i:=0 to 10 do
    writeln(TRPSType(random(ord(high(TRPSType))+1)));
end.

Maybe it can be generalized to a generics function, but I'm not that familiar with those.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89