Rock-paper-scissors: Difference between revisions

moving icon/unicon
(moving icon/unicon)
Line 265:
My wins: 4
Your wins: 1</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
The key to this comes down to two structures and two lines of code. The player history ''historyP'' is just an ordered list of every player turn and provides the weight for the random selection. The ''beats'' list is used to rank moves and to choose the move that would beat the randomly selected move.
<lang Icon>link printf
 
procedure main()
 
printf("Welcome to Rock, Paper, Scissors.\n_
Rock beats scissors, Scissors beat paper, and Paper beats rock.\n\n")
 
historyP := ["rock","paper","scissors"] # seed player history
winP := winC := draws := 0 # totals
beats := ["rock","scissors","paper","rock"] # what beats what 1 apart
 
repeat {
printf("Enter your choice or rock(r), paper(p), scissors(s) or quit(q):")
turnP := case map(read()) of {
"q"|"quit": break
"r"|"rock": "rock"
"p"|"paper": "paper"
"s"|"scissors": "scissors"
default: printf(" - invalid choice.\n") & next
}
 
turnC := beats[(?historyP == beats[i := 2 to *beats],i-1)] # choose move
put(historyP,turnP) # record history
printf("You chose %s, computer chose %s",turnP,turnC)
 
(beats[p := 1 to *beats] == turnP) &
(beats[c := 1 to *beats] == turnC) & (abs(p-c) <= 1) # rank play
if p = c then
printf(" - draw (#%d)\n",draws +:= 1 )
else if p > c then
printf(" - player win(#%d)\n",winP +:= 1)
else
printf(" - computer win(#%d)\n",winC +:= 1)
}
 
printf("\nResults:\n %d rounds\n %d Draws\n %d Computer wins\n %d Player wins\n",
winP+winC+draws,draws,winC,winP)
end</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides printf]
 
Sample output:<pre>Welcome to Rock, Paper, Scissors.
Rock beats scissors, Scissors beat paper, and Paper beats rock.
 
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):s
You chose scissors, computer chose scissors - draw (#1)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):p
You chose paper, computer chose paper - draw (#2)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):r
You chose rock, computer chose scissors - computer win(#1)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):r
You chose rock, computer chose rock - draw (#3)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):p
You chose paper, computer chose paper - draw (#4)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):s
You chose scissors, computer chose scissors - draw (#5)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):r
You chose rock, computer chose rock - draw (#6)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):r
You chose rock, computer chose paper - player win(#1)
Enter your choice or rock(r), paper(p), scissors(s) or quit(q):q
 
Results:
8 rounds
6 Draws
1 Computer wins
1 Player wins</pre>
 
=={{header|Java}}==
Anonymous user