Rock-paper-scissors: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 3,111:
p1.challengeOther(p2); //true (Win)
</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
# To quit, enter a blank line or type "q" or "quit"
def choices: ["r", "p", "s", "q"];
 
# The main source of entropy for this pseudo pseudo random number generator is the player :-)
# PRN in range(0;3)
def rand: now * 100 | floor | tostring[-2:] | tonumber % 3;
 
def tallies:
{pWins: 0, # player wins
cWins: 0, # computer wins
draws: 0, # neither wins
games: 0, # games played
pFreqs: [0, 0, 0] # player frequencies for each choice (rps)
};
 
# Update the tallies and populate .emit
def update($pChoice; $cChoice):
if $pChoice == "r" and $cChoice == "s"
then .emit += "Rock breaks scissors - You win!"
| .pWins += 1
elif $pChoice == "p" and $cChoice == "r"
then .emit += "Paper covers rock - You win!"
| .pWins += 1
elif $pChoice == "s" and $cChoice == "p"
then .emit += "Scissors cut paper - You win!"
| .pWins += 1
elif $pChoice == "s" and $cChoice == "r"
then .emit += "Rock breaks scissors - Computer wins!"
| .cWins += 1
elif $pChoice == "r" and $cChoice == "p"
then .emit += "Paper covers rock - Computer wins!"
| .cWins += 1
elif $pChoice == "p" and $cChoice == "s"
then .emit += "Scissors cut paper - Computer wins!"
| .cWins += 1
else .emit += "It's a draw!"
| .draws += 1
end
| .pFreqs[choices|index($pChoice)] += 1
| .games += 1 ;
 
def printScore:
"Wins: You \(.pWins), Computer \(.cWins), Neither \(.draws)\n";
 
 
def getComputerChoice:
# make a completely random choice until 3 games have been played
if .games < 3 then choices[rand]
else .games as $games
| (.pFreqs | map(3 * . / $games)) as $pFreqs
| rand as $num
| if $num < $pFreqs[0] then "p"
elif $num < $pFreqs[0] + $pFreqs[1] then "s"
else "r"
end
end ;
 
# Get player's choice (empty line or q or quit to quit).
# Return false if the choice is not recognized.
def pChoice:
(first(inputs) // null) as $in
| if $in == null or $in == "q" or $in == "quit" then null
else ($in|ascii_downcase) as $in
| if any(choices[]; . == $in) then $in
else false
end
end;
 
# Solicit input
def prompt:
if .games == 0
then "Enter: (r)ock, (p)aper, (s)cissors or (q)uit"
else printScore + "---\n\nYour choice r/p/s/q : "
end;
 
def play:
label $out
| foreach range(1; infinite) as $i (tallies ;
# if .prompt then it is time to get input:
if .prompt
then pChoice as $pChoice
| if $pChoice == null
then .prompt = false
| .emit = "OK, quitting", break $out
elif $pChoice == false
then .emit = "Valid responses are one of r p s q\nPlease try again."
else getComputerChoice as $cChoice
| .prompt = false
| .emit = "Computer's choice : \($cChoice)\n"
| update($pChoice; $cChoice)
end
else .prompt = prompt
| .emit = null
end )
 
| select(.emit).emit,
select(.prompt).prompt ;
 
play
</syntaxhighlight>
 
'''Invocation:''' jq -nrR -f rock-paper-scissors.jq
 
'''Sample game'''
<pre>
Enter: (r)ock, (p)aper, (s)cissors or (q)uit
?
Valid responses are one of r p s q
Please try again.
Enter: (r)ock, (p)aper, (s)cissors or (q)uit
r
Computer's choice : r
It's a draw!
Wins: You 0, Computer 0, Neither 1
---
 
Your choice r/p/s/q :
p
Computer's choice : s
Scissors cut paper - Computer wins!
Wins: You 0, Computer 1, Neither 1
---
 
Your choice r/p/s/q :
s
Computer's choice : s
It's a draw!
Wins: You 0, Computer 1, Neither 2
---
 
Your choice r/p/s/q :
OK, quitting
</pre>
 
=={{header|Julia}}==
Line 4,719 ⟶ 4,856:
NPC = randint(0, 2)
print('The computer played ' + hands[NPC] + '; ' + judge[YOU-NPC])</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery">
[ 0 ] is rock ( --> n )
[ 1 ] is paper ( --> n )
[ 2 ] is scissor ( --> n )
 
[ $ "Choose rock, paper or scissors: "
input cr
trim reverse trim reverse
$ "" swap witheach [ lower join ]
dup $ "rock" = iff
[ drop rock ] done
dup $ "paper" = iff
[ drop paper ] done
$ "scissors" = iff
scissor done
again ] is player ( --> n )
 
[ stack 1 ] is rocks ( --> s )
[ stack 1 ] is papers ( --> s )
[ stack 1 ] is scissors ( --> s )
 
[ 1 swap
[ table rocks papers scissors ]
do tally ] is notechoice ( n --> )
 
[ 0 ' [ rocks papers scissors ]
witheach [ share + ]
random
dup rocks share < iff
[ drop paper ] done
rocks share -
papers share < iff
scissor done
rock ] is computer ( --> n )
 
[ say "Computer chose "
[ table rock paper scissors ]
echo say "." cr ] is echomove ( n --> )
 
[ [ table
[ table 0 1 2 ]
[ table 2 0 1 ]
[ table 1 2 0 ] ] do ] is result ( n n --> n )
 
[ [ table
$ "It's a draw."
$ "Computer wins."
$ "Player wins." ]
do echo$ cr cr ] is announce ( n --> )
 
[ stack 0 ] is draws ( --> s )
[ stack 0 ] is cwins ( --> s )
[ stack 0 ] is pwins ( --> s )
 
[ [ table draws cwins pwins ]
1 swap tally ] is keepscore ( n --> )
 
[ say "Computer: " cwins share echo
say " Player: " pwins share echo
say " Draws: " draws share echo
cr cr ] is scoreboard ( --> )
 
[ ' [ rocks papers scissors ]
witheach [ 1 swap replace ]
' [ draws cwins pwins ]
witheach [ 0 swap replace ] ] is initialise ( --> )
 
[ 0
[ drop
$ "How many games? " input
trim reverse trim reverse
$->n until ]
cr ] is games ( --> n )
 
[ initialise
games times
[ computer
player dup notechoice
over echomove
result dup announce
keepscore
scoreboard ] ] is play ( --> )</syntaxhighlight>
 
{{out}}
 
As a dialogue in the Quackery shell.
 
<pre>/O> play
...
How many games? 3
 
Choose rock, paper or scissors: rock
 
Computer chose rock.
It's a draw.
 
Computer: 0 Player: 0 Draws: 1
 
Choose rock, paper or scissors: paper
 
Computer chose scissors.
Computer wins.
 
Computer: 1 Player: 0 Draws: 1
 
Choose rock, paper or scissors: scissors
 
Computer chose paper.
Player wins.
 
Computer: 1 Player: 1 Draws: 1
 
 
Stack empty.
 
/O></pre>
 
=={{header|R}}==
Line 6,507 ⟶ 6,763:
done</syntaxhighlight>
 
=={{header|V (Vlang)}}==
Semi-translation of Go version:
 
<syntaxhighlight lang="v (vlang)">
import rand
import os
Line 6,675 ⟶ 6,931:
{{libheader|Wren-str}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./str" for Str
import "./ioutil" for Input
 
var choices = "rpsq"
9,476

edits