Rock-paper-scissors: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|NS-HUBASIC}}: NS-HUBASIC example added)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 491:
 
</lang>
 
=={{header|Bash}}==
<lang bash>#!/bin/bash
echo "What will you choose? [rock/paper/scissors]"
read response
aiThought=$(echo $[ 1 + $[ RANDOM % 3 ]])
case $aiThought in
1) aiResponse="rock" ;;
2) aiResponse="paper" ;;
3) aiResponse="scissors" ;;
esac
echo "AI - $aiResponse"
responses="$response$aiResponse"
case $responses in
rockrock) isTie=1 ;;
rockpaper) playerWon=0 ;;
rockscissors) playerWon=1 ;;
paperrock) playerWon=1 ;;
paperpaper) isTie=1 ;;
paperscissors) playerWon=0 ;;
scissorsrock) playerWon=0 ;;
scissorspaper) playerWon=1 ;;
scissorsscissors) isTie=1 ;;
esac
if [[ $isTie == 1 ]] ; then echo "It's a tie!" && exit 1 ; fi
if [[ $playerWon == 0 ]] ; then echo "Sorry, $aiResponse beats $response , try again.." && exit 1 ; fi
if [[ $playerWon == 1 ]] ; then echo "Good job, $response beats $aiResponse!" && exit 1 ; fi</lang>
 
=={{header|BASIC}}==
Line 579 ⟶ 606:
You chose: 60 0 1
I chose: 2 55 4
 
=={{header|Bash}}==
<lang bash>#!/bin/bash
echo "What will you choose? [rock/paper/scissors]"
read response
aiThought=$(echo $[ 1 + $[ RANDOM % 3 ]])
case $aiThought in
1) aiResponse="rock" ;;
2) aiResponse="paper" ;;
3) aiResponse="scissors" ;;
esac
echo "AI - $aiResponse"
responses="$response$aiResponse"
case $responses in
rockrock) isTie=1 ;;
rockpaper) playerWon=0 ;;
rockscissors) playerWon=1 ;;
paperrock) playerWon=1 ;;
paperpaper) isTie=1 ;;
paperscissors) playerWon=0 ;;
scissorsrock) playerWon=0 ;;
scissorspaper) playerWon=1 ;;
scissorsscissors) isTie=1 ;;
esac
if [[ $isTie == 1 ]] ; then echo "It's a tie!" && exit 1 ; fi
if [[ $playerWon == 0 ]] ; then echo "Sorry, $aiResponse beats $response , try again.." && exit 1 ; fi
if [[ $playerWon == 1 ]] ; then echo "Good job, $response beats $aiResponse!" && exit 1 ; fi</lang>
 
=={{header|Batch File}}==
Line 918:
}
</lang>
 
=={{header|C++}}==
Version using Additional Weapons
 
<lang cpp>
#include <windows.h>
#include <iostream>
#include <string>
 
//-------------------------------------------------------------------------------
using namespace std;
 
//-------------------------------------------------------------------------------
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C };
enum indexes { PLAYER, COMPUTER, DRAW };
 
//-------------------------------------------------------------------------------
class stats
{
public:
stats() : _draw( 0 )
{
ZeroMemory( _moves, sizeof( _moves ) );
ZeroMemory( _win, sizeof( _win ) );
}
void draw() { _draw++; }
void win( int p ) { _win[p]++; }
void move( int p, int m ) { _moves[p][m]++; }
int getMove( int p, int m ) { return _moves[p][m]; }
string format( int a )
{
char t[32];
wsprintf( t, "%.3d", a );
string d( t );
return d;
}
 
void print()
{
string d = format( _draw ),
pw = format( _win[PLAYER] ), cw = format( _win[COMPUTER] ),
pr = format( _moves[PLAYER][ROCK] ), cr = format( _moves[COMPUTER][ROCK] ),
pp = format( _moves[PLAYER][PAPER] ), cp = format( _moves[COMPUTER][PAPER] ),
ps = format( _moves[PLAYER][SCISSORS] ), cs = format( _moves[COMPUTER][SCISSORS] ),
pl = format( _moves[PLAYER][LIZARD] ), cl = format( _moves[COMPUTER][LIZARD] ),
pk = format( _moves[PLAYER][SPOCK] ), ck = format( _moves[COMPUTER][SPOCK] );
 
system( "cls" );
cout << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| PLAYER | " << pw << " | | " << pr << " | " << pp << " | " << ps << " | " << pl << " | " << pk << " |" << endl;
cout << "+----------+-------+ " << d << " +--------+---------+----------+--------+---------+" << endl;
cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << endl << endl;
 
system( "pause" );
 
}
 
private:
int _moves[2][MX_C], _win[2], _draw;
};
//-------------------------------------------------------------------------------
class rps
{
private:
int makeMove()
{
int total = 0, r, s;
for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );
r = rand() % total;
 
for( int i = ROCK; i < SCISSORS; i++ )
{
s = statistics.getMove( PLAYER, i );
if( r < s ) return ( i + 1 );
r -= s;
}
 
return ROCK;
}
 
void printMove( int p, int m )
{
if( p == COMPUTER ) cout << "My move: ";
else cout << "Your move: ";
 
switch( m )
{
case ROCK: cout << "ROCK\n"; break;
case PAPER: cout << "PAPER\n"; break;
case SCISSORS: cout << "SCISSORS\n"; break;
case LIZARD: cout << "LIZARD\n"; break;
case SPOCK: cout << "SPOCK\n";
}
}
 
public:
rps()
{
checker[ROCK][ROCK] = 2; checker[ROCK][PAPER] = 1; checker[ROCK][SCISSORS] = 0; checker[ROCK][LIZARD] = 0; checker[ROCK][SPOCK] = 1;
checker[PAPER][ROCK] = 0; checker[PAPER][PAPER] = 2; checker[PAPER][SCISSORS] = 1; checker[PAPER][LIZARD] = 1; checker[PAPER][SPOCK] = 0;
checker[SCISSORS][ROCK] = 1; checker[SCISSORS][PAPER] = 0; checker[SCISSORS][SCISSORS] = 2; checker[SCISSORS][LIZARD] = 0; checker[SCISSORS][SPOCK] = 1;
checker[LIZARD][ROCK] = 1; checker[LIZARD][PAPER] = 0; checker[LIZARD][SCISSORS] = 1; checker[LIZARD][LIZARD] = 2; checker[LIZARD][SPOCK] = 0;
checker[SPOCK][ROCK] = 0; checker[SPOCK][PAPER] = 1; checker[SPOCK][SCISSORS] = 0; checker[SPOCK][LIZARD] = 1; checker[SPOCK][SPOCK] = 2;
}
void play()
{
int p, r, m;
while( true )
{
cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
cin >> p;
if( !p || p < 0 ) break;
if( p > 0 && p < 6 )
{
p--;
cout << endl;
printMove( PLAYER, p );
statistics.move( PLAYER, p );
 
m = makeMove();
statistics.move( COMPUTER, m );
printMove( COMPUTER, m );
 
r = checker[p][m];
switch( r )
{
case DRAW:
cout << endl << "DRAW!" << endl << endl;
statistics.draw();
break;
case COMPUTER:
cout << endl << "I WIN!" << endl << endl;
statistics.win( COMPUTER );
break;
case PLAYER:
cout << endl << "YOU WIN!" << endl << endl;
statistics.win( PLAYER );
 
}
system( "pause" );
}
system( "cls" );
}
statistics.print();
}
 
private:
stats statistics;
int checker[MX_C][MX_C];
};
//-------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
srand( GetTickCount() );
rps game;
game.play();
return 0;
}
//-------------------------------------------------------------------------------
</lang>
 
Sample output:
<pre>
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
 
Your move: ROCK
My move: PAPER
 
I WIN!
 
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 3
 
Your move: PAPER
My move: SPOCK
 
YOU WIN!
 
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
 
Your move: SPOCK
My move: LIZARD
 
I WIN!
 
[...]
 
+----------+-------+--------+--------+---------+----------+--------+---------+
| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |
+----------+-------+--------+--------+---------+----------+--------+---------+
| PLAYER | 001 | | 003 | 002 | 000 | 000 | 009 |
+----------+-------+ 005 +--------+---------+----------+--------+---------+
| COMPUTER | 008 | | 000 | 005 | 000 | 001 | 008 |
+----------+-------+--------+--------+---------+----------+--------+---------+
 
</pre>
 
=={{header|C sharp}}==
Line 1,326 ⟶ 1,126:
Losses: 60
Draws: 1
</pre>
 
=={{header|C++}}==
Version using Additional Weapons
 
<lang cpp>
#include <windows.h>
#include <iostream>
#include <string>
 
//-------------------------------------------------------------------------------
using namespace std;
 
//-------------------------------------------------------------------------------
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C };
enum indexes { PLAYER, COMPUTER, DRAW };
 
//-------------------------------------------------------------------------------
class stats
{
public:
stats() : _draw( 0 )
{
ZeroMemory( _moves, sizeof( _moves ) );
ZeroMemory( _win, sizeof( _win ) );
}
void draw() { _draw++; }
void win( int p ) { _win[p]++; }
void move( int p, int m ) { _moves[p][m]++; }
int getMove( int p, int m ) { return _moves[p][m]; }
string format( int a )
{
char t[32];
wsprintf( t, "%.3d", a );
string d( t );
return d;
}
 
void print()
{
string d = format( _draw ),
pw = format( _win[PLAYER] ), cw = format( _win[COMPUTER] ),
pr = format( _moves[PLAYER][ROCK] ), cr = format( _moves[COMPUTER][ROCK] ),
pp = format( _moves[PLAYER][PAPER] ), cp = format( _moves[COMPUTER][PAPER] ),
ps = format( _moves[PLAYER][SCISSORS] ), cs = format( _moves[COMPUTER][SCISSORS] ),
pl = format( _moves[PLAYER][LIZARD] ), cl = format( _moves[COMPUTER][LIZARD] ),
pk = format( _moves[PLAYER][SPOCK] ), ck = format( _moves[COMPUTER][SPOCK] );
 
system( "cls" );
cout << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| PLAYER | " << pw << " | | " << pr << " | " << pp << " | " << ps << " | " << pl << " | " << pk << " |" << endl;
cout << "+----------+-------+ " << d << " +--------+---------+----------+--------+---------+" << endl;
cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << endl << endl;
 
system( "pause" );
 
}
 
private:
int _moves[2][MX_C], _win[2], _draw;
};
//-------------------------------------------------------------------------------
class rps
{
private:
int makeMove()
{
int total = 0, r, s;
for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );
r = rand() % total;
 
for( int i = ROCK; i < SCISSORS; i++ )
{
s = statistics.getMove( PLAYER, i );
if( r < s ) return ( i + 1 );
r -= s;
}
 
return ROCK;
}
 
void printMove( int p, int m )
{
if( p == COMPUTER ) cout << "My move: ";
else cout << "Your move: ";
 
switch( m )
{
case ROCK: cout << "ROCK\n"; break;
case PAPER: cout << "PAPER\n"; break;
case SCISSORS: cout << "SCISSORS\n"; break;
case LIZARD: cout << "LIZARD\n"; break;
case SPOCK: cout << "SPOCK\n";
}
}
 
public:
rps()
{
checker[ROCK][ROCK] = 2; checker[ROCK][PAPER] = 1; checker[ROCK][SCISSORS] = 0; checker[ROCK][LIZARD] = 0; checker[ROCK][SPOCK] = 1;
checker[PAPER][ROCK] = 0; checker[PAPER][PAPER] = 2; checker[PAPER][SCISSORS] = 1; checker[PAPER][LIZARD] = 1; checker[PAPER][SPOCK] = 0;
checker[SCISSORS][ROCK] = 1; checker[SCISSORS][PAPER] = 0; checker[SCISSORS][SCISSORS] = 2; checker[SCISSORS][LIZARD] = 0; checker[SCISSORS][SPOCK] = 1;
checker[LIZARD][ROCK] = 1; checker[LIZARD][PAPER] = 0; checker[LIZARD][SCISSORS] = 1; checker[LIZARD][LIZARD] = 2; checker[LIZARD][SPOCK] = 0;
checker[SPOCK][ROCK] = 0; checker[SPOCK][PAPER] = 1; checker[SPOCK][SCISSORS] = 0; checker[SPOCK][LIZARD] = 1; checker[SPOCK][SPOCK] = 2;
}
void play()
{
int p, r, m;
while( true )
{
cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
cin >> p;
if( !p || p < 0 ) break;
if( p > 0 && p < 6 )
{
p--;
cout << endl;
printMove( PLAYER, p );
statistics.move( PLAYER, p );
 
m = makeMove();
statistics.move( COMPUTER, m );
printMove( COMPUTER, m );
 
r = checker[p][m];
switch( r )
{
case DRAW:
cout << endl << "DRAW!" << endl << endl;
statistics.draw();
break;
case COMPUTER:
cout << endl << "I WIN!" << endl << endl;
statistics.win( COMPUTER );
break;
case PLAYER:
cout << endl << "YOU WIN!" << endl << endl;
statistics.win( PLAYER );
 
}
system( "pause" );
}
system( "cls" );
}
statistics.print();
}
 
private:
stats statistics;
int checker[MX_C][MX_C];
};
//-------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
srand( GetTickCount() );
rps game;
game.play();
return 0;
}
//-------------------------------------------------------------------------------
</lang>
 
Sample output:
<pre>
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
 
Your move: ROCK
My move: PAPER
 
I WIN!
 
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 3
 
Your move: PAPER
My move: SPOCK
 
YOU WIN!
 
What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? 1
 
Your move: SPOCK
My move: LIZARD
 
I WIN!
 
[...]
 
+----------+-------+--------+--------+---------+----------+--------+---------+
| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |
+----------+-------+--------+--------+---------+----------+--------+---------+
| PLAYER | 001 | | 003 | 002 | 000 | 000 | 009 |
+----------+-------+ 005 +--------+---------+----------+--------+---------+
| COMPUTER | 008 | | 000 | 005 | 000 | 001 | 008 |
+----------+-------+--------+--------+---------+----------+--------+---------+
 
</pre>
 
Line 1,497:
break unless game.round
end</lang>
 
 
=={{header|D}}==
Line 2,874 ⟶ 2,873:
p1.challengeOther(p2); //true (Win)
</lang>
 
=={{header|Julia}}==
<lang julia>function rps()
Line 3,715:
I won: 5, you won: 2, draws: 0
</pre>
 
=={{header|Perl 6}}==
This is slightly more complicated than it could be; it is a general case framework with input filtering. It weights the computers choices based on your previous choices. Type in at least the first two characters of your choice, or just hit enter to quit. Customize it by supplying your own <tt>%vs</tt> options/outcomes.
 
Here is standard Rock-Paper-Scissors.
 
<lang perl6>my %vs = (
options => [<Rock Paper Scissors>],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: '],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ]
}
);
 
my %choices = %vs<options>.map({; $_.substr(0,2).lc => $_ });
my $keys = %choices.keys.join('|');
my $prompt = %vs<options>.map({$_.subst(/(\w\w)/, -> $/ {"[$0]"})}).join(' ')~"? ";
my %weight = %choices.keys »=>» 1;
 
my @stats = 0,0,0;
my $round;
 
while my $player = (prompt "Round {++$round}: " ~ $prompt).lc {
$player.=substr(0,2);
say 'Invalid choice, try again.' and $round-- and next
unless $player.chars == 2 and $player ~~ /<$keys>/;
my $computer = (flat %weight.keys.map( { $_ xx %weight{$_} } )).pick;
%weight{$_.key}++ for %vs{$player}.grep( { $_.value[0] == 1 } );
my $result = %vs{$player}{$computer}[0];
@stats[$result]++;
say "You chose %choices{$player}, Computer chose %choices{$computer}.";
print %vs{$player}{$computer}[1];
print ( 'You win!', 'You Lose!','Tie.' )[$result];
say " - (W:{@stats[0]} L:{@stats[1]} T:{@stats[2]})\n",
};</lang>Example output:
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors? ro
You chose Rock, Computer chose Paper.
Paper covers Rock: You Lose! - (W:0 L:1 T:0)
 
Round 2: [Ro]ck [Pa]per [Sc]issors? pa
You chose Paper, Computer chose Scissors.
Scissors cut Paper: You Lose! - (W:0 L:2 T:0)
 
Round 3: [Ro]ck [Pa]per [Sc]issors? pa
You chose Paper, Computer chose Scissors.
Scissors cut Paper: You Lose! - (W:0 L:3 T:0)
 
Round 4: [Ro]ck [Pa]per [Sc]issors? ro
You chose Rock, Computer chose Rock.
Tie. - (W:0 L:3 T:1)
 
Round 5: [Ro]ck [Pa]per [Sc]issors? sc
You chose Scissors, Computer chose Scissors.
Tie. - (W:0 L:3 T:2)
...</pre>
 
Here is example output from the same code only with a different %vs data structure implementing [http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock Rock-Paper-Scissors-Lizard-Spock].
 
<lang perl6>my %vs = (
options => [<Rock Paper Scissors Lizard Spock>],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ],
li => [ 0, 'Rock crushes Lizard: ' ],
sp => [ 1, 'Spock vaporizes Rock: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ],
li => [ 1, 'Lizard eats Paper: ' ],
sp => [ 0, 'Paper disproves Spock: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: ' ],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ],
li => [ 0, 'Scissors decapitate Lizard: '],
sp => [ 1, 'Spock smashes Scissors: ' ]
},
li => {
ro => [ 1, 'Rock crushes Lizard: ' ],
pa => [ 0, 'Lizard eats Paper: ' ],
sc => [ 1, 'Scissors decapitate Lizard: '],
li => [ 2, '' ],
sp => [ 0, 'Lizard poisons Spock: ' ]
},
sp => {
ro => [ 0, 'Spock vaporizes Rock: ' ],
pa => [ 1, 'Paper disproves Spock: ' ],
sc => [ 0, 'Spock smashes Scissors: ' ],
li => [ 1, 'Lizard poisons Spock: ' ],
sp => [ 2, '' ]
}
);</lang>
 
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li
You chose Lizard, Computer chose Scissors.
Scissors decapitate Lizard: You Lose! - (W:0 L:1 T:0)
 
Round 2: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? sp
You chose Spock, Computer chose Paper.
Paper disproves Spock: You Lose! - (W:0 L:2 T:0)
 
Round 3: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? ro
You chose Rock, Computer chose Lizard.
Rock crushes Lizard: You Win! - (W:1 L:2 T:0)
 
Round 4: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? ro
You chose Rock, Computer chose Scissors.
Rock smashes Scissors: You win! - (W:2 L:2 T:0)
 
Round 5: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li
You chose Lizard, Computer chose Paper.
Lizard eats Paper: You win! - (W:3 L:2 T:0)
...</pre>
 
=={{header|Phix}}==
Line 4,282 ⟶ 4,154:
NPC = randint(0, 2)
print('The computer played ' + hands[NPC] + '; ' + judge[YOU-NPC])</lang>
 
=={{header|Racket}}==
<lang racket>
#lang racket
(require math)
 
(define history (make-hash '((paper . 1) (scissors . 1) (rock . 1))))
(define total 3)
 
(define (update-history! human-choice)
(set! total (+ total 1))
(hash-update! history human-choice add1 0))
 
(define (pick-one)
(sample
(discrete-dist '(paper scissors rock)
(map (λ (x) (hash-ref history x))
'(scissors paper rock)))))
 
(define (find-winner computer human)
(define order '(scissors paper rock scissors))
(cond
[(eq? computer human) 'none]
[(eq? (second (member computer order)) human) 'computer]
[ 'human]))
 
(define (game-loop)
(define computer-choice (pick-one))
(define human-choice (read))
(define winner (find-winner computer-choice human-choice))
(update-history! human-choice)
(displayln (~a "Computer picked " computer-choice ", "
"human picked " human-choice ", "
winner " wins."))
(game-loop))
 
(game-loop)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
This is slightly more complicated than it could be; it is a general case framework with input filtering. It weights the computers choices based on your previous choices. Type in at least the first two characters of your choice, or just hit enter to quit. Customize it by supplying your own <tt>%vs</tt> options/outcomes.
 
Here is standard Rock-Paper-Scissors.
 
<lang perl6>my %vs = (
options => [<Rock Paper Scissors>],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: '],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ]
}
);
 
my %choices = %vs<options>.map({; $_.substr(0,2).lc => $_ });
my $keys = %choices.keys.join('|');
my $prompt = %vs<options>.map({$_.subst(/(\w\w)/, -> $/ {"[$0]"})}).join(' ')~"? ";
my %weight = %choices.keys »=>» 1;
 
my @stats = 0,0,0;
my $round;
 
while my $player = (prompt "Round {++$round}: " ~ $prompt).lc {
$player.=substr(0,2);
say 'Invalid choice, try again.' and $round-- and next
unless $player.chars == 2 and $player ~~ /<$keys>/;
my $computer = (flat %weight.keys.map( { $_ xx %weight{$_} } )).pick;
%weight{$_.key}++ for %vs{$player}.grep( { $_.value[0] == 1 } );
my $result = %vs{$player}{$computer}[0];
@stats[$result]++;
say "You chose %choices{$player}, Computer chose %choices{$computer}.";
print %vs{$player}{$computer}[1];
print ( 'You win!', 'You Lose!','Tie.' )[$result];
say " - (W:{@stats[0]} L:{@stats[1]} T:{@stats[2]})\n",
};</lang>Example output:
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors? ro
You chose Rock, Computer chose Paper.
Paper covers Rock: You Lose! - (W:0 L:1 T:0)
 
Round 2: [Ro]ck [Pa]per [Sc]issors? pa
You chose Paper, Computer chose Scissors.
Scissors cut Paper: You Lose! - (W:0 L:2 T:0)
 
Round 3: [Ro]ck [Pa]per [Sc]issors? pa
You chose Paper, Computer chose Scissors.
Scissors cut Paper: You Lose! - (W:0 L:3 T:0)
 
Round 4: [Ro]ck [Pa]per [Sc]issors? ro
You chose Rock, Computer chose Rock.
Tie. - (W:0 L:3 T:1)
 
Round 5: [Ro]ck [Pa]per [Sc]issors? sc
You chose Scissors, Computer chose Scissors.
Tie. - (W:0 L:3 T:2)
...</pre>
 
Here is example output from the same code only with a different %vs data structure implementing [http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock Rock-Paper-Scissors-Lizard-Spock].
 
<lang perl6>my %vs = (
options => [<Rock Paper Scissors Lizard Spock>],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ],
li => [ 0, 'Rock crushes Lizard: ' ],
sp => [ 1, 'Spock vaporizes Rock: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ],
li => [ 1, 'Lizard eats Paper: ' ],
sp => [ 0, 'Paper disproves Spock: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: ' ],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ],
li => [ 0, 'Scissors decapitate Lizard: '],
sp => [ 1, 'Spock smashes Scissors: ' ]
},
li => {
ro => [ 1, 'Rock crushes Lizard: ' ],
pa => [ 0, 'Lizard eats Paper: ' ],
sc => [ 1, 'Scissors decapitate Lizard: '],
li => [ 2, '' ],
sp => [ 0, 'Lizard poisons Spock: ' ]
},
sp => {
ro => [ 0, 'Spock vaporizes Rock: ' ],
pa => [ 1, 'Paper disproves Spock: ' ],
sc => [ 0, 'Spock smashes Scissors: ' ],
li => [ 1, 'Lizard poisons Spock: ' ],
sp => [ 2, '' ]
}
);</lang>
 
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li
You chose Lizard, Computer chose Scissors.
Scissors decapitate Lizard: You Lose! - (W:0 L:1 T:0)
 
Round 2: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? sp
You chose Spock, Computer chose Paper.
Paper disproves Spock: You Lose! - (W:0 L:2 T:0)
 
Round 3: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? ro
You chose Rock, Computer chose Lizard.
Rock crushes Lizard: You Win! - (W:1 L:2 T:0)
 
Round 4: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? ro
You chose Rock, Computer chose Scissors.
Rock smashes Scissors: You win! - (W:2 L:2 T:0)
 
Round 5: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li
You chose Lizard, Computer chose Paper.
Lizard eats Paper: You win! - (W:3 L:2 T:0)
...</pre>
 
=={{header|Rascal}}==
Line 4,334 ⟶ 4,373:
rascal>RPS("Rock")
str: "Computer played Paper. Paper wins!"</lang>
 
=={{header|Racket}}==
<lang racket>
#lang racket
(require math)
 
(define history (make-hash '((paper . 1) (scissors . 1) (rock . 1))))
(define total 3)
 
(define (update-history! human-choice)
(set! total (+ total 1))
(hash-update! history human-choice add1 0))
 
(define (pick-one)
(sample
(discrete-dist '(paper scissors rock)
(map (λ (x) (hash-ref history x))
'(scissors paper rock)))))
 
(define (find-winner computer human)
(define order '(scissors paper rock scissors))
(cond
[(eq? computer human) 'none]
[(eq? (second (member computer order)) human) 'computer]
[ 'human]))
 
(define (game-loop)
(define computer-choice (pick-one))
(define human-choice (read))
(define winner (find-winner computer-choice human-choice))
(update-history! human-choice)
(displayln (~a "Computer picked " computer-choice ", "
"human picked " human-choice ", "
winner " wins."))
(game-loop))
 
(game-loop)
</lang>
 
=={{header|REXX}}==
Line 5,111 ⟶ 5,112:
4:1 Play: q
</pre>
 
 
=={{header|SuperCollider}}==
Line 5,225:
}
</pre>
 
=={{header|Swift}}==
<lang Swift>enum Choice: CaseIterable {
Line 5,418 ⟶ 5,419:
Bye!
</pre>
 
=={{header|TI-83 BASIC}}==
<lang ti83b>PROGRAM:RPS
:{0,0,0}→L1
:{0,0,0}→L2
:Lbl ST
:Disp "R/P/S"
:Disp "1/2/3"
:Lbl EC
:Input A
:If A>3 or A<1
:Then
:Goto NO
:End
:randInt(1,3+L1(1)+L1(2)+L1(3)→C
:If C≤1+L1(1)
:Then
:2→B
:Goto NS
:End
:If C>2+L1(2)
:Then
:1→B
:Else
:3→B
:End
:Lbl NS
:L1(A)+1→L1(A)
:If A=B
:Then
:Disp "TIE GAME"
:L2(3)+1→L2(3)
:Goto TG
:End
:If (A=1 and B=2) or (A=2 and B=3) or (A=3 and B=1)
:Then
:Disp "I WIN"
:L2(1)+1→L2(1)
:Else
:Disp "YOU WIN"
:L2(2)+1→L2(2)
:End
:Lbl TG
:Disp "PLAY AGAIN?"
:Input Str1
:If Str1="YES"
:Then
:ClrHome
:Goto ST
:Else
:Goto EN
:End
:Lbl NO
:ClrHome
:Pause "PICK 1,2, or 3"
:ClrHome
:Goto EC
:Lbl EN
:ClrHome
:Disp "I WON:"
:Disp L2(1)
:Disp "YOU WON:"
:Disp L2(2)
:Disp "WE TIED:"
:Disp L2(3)
:Disp "BYE"
</lang>
 
{{omit from|GUISS}}
 
=={{header|TorqueScript}}==
Line 5,579 ⟶ 5,649:
 
=> You chose rock computer chose paper, you lose!
 
=={{header|TI-83 BASIC}}==
<lang ti83b>PROGRAM:RPS
:{0,0,0}→L1
:{0,0,0}→L2
:Lbl ST
:Disp "R/P/S"
:Disp "1/2/3"
:Lbl EC
:Input A
:If A>3 or A<1
:Then
:Goto NO
:End
:randInt(1,3+L1(1)+L1(2)+L1(3)→C
:If C≤1+L1(1)
:Then
:2→B
:Goto NS
:End
:If C>2+L1(2)
:Then
:1→B
:Else
:3→B
:End
:Lbl NS
:L1(A)+1→L1(A)
:If A=B
:Then
:Disp "TIE GAME"
:L2(3)+1→L2(3)
:Goto TG
:End
:If (A=1 and B=2) or (A=2 and B=3) or (A=3 and B=1)
:Then
:Disp "I WIN"
:L2(1)+1→L2(1)
:Else
:Disp "YOU WIN"
:L2(2)+1→L2(2)
:End
:Lbl TG
:Disp "PLAY AGAIN?"
:Input Str1
:If Str1="YES"
:Then
:ClrHome
:Goto ST
:Else
:Goto EN
:End
:Lbl NO
:ClrHome
:Pause "PICK 1,2, or 3"
:ClrHome
:Goto EC
:Lbl EN
:ClrHome
:Disp "I WON:"
:Disp L2(1)
:Disp "YOU WON:"
:Disp L2(2)
:Disp "WE TIED:"
:Disp L2(3)
:Disp "BYE"
</lang>
 
{{omit from|GUISS}}
 
=={{header|uBasic/4tH}}==
10,333

edits