Tuesday, May 1, 2012

Small Basic Rock Paper Scissors Code

Just a little quickie, this time. I was looking at the Rock, Paper, Scissors that others have written, and Jason boiled it down to 2 lines. Which I think isn't strictly true. I'd have call it one line. Here is another one-liner. This one works by first splitting the input into 3, and splitting it again. Basically, a simpler way to handle multi-dimensional strings. I also added a touch of detail in that you actually type R/P/S as entry. I could have added the code to convert the input into uppercase, but it's long enough as it is.

I do not do tutorials, at least at this time, but I left out all the original code as comments so hopefully, you can learn from it.

'Rock Paper Scissor
' by Harry Hardjono
'April 2012 - GTJ601
'
'This is an implementation of Rock-Paper-Scissors
'Rock beat Scisscors
'Scisscors beat Paper
'Paper beat Rock
'
'The input requires capital letter, and is one of these:
'(R)ock, (P)aper,(S)cissors
'The program will randomly choose one of them
'and display the result.
'
'The output is encoded because I'm too lazy to type them out.
'">R" means "You choose Rock."
'">P" means "You choose Paper."
'">S" means "You choose Scissors."
'"vR" means "Computer chooses Rock."
'"vP" means "Computer chooses Paper."
'"vS" means "Computer chooses Scissors."
'"=W" means "You win!"
'"=L" means "Computer wins!"
'"=D" means "It's a draw!"
'So, ">RvS=W" is interpreted as:
'"You choose Rock. Computer chooses Scissors. You win!"
'
'The following is the original source code before
'I collapse them into one line.
'
'Loop:
'T1=">RvR=D>RvP=L>RvS=W>PvR=W>PvP=D>PvS=L>SvR=L>SvP=W>SvS=D"
'P1=(Text.GetIndexOf("RPS",TextWindow.Read())-1)
'T2=Text.GetSubText(T1,1+(P1*18),18)
'P2=(Math.GetRandomNumber(3)-1)
'T3=(Text.GetSubText(T2,1+(P2*6),6))
'TextWindow.WriteLine(T3)
'Goto Loop

Loop:
TextWindow.WriteLine((Text.GetSubText(Text.GetSubText(">RvR=D>RvP=L>RvS=W>PvR=W>PvP=D>PvS=L>SvR=L>SvP=W>SvS=D",1+((Text.GetIndexOf("RPS",TextWindow.Read())-1)*18),18),1+((Math.GetRandomNumber(3)-1)*6),6)))
Goto Loop

No comments: