# Paper, Rock, Scissors - Challenge

## Recommended Posts

Challenge: Create the smallest number lines of AutoIt code to create a paper, rock, scissors game in which you ask for the user's choice and generate a random result for the computer.

Good luck.

Note: Mat can't play in this game. Sorry!

If this needs to be moved then by all means move it to an appropriate location. Thanks.

##### Share on other sites

its not that good but I figured I do it. I spent like 10 minutes on it I might try to make a better one

```\$gamegui = GUICreate("Main",300,200)

\$rock = GUICtrlCreateButton("Rock",0,0,100,30)
\$paper = GUICtrlCreateButton("Paper",100,0,100,30)
\$scissor = GUICtrlCreateButton("Scissors",200,0,100,30)

GUISetState()

while 1
\$msg = GUIGetMsg(1)
Switch \$msg[1]
Case \$gamegui
Switch \$msg[0]
Case \$rock
\$npc = Random(1,3,1)
MsgBox(4096,"Rock","You have chosen Rock. Goodluck!")
\$rockval = 1
If \$npc = 1 Then
MsgBox(4096,"","Npc has chosen: Rock")
\$npcrock = 1
Else
If \$npc = 2 Then
MsgBox(4096,"","Npc has chosen: Paper")
\$npcpaper = 2
Else
If \$npc = 3 Then
MsgBox(4096,"","Npc has chosen: Scissor")
\$npcscissor = 3
EndIf
EndIf
EndIf
If \$npc = 1 And \$rockval = 1 Then
MsgBox(4096,"Tie!","You have tied!")
Else
\$paperval = 2
\$scissorval = 3
If \$npc = 1 And \$scissorval = 3 or \$paperval = 2 Then
MsgBox(4096,"Winner","You won!")
EndIf
EndIf

Case \$paper
\$npc = Random(1,3,1)
MsgBox(4096,"Paper","You have chosen Paper. Goodluck!")
\$paperval = 2
If \$npc = 1 Then
MsgBox(4096,"","Npc has chosen: Rock")
\$npcrock = 1
Else
If \$npc = 2 Then
MsgBox(4096,"","Npc has chosen: Paper")
\$npcpaper = 2
Else
If \$npc = 3 Then
MsgBox(4096,"","Npc has chosen: Scissor")
\$npcscissor = 3
EndIf
EndIf
EndIf
If \$npc = 2 And \$paperval = 2 Then
MsgBox(4096,"Tie!","You have tied!")
Else
\$rockval = 1
\$scissorval = 3
If \$npc = 1 And \$rockval = 1 or \$scissorval = 3 Then
MsgBox(4096,"Winner","You won!")
EndIf
EndIf
Case \$scissor
\$npc = Random(1,3,1)
MsgBox(4096,"Scissor","You have chosen Scissor. Goodluck!")
\$scissorval = 3
If \$npc = 1 Then
MsgBox(4096,"","Npc has chosen: Rock")
\$npcrock = 1
Else
If \$npc = 2 Then
MsgBox(4096,"","Npc has chosen: Paper")
\$npcpaper = 2
Else
If \$npc = 3 Then
MsgBox(4096,"","Npc has chosen: Scissor")
\$npcscissor = 3
EndIf
EndIf
EndIf
If \$npc = 3 And \$scissorval = 3 Then
MsgBox(4096,"Tie!","You have tied!")
Else
\$paperval = 2
\$rockval = 1
If \$npc = 1 And \$rockval = 1 or \$paperval = 2 Then
MsgBox(4096,"Winner","You won!")
EndIf
EndIf
Case -3
Exit
EndSwitch
EndSwitch
WEnd```

##### Share on other sites

```Global \$sU = StringLeft(StringLower(StringStripWS(InputBox("Paper, Rock, Scissors - Challenge", "Type paper/rock/scissors"), 8)), 1)
If StringRegExp(\$sU, "^[prs]", 1) = 0 Then Exit MsgBox("", "", "Error")
Global \$sC = (Random(0, 1, 1) ? (Random(0, 1, 1) ? (Random(0, 1, 1) ? "p" : "r") : "s") : (Random(0, 1, 1) ? (Random(0, 1, 1) ? "p" : "r") : "s"))
Select
Case \$sU = \$sC
MsgBox("", \$sU & " vs. " & \$sC, "tie")
Case (\$sU = "p" And \$sC = "r") Or (\$sU = "r" And \$sC = "s") Or (\$sU = "s" And \$sC = "p")
MsgBox("", \$sU & " vs. " & \$sC, "You win")
Case (\$sC = "p" And \$sU = "r") Or (\$sC = "r" And \$sU = "s") Or (\$sC = "s" And \$sU = "p")
MsgBox("", \$sU & " vs. " & \$sC, "You lose")
EndSelect```

##### Share on other sites

Great. That's on the right track. Thanks for your submission.

Edit: I notice just by looking it will accept price/scary/rubber as valid choices too.

Edited by guinness

##### Share on other sites

Oh, and before someone accuses me of being lazy I already have something, but am laying off presenting it until entries stop.

##### Share on other sites

You said nothing about good coding practice

Edit: I swiped gottygolly's code

Test.au3

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

##### Share on other sites

guinness,

My offering:

```\$iUser = String(InputBox("Choose your move", "0 = Paper; 1 = Rock, 2 = Scissors"))
If \$iUser = "0" Or \$iUser = "1" Or \$iUser = "2" Then
\$iOpponent = Random(0, 2, 1)
ConsoleWrite("You: " & \$iUser & " vs " & \$iOpponent & " :Computer = " & ( (\$iUser = \$iOpponent) ? ("A Draw") : ( (\$iOpponent = Mod(\$iUser + 2, 3)) ? ("Comp Wins") : ("You Win")  ) ) & @CRLF)
EndIf```
If you want it a bit more "user-friendly" then you need an extra line:

```Global \$aItem[3] = ["Paper", "Rock", "Scissors"]
\$iUser = String(InputBox("Choose your move", "0 = Paper; 1 = Rock, 2 = Scissors"))
If \$iUser = "0" Or \$iUser = "1" Or \$iUser = "2" Then
\$iOpponent = Random(0, 2, 1)
ConsoleWrite("You: " & \$aItem[\$iUser] & " vs " & \$aItem[\$iOpponent] & " :Computer = " & ( (\$iUser = \$iOpponent) ? ("A Draw") : ( (\$iOpponent = Mod(\$iUser + 2, 3)) ? ("Comp Wins") : ("You Win")  ) ) & @CRLF)
EndIf```
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Now we're talking Thanks Melba23 & rindeal.

You can have multiple selections but I will take your last submission as being your final.

##### Share on other sites

`Global \$aItem[3] = ["Paper", "Rock", "Scissors"], \$iUser = String(InputBox("Choose your move", "0 = Paper; 1 = Rock, 2 = Scissors")),\$iOpponent = Random(0, 2, 1), \$sResult=(\$iUser = "0" Or \$iUser = "1" Or \$iUser = "2") ? MsgBox("", "You: " & \$aItem[\$iUser] & " vs " & \$aItem[\$iOpponent] & " :Computer", ((\$iUser = \$iOpponent) ? ("A Draw") : ((\$iOpponent = Mod(\$iUser + 2, 3)) ? ("Comp Wins") : ("You Win"))) & @CRLF) : MsgBox("", "", "Error")`

I've just found out that it can be oneliner

Edited by rindeal

##### Share on other sites

I did something similar to M23.

```\$player = InputBox("","Rock (4), Paper (2), or Scissors (1)")
\$computer = Random(1,3,1)
If \$computer=3 Then \$computer=4
\$test = \$player - \$computer
MsgBox(0,"","You " & ((\$test = 0)?("tie") :((\$test=1)or(\$test=2)or(\$test=-3))?("win") :("lose")))```

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

##### Share on other sites
`Global \$aItem[3] = ["Paper", "Rock", "Scissors"], \$iUser = String(InputBox("Choose your move", "0 = Paper; 1 = Rock, 2 = Scissors")),\$iOpponent = Random(0, 2, 1), \$sResult=(\$iUser = "0" Or \$iUser = "1" Or \$iUser = "2") ? MsgBox("", "You: " & \$aItem[\$iUser] & " vs " & \$aItem[\$iOpponent] & " :Computer", ((\$iUser = \$iOpponent) ? ("A Draw") : ((\$iOpponent = Mod(\$iUser + 2, 3)) ? ("Comp Wins") : ("You Win"))) & @CRLF) : MsgBox("", "", "Error")`

I've just found out that it can be oneliner

That's what I did kind of. Though you're lucky AutoIt uses a variant datatype as in C# that would across more than one line (see Dev section for my C# submission.)

Edited by guinness

##### Share on other sites

Note: Mat can't play in this game. Sorry!

(he would win too!)

##### Share on other sites

This can be shorter but I wanted to make a square :

```Local \$0 = "rock", \$1 = "paper", _
\$2 = "scissors", \$4 = 0, \$5 = 1, _
\$8 = 2, \$z = Random(0, 2, 1), \$x _
= InputBox("choice ?", \$0 & ", " _
& \$1 & " or " & \$2 & " :"), \$v = _
Eval, \$w = \$v(StringLen(\$x)), \$u _
= MsgBox(0, "res", \$x & " vs " & _
Eval(\$z) & ":" & ((\$x = Eval(\$z) _
) ? " draw" : (\$w + 1 = \$z Or \$w _
-2 = \$z) ? " lose" : " win"))```

I can't do better than Melba with his Mod trick

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

##### Share on other sites

I win!

One line and one declaration only! (I said check out my C# example!.)

```#AutoIt3Wrapper_Run_Au3Check=N
Or this even >>

```#AutoIt3Wrapper_Run_Au3Check=N
What I mean by the one declaration is I am only using one variable, whereas I see most are taking advantage of AutoIt's use of the variant datatype. Edited by guinness

##### Share on other sites

Wow, I didn't know that this

`\$aArray[]=["foo",\$aArray[0]]`

is possible...

##### Share on other sites

Wow, I didn't know that this

`\$aArray[]=["foo",\$aArray[0]]`
is possible...

So what do you say? Fill in the missing word... guinness you are ..... Edited by guinness

##### Share on other sites

guinness,

With your 2nd version using a regex you'd rather use this one

`StringRegExp(\$aAnswers[0], "([^123])|(.{2,})")`

Cause if you actually type "12" you get a nice crash

##### Share on other sites

guinness,

With your 2nd version using a regex you'd rather use this one

`StringRegExp(\$aAnswers[0], "([^123])|(.{2,})")`
Cause if you actually type "12" you get a nice crash

Fixed. See my regexp as your version is a little over complex.

Edit: StringRegExp(\$aAnswers[0], "^[123]{1}\$") = 0 is all it should be.

Edited by guinness

## Create an account

Register a new account

• ### Similar Content

• By guinness
Last time I had a >Paper, Rock, Scissors - Challenge and now I think it's time for a new one.
Challenge: Create a lottery game with the least amount of lines in AutoIt. The user should be prompted to enter a string of 7 lottery numbers from 1 to 49 and have either a comma (,) or single space as the delimiter e.g. 1,5,10,37,15,43,2. It should be checked against 7 random numbers to see if they all match. If all 7 match then they win, otherwise they lose. The numbers should be valid and contain zero duplicates in both the user's choice and computer generated sequence.
Good luck.

Note: I am not playing this time around as some people said it was unfair that I took part last time. Oh and serious contenders need only apply please.
PS. I created this in C# using 4 lines of code when the idea was presented to me by a mate...so yeah...good luck! -_0
NO /AutoItExecuteScript workarounds.
• By guinness
Created in only 2 lines. The first Console.WriteLine() is not included as it's there just to report the valid values.