Rock-paper-scissors: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 29: | Line 29: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V rules = [‘rock’ = ‘paper’, ‘scissors’ = ‘rock’, ‘paper’ = ‘scissors’] |
||
V previous = [‘rock’, ‘paper’, ‘scissors’] |
V previous = [‘rock’, ‘paper’, ‘scissors’] |
||
Line 51: | Line 51: | ||
E |
E |
||
print(‘that's not a valid choice’)</ |
print(‘that's not a valid choice’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 74: | Line 74: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; with Ada.Numerics.Float_Random; |
||
procedure Rock_Paper_Scissors is |
procedure Rock_Paper_Scissors is |
||
Line 180: | Line 180: | ||
Ada.Text_IO.Put_Line(Result'Image(R) & Natural'Image(Score(R))); |
Ada.Text_IO.Put_Line(Result'Image(R) & Natural'Image(Score(R))); |
||
end loop; |
end loop; |
||
end Rock_Paper_Scissors;</ |
end Rock_Paper_Scissors;</syntaxhighlight> |
||
First and last few lines of the output of a game, where the human did permanently choose Rock: |
First and last few lines of the output of a game, where the human did permanently choose Rock: |
||
Line 228: | Line 228: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">text |
||
computer_play(record plays, record beats) |
computer_play(record plays, record beats) |
||
{ |
{ |
||
Line 298: | Line 298: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# rock/paper/scissors game # |
# rock/paper/scissors game # |
||
# counts of the number of times the player has chosen each move # |
# counts of the number of times the player has chosen each move # |
||
Line 389: | Line 389: | ||
OD; |
OD; |
||
print( ( "Thanks for a most enjoyable game", newline ) ) |
print( ( "Thanks for a most enjoyable game", newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="ahk">DllCall("AllocConsole") |
||
Write("Welcome to Rock-Paper-Scissors`nMake a choice: ") |
Write("Welcome to Rock-Paper-Scissors`nMake a choice: ") |
||
Line 436: | Line 436: | ||
Write(txt){ |
Write(txt){ |
||
FileAppend, % txt, CONOUT$ |
FileAppend, % txt, CONOUT$ |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
Line 442: | Line 442: | ||
I´ve created a GUI to play and show results, no Console Input |
I´ve created a GUI to play and show results, no Console Input |
||
< |
<syntaxhighlight lang="autoit"> |
||
RPS() |
RPS() |
||
Line 536: | Line 536: | ||
EndFunc ;==>_RPS_Eval |
EndFunc ;==>_RPS_Eval |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Bash}}== |
=={{header|Bash}}== |
||
< |
<syntaxhighlight lang="bash">#!/bin/bash |
||
echo "What will you choose? [rock/paper/scissors]" |
echo "What will you choose? [rock/paper/scissors]" |
||
read response |
read response |
||
Line 563: | Line 563: | ||
if [[ $isTie == 1 ]] ; then echo "It's a tie!" && exit 1 ; fi |
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 == 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</ |
if [[ $playerWon == 1 ]] ; then echo "Good job, $response beats $aiResponse!" && exit 1 ; fi</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
< |
<syntaxhighlight lang="qbasic">DIM pPLchoice(1 TO 3) AS INTEGER, pCMchoice(1 TO 3) AS INTEGER |
||
DIM choices(1 TO 3) AS STRING |
DIM choices(1 TO 3) AS STRING |
||
DIM playerwins(1 TO 3) AS INTEGER |
DIM playerwins(1 TO 3) AS INTEGER |
||
Line 629: | Line 629: | ||
PRINT , choices(1), choices(2), choices(3) |
PRINT , choices(1), choices(2), choices(3) |
||
PRINT "You chose:", pPLchoice(1), pPLchoice(2), pPLchoice(3) |
PRINT "You chose:", pPLchoice(1), pPLchoice(2), pPLchoice(3) |
||
PRINT " I chose:", pCMchoice(1), pCMchoice(2), pCMchoice(3)</ |
PRINT " I chose:", pCMchoice(1), pCMchoice(2), pCMchoice(3)</syntaxhighlight> |
||
A sample game: |
A sample game: |
||
Line 654: | Line 654: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 726: | Line 726: | ||
set /a "freq%choice%+=1" |
set /a "freq%choice%+=1" |
||
pause |
pause |
||
goto start</ |
goto start</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic">PRINT"Welcome to the game of rock-paper-scissors" |
||
PRINT "Each player guesses one of these three, and reveals it at the same time." |
PRINT "Each player guesses one of these three, and reveals it at the same time." |
||
PRINT "Rock blunts scissors, which cut paper, which wraps stone." |
PRINT "Rock blunts scissors, which cut paper, which wraps stone." |
||
Line 774: | Line 774: | ||
IF r%<=p%(0) THEN =1 |
IF r%<=p%(0) THEN =1 |
||
IF r%<=p%(0)+p%(1) THEN =2 |
IF r%<=p%(0)+p%(1) THEN =2 |
||
=0</ |
=0</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
Line 804: | Line 804: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 856: | Line 856: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Here's another code: (Does it using a while loop) |
Here's another code: (Does it using a while loop) |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#include <stdio.h> // Standard IO |
#include <stdio.h> // Standard IO |
||
#include <stdlib.h> // other stuff |
#include <stdlib.h> // other stuff |
||
Line 963: | Line 963: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="c sharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,139: | Line 1,139: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Sample output of first 2 and last 2 rounds when player chooses rock every turn: |
Sample output of first 2 and last 2 rounds when player chooses rock every turn: |
||
Line 1,177: | Line 1,177: | ||
Version using Additional Weapons |
Version using Additional Weapons |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,337: | Line 1,337: | ||
} |
} |
||
//------------------------------------------------------------------------------- |
//------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output: |
Sample output: |
||
Line 1,384: | Line 1,384: | ||
Code: |
Code: |
||
< |
<syntaxhighlight lang="clojure">(ns rps.core |
||
(:require [clojure.data.generators :refer [weighted]]) |
(:require [clojure.data.generators :refer [weighted]]) |
||
(:import jline.Terminal) |
(:import jline.Terminal) |
||
Line 1,422: | Line 1,422: | ||
"Rock, Paper, Scissors!" |
"Rock, Paper, Scissors!" |
||
[& args] |
[& args] |
||
(play-game {:rock 1, :paper 1, :scissors 1}))</ |
(play-game {:rock 1, :paper 1, :scissors 1}))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,436: | Line 1,436: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
Inspired by [[#Ruby]] solution, but improved to allow additional weapons |
Inspired by [[#Ruby]] solution, but improved to allow additional weapons |
||
< |
<syntaxhighlight lang="ruby"> |
||
# conventional weapons |
# conventional weapons |
||
enum Choice |
enum Choice |
||
Line 1,542: | Line 1,542: | ||
loop do |
loop do |
||
break unless game.round |
break unless game.round |
||
end</ |
end</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.random, std.string, std.conv, std.array, std.typecons; |
||
enum Choice { rock, paper, scissors } |
enum Choice { rock, paper, scissors } |
||
Line 1,602: | Line 1,602: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>rock, paper or scissors? paper |
<pre>rock, paper or scissors? paper |
||
Line 1,625: | Line 1,625: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Erlang}} |
{{trans|Erlang}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Rock_paper_scissors do |
||
def play, do: loop([1,1,1]) |
def play, do: loop([1,1,1]) |
||
Line 1,672: | Line 1,672: | ||
end |
end |
||
Rock_paper_scissors.play</ |
Rock_paper_scissors.play</syntaxhighlight> |
||
'''Sample output:''' |
'''Sample output:''' |
||
Line 1,693: | Line 1,693: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang"> |
||
-module(rps). |
-module(rps). |
||
-compile(export_all). |
-compile(export_all). |
||
Line 1,740: | Line 1,740: | ||
true -> $R |
true -> $R |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="euphoria">function weighted_rand(sequence table) |
||
integer sum,r |
integer sum,r |
||
sum = 0 |
sum = 0 |
||
Line 1,793: | Line 1,793: | ||
printf(1,"\nScore %d:%d\n",score) |
printf(1,"\nScore %d:%d\n",score) |
||
user_rec[user_action] += 1 |
user_rec[user_action] += 1 |
||
end while</ |
end while</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let random = Random () |
let random = Random () |
||
Line 1,857: | Line 1,857: | ||
game(1.0, 1.0, 1.0) |
game(1.0, 1.0, 1.0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: combinators formatting io kernel math math.ranges qw |
||
random sequences ; |
random sequences ; |
||
IN: rosetta-code.rock-paper-scissors |
IN: rosetta-code.rock-paper-scissors |
||
Line 1,903: | Line 1,903: | ||
: main ( -- ) { 0 0 0 1 1 1 } clone game drop ; |
: main ( -- ) { 0 0 0 1 1 1 } clone game drop ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,953: | Line 1,953: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth"> |
||
include random.fs |
include random.fs |
||
Line 2,037: | Line 2,037: | ||
2dup update-log 2dup print-throws |
2dup update-log 2dup print-throws |
||
determine-winner rps ; |
determine-winner rps ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,052: | Line 2,052: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Please find an example run in a GNU/linux system along with compilation instructions at the beginning of the FORTRAN 2008 source code. Following the source are examples demonstrating the effectiveness of the built in simple predictive artificial intelligence. It uses the yes utility for a constant data source. |
Please find an example run in a GNU/linux system along with compilation instructions at the beginning of the FORTRAN 2008 source code. Following the source are examples demonstrating the effectiveness of the built in simple predictive artificial intelligence. It uses the yes utility for a constant data source. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
! compilation |
! compilation |
||
! gfortran -std=f2008 -Wall -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f |
! gfortran -std=f2008 -Wall -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f |
||
Line 2,115: | Line 2,115: | ||
if (score(COMPUTER) .lt. score(HAPLESSUSER)) print*,'you won!' |
if (score(COMPUTER) .lt. score(HAPLESSUSER)) print*,'you won!' |
||
end program rpsgame |
end program rpsgame |
||
</syntaxhighlight> |
|||
</lang> |
|||
rpsgame won't play more than 30 games at a time. |
rpsgame won't play more than 30 games at a time. |
||
< |
<syntaxhighlight lang="bash"> |
||
$ yes r | ./f # rock |
$ yes r | ./f # rock |
||
rock, paper, scissors? scoring computer choice (r) and your choice (r) |
rock, paper, scissors? scoring computer choice (r) and your choice (r) |
||
Line 2,148: | Line 2,148: | ||
26.5 3.5 |
26.5 3.5 |
||
$ |
$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Yabasic}} |
{{trans|Yabasic}} |
||
< |
<syntaxhighlight lang="freebasic">Dim Shared As Byte ganador = 1, accion = 2, perdedor = 3, wp, wc |
||
Dim Shared As String word(10, 3) |
Dim Shared As String word(10, 3) |
||
For n As Byte = 0 To 9 |
For n As Byte = 0 To 9 |
||
Line 2,241: | Line 2,241: | ||
Data "Paper","disproves","Spock" |
Data "Paper","disproves","Spock" |
||
Data "Spock","vaporizes","Rock" |
Data "Spock","vaporizes","Rock" |
||
Data "Rock","blunts","Scissors"</ |
Data "Rock","blunts","Scissors"</syntaxhighlight> |
||
=={{header|GlovePIE}}== |
=={{header|GlovePIE}}== |
||
You can only press the R, P or S key to advance. |
You can only press the R, P or S key to advance. |
||
< |
<syntaxhighlight lang="glovepie">if var.end=0 then |
||
var.end=0 |
var.end=0 |
||
var.computerchoice=random(3) // 1 is rock, 2 is paper, and 3 is scissors. |
var.computerchoice=random(3) // 1 is rock, 2 is paper, and 3 is scissors. |
||
Line 2,286: | Line 2,286: | ||
endif |
endif |
||
endif |
endif |
||
endif</ |
endif</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,357: | Line 2,357: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre> |
<pre> |
||
Line 2,378: | Line 2,378: | ||
</pre> |
</pre> |
||
Additional weapons: |
Additional weapons: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,634: | Line 2,634: | ||
aw = form[ax] |
aw = form[ax] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Example game files: |
Example game files: |
||
<pre> |
<pre> |
||
Line 2,728: | Line 2,728: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import System.Random (randomRIO) |
||
data Choice |
data Choice |
||
Line 2,786: | Line 2,786: | ||
main :: IO a |
main :: IO a |
||
main = game (1, 1, 1)</ |
main = game (1, 1, 1)</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="icon">link printf |
||
procedure main() |
procedure main() |
||
Line 2,830: | Line 2,830: | ||
printf("\nResults:\n %d rounds\n %d Draws\n %d Computer wins\n %d Player wins\n", |
printf("\nResults:\n %d rounds\n %d Draws\n %d Computer wins\n %d Player wins\n", |
||
winP+winC+draws,draws,winC,winP) |
winP+winC+draws,draws,winC,winP) |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 2,863: | Line 2,863: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Rock.bas" |
||
110 RANDOMIZE |
110 RANDOMIZE |
||
120 STRING CH$(1 TO 3)*8,K$*1 |
120 STRING CH$(1 TO 3)*8,K$*1 |
||
Line 2,915: | Line 2,915: | ||
600 LET CMCHOICE=1 |
600 LET CMCHOICE=1 |
||
610 END SELECT |
610 END SELECT |
||
620 END DEF</ |
620 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">require'general/misc/prompt strings' NB. was 'misc strings' in older versions of J |
||
game=:3 :0 |
game=:3 :0 |
||
outcomes=. rps=. 0 0 0 |
outcomes=. rps=. 0 0 0 |
||
Line 2,937: | Line 2,937: | ||
end. |
end. |
||
('Draws:','My wins:',:'Your wins: '),.":,.outcomes |
('Draws:','My wins:',:'Your wins: '),.":,.outcomes |
||
)</ |
)</syntaxhighlight> |
||
Example use (playing to give the computer implementation the advantage): |
Example use (playing to give the computer implementation the advantage): |
||
< |
<syntaxhighlight lang="j"> game'' |
||
Choose Rock, Paper or Scissors: rock |
Choose Rock, Paper or Scissors: rock |
||
I choose Scissors |
I choose Scissors |
||
Line 2,960: | Line 2,960: | ||
Draws: 0 |
Draws: 0 |
||
My wins: 4 |
My wins: 4 |
||
Your wins: 1</ |
Your wins: 1</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
This could probably be made simpler, but some more complexity is necessary so that other items besides rock, paper, and scissors can be added (as school children and nerds like to do [setup for rock-paper-scissors-lizard-spock is in multi-line comments]). The method <code>getAIChoice()</code> borrows from [[#Ada|the Ada example]] in spirit, but is more generic to additional items. |
This could probably be made simpler, but some more complexity is necessary so that other items besides rock, paper, and scissors can be added (as school children and nerds like to do [setup for rock-paper-scissors-lizard-spock is in multi-line comments]). The method <code>getAIChoice()</code> borrows from [[#Ada|the Ada example]] in spirit, but is more generic to additional items. |
||
< |
<syntaxhighlight lang="java5">import java.util.Arrays; |
||
import java.util.EnumMap; |
import java.util.EnumMap; |
||
import java.util.List; |
import java.util.List; |
||
Line 3,043: | Line 3,043: | ||
return null; |
return null; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre>Make your choice: rock |
<pre>Make your choice: rock |
||
Line 3,084: | Line 3,084: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript"> |
||
const logic = { |
const logic = { |
||
rock: { w: 'scissor', l: 'paper'}, |
rock: { w: 'scissor', l: 'paper'}, |
||
Line 3,110: | Line 3,110: | ||
p1.challengeOther(p2); //true (Win) |
p1.challengeOther(p2); //true (Win) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function rps() |
||
print("Welcome to Rock, paper, scissors! Go ahead and type your pick.\n |
print("Welcome to Rock, paper, scissors! Go ahead and type your pick.\n |
||
r(ock), p(aper), or s(cissors)\n |
r(ock), p(aper), or s(cissors)\n |
||
Line 3,135: | Line 3,135: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
<pre>julia> rps() |
<pre>julia> rps() |
||
Welcome to Rock, paper, scissors! Go ahead and type your pick. |
Welcome to Rock, paper, scissors! Go ahead and type your pick. |
||
Line 3,160: | Line 3,160: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.2.10 |
||
import java.util.Random |
import java.util.Random |
||
Line 3,238: | Line 3,238: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Sample session: |
Sample session: |
||
Line 3,282: | Line 3,282: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Notes: This implementation uses the default session handling in Lasso, and assumes it's running on a web server. User choices are passed in via HTTP as GET query parameters. |
Notes: This implementation uses the default session handling in Lasso, and assumes it's running on a web server. User choices are passed in via HTTP as GET query parameters. |
||
< |
<syntaxhighlight lang="lasso">session_start('user') |
||
session_addvar('user', 'historic_choices') |
session_addvar('user', 'historic_choices') |
||
session_addvar('user', 'win_record') |
session_addvar('user', 'win_record') |
||
Line 3,350: | Line 3,350: | ||
'User: '+($win_record->find('user')->size)+br |
'User: '+($win_record->find('user')->size)+br |
||
'Tie: '+($win_record->find('tie')->size)+br |
'Tie: '+($win_record->find('tie')->size)+br |
||
^}</ |
^}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Rock Paper Scissors Quit (<- as links) |
<pre>Rock Paper Scissors Quit (<- as links) |
||
Line 3,363: | Line 3,363: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
dim rps( 2), g$( 3) |
dim rps( 2), g$( 3) |
||
Line 3,452: | Line 3,452: | ||
print " Thanks for playing!" |
print " Thanks for playing!" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
You won 3204, and I won 3669. There were 3128 draws. |
You won 3204, and I won 3669. There were 3128 draws. |
||
I AM THE CHAMPION!! |
I AM THE CHAMPION!! |
||
Line 3,481: | Line 3,481: | ||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="locobasic">10 mode 1:defint a-z:randomize time |
||
20 rps$="rps" |
20 rps$="rps" |
||
30 msg$(1)="Rock breaks scissors" |
30 msg$(1)="Rock breaks scissors" |
||
Line 3,503: | Line 3,503: | ||
210 rn=rnd*plays |
210 rn=rnd*plays |
||
220 if rn<pcf(1) then achoice=2 else if rn<pcf(1)+pcf(2) then achoice=3 else achoice=1 |
220 if rn<pcf(1) then achoice=2 else if rn<pcf(1)+pcf(2) then achoice=3 else achoice=1 |
||
230 goto 110</ |
230 goto 110</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function cpuMove() |
||
local totalChance = record.R + record.P + record.S |
local totalChance = record.R + record.P + record.S |
||
if totalChance == 0 then -- First game, unweighted random |
if totalChance == 0 then -- First game, unweighted random |
||
Line 3,565: | Line 3,565: | ||
checkWinner(cpuChoice, playerChoice) |
checkWinner(cpuChoice, playerChoice) |
||
io.write("\nPress ENTER to continue or enter 'Q' to quit . . . ") |
io.write("\nPress ENTER to continue or enter 'Q' to quit . . . ") |
||
until io.read():upper():sub(1, 1) == "Q"</ |
until io.read():upper():sub(1, 1) == "Q"</syntaxhighlight> |
||
Session in which I chose nothing but rock: |
Session in which I chose nothing but rock: |
||
<pre> |
<pre> |
||
Line 3,584: | Line 3,584: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">DynamicModule[{record, play, text = "\nRock-paper-scissors\n", |
||
choices = {"Rock", "Paper", "Scissors"}}, |
choices = {"Rock", "Paper", "Scissors"}}, |
||
Evaluate[record /@ choices] = {1, 1, 1}; |
Evaluate[record /@ choices] = {1, 1, 1}; |
||
Line 3,595: | Line 3,595: | ||
Alternatives @@ Reverse /@ Partition[choices, 2, 1, 1], |
Alternatives @@ Reverse /@ Partition[choices, 2, 1, 1], |
||
"You win.", _, "Draw."]]; |
"You win.", _, "Draw."]]; |
||
Column@{Dynamic[text], ButtonBar[# :> play[#] & /@ choices]}]</ |
Column@{Dynamic[text], ButtonBar[# :> play[#] & /@ choices]}]</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
{{trans|Prolog}} |
{{trans|Prolog}} |
||
< |
<syntaxhighlight lang="mercury">:- module rps. |
||
:- interface. |
:- interface. |
||
:- import_module io. |
:- import_module io. |
||
Line 3,664: | Line 3,664: | ||
" |
" |
||
Seed = time(NULL); |
Seed = time(NULL); |
||
").</ |
").</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import random, strutils, tables |
||
type |
type |
||
Line 3,729: | Line 3,729: | ||
echo "You win." |
echo "You win." |
||
inc yourWins |
inc yourWins |
||
echo "Total wins. You: ", yourWins, " Me: ", myWins</ |
echo "Total wins. You: ", yourWins, " Me: ", myWins</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,773: | Line 3,773: | ||
=={{header|NS-HUBASIC}}== |
=={{header|NS-HUBASIC}}== |
||
< |
<syntaxhighlight lang="ns-hubasic">10 COMPUTER=RND(3)+1 |
||
20 COMPUTER$="ROCK" |
20 COMPUTER$="ROCK" |
||
30 IF COMPUTER=2 THEN COMPUTER$="PAPER" |
30 IF COMPUTER=2 THEN COMPUTER$="PAPER" |
||
Line 3,789: | Line 3,789: | ||
150 IF HUMAN$="PAPER" AND COMPUTER=3 THEN PRINT "SCISSORS CUT PAPER";", SO YOU LOSE." |
150 IF HUMAN$="PAPER" AND COMPUTER=3 THEN PRINT "SCISSORS CUT PAPER";", SO YOU LOSE." |
||
160 IF HUMAN$="SCISSORS" AND COMPUTER=1 THEN PRINT "ROCK BLUNTS SCISSORS";", SO YOU LOSE." |
160 IF HUMAN$="SCISSORS" AND COMPUTER=1 THEN PRINT "ROCK BLUNTS SCISSORS";", SO YOU LOSE." |
||
170 IF HUMAN$="SCISSORS" AND COMPUTER=2 THEN PRINT "SCISSORS CUT PAPER, SO YOU WIN."10 COMPUTER=RND(3)+1</ |
170 IF HUMAN$="SCISSORS" AND COMPUTER=2 THEN PRINT "SCISSORS CUT PAPER, SO YOU WIN."10 COMPUTER=RND(3)+1</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang="ocaml"> |
|||
<lang OCaml> |
|||
let pf = Printf.printf ;; |
let pf = Printf.printf ;; |
||
Line 3,846: | Line 3,846: | ||
make_moves 1. 1. 1. ;; |
make_moves 1. 1. 1. ;; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">contest(rounds)={ |
||
my(v=[1,1,1],wins,losses); \\ Laplace rule |
my(v=[1,1,1],wins,losses); \\ Laplace rule |
||
for(i=1,rounds, |
for(i=1,rounds, |
||
Line 3,883: | Line 3,883: | ||
[wins,losses] |
[wins,losses] |
||
}; |
}; |
||
contest(10)</ |
contest(10)</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The program supports "--quiet" option, which makes it suppress all in-game output (useful for batch testing). At the end of a game it displays detailed statistics. |
The program supports "--quiet" option, which makes it suppress all in-game output (useful for batch testing). At the end of a game it displays detailed statistics. |
||
< |
<syntaxhighlight lang="perl"> |
||
use 5.012; |
use 5.012; |
||
use warnings; |
use warnings; |
||
Line 4,009: | Line 4,009: | ||
main(); |
main(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example input can be generated as follows: |
Example input can be generated as follows: |
||
< |
<syntaxhighlight lang="bash"> |
||
perl -e '@c=qw(r p s); for(1..10000){ print $c[ rand() < 0.75 ? 0 : int rand(2) + 1 ], "\n" }' | perl rps.pl --quiet |
perl -e '@c=qw(r p s); for(1..10000){ print $c[ rand() < 0.75 ? 0 : int rand(2) + 1 ], "\n" }' | perl rps.pl --quiet |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,061: | Line 4,061: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
< |
<syntaxhighlight lang="phix">--standard game |
||
constant rule3 = {"rock blunts scissors", |
constant rule3 = {"rock blunts scissors", |
||
"paper wraps rock", |
"paper wraps rock", |
||
Line 4,162: | Line 4,162: | ||
printf(1," ") for i=1 to choices do printf(1,"%9s",what[i]) end for |
printf(1," ") for i=1 to choices do printf(1,"%9s",what[i]) end for |
||
printf(1,"\nyou: ") for i=1 to choices do printf(1,"%9d",pplays[i]) end for |
printf(1,"\nyou: ") for i=1 to choices do printf(1,"%9d",pplays[i]) end for |
||
printf(1,"\n me: ") for i=1 to choices do printf(1,"%9d",cplays[i]) end for</ |
printf(1,"\n me: ") for i=1 to choices do printf(1,"%9d",cplays[i]) end for</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 8px"> |
<pre style="font-size: 8px"> |
||
Line 4,184: | Line 4,184: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
||
0 var wh |
0 var wh |
||
Line 4,239: | Line 4,239: | ||
"Your punctuation: " print wh ? |
"Your punctuation: " print wh ? |
||
"Mi punctuation: " print wc ? |
"Mi punctuation: " print wc ? |
||
wh wc > if "You win!" else wh wc < if "I win!" else "Draw!" endif endif ?</ |
wh wc > if "You win!" else wh wc < if "I win!" else "Draw!" endif endif ?</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>'Rock, Paper, Scissors, Lizard, Spock!' rules are: |
<pre>'Rock, Paper, Scissors, Lizard, Spock!' rules are: |
||
Line 4,277: | Line 4,277: | ||
<syntaxhighlight lang="php"> |
|||
<lang PHP> |
|||
<?php |
<?php |
||
Line 4,307: | Line 4,307: | ||
echo "<br>" . $results; |
echo "<br>" . $results; |
||
?> |
?> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
Line 4,314: | Line 4,314: | ||
(Some part is from the Prolog version.) |
(Some part is from the Prolog version.) |
||
< |
<syntaxhighlight lang="picat">go ?=> |
||
println("\nEnd terms with '.'.\n'quit.' ends the session.\n"), |
println("\nEnd terms with '.'.\n'quit.' ends the session.\n"), |
||
Prev = findall(P,beats(P,_)), |
Prev = findall(P,beats(P,_)), |
||
Line 4,411: | Line 4,411: | ||
beats(paper, rock). |
beats(paper, rock). |
||
beats(rock, scissors). |
beats(rock, scissors). |
||
beats(scissors, paper).</ |
beats(scissors, paper).</syntaxhighlight> |
||
Line 4,447: | Line 4,447: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(use (C Mine Your) |
||
(let (Rock 0 Paper 0 Scissors 0) |
(let (Rock 0 Paper 0 Scissors 0) |
||
(loop |
(loop |
||
Line 4,466: | Line 4,466: | ||
((== Your (car Mine)) "I win") |
((== Your (car Mine)) "I win") |
||
(T "You win") ) ) |
(T "You win") ) ) |
||
(inc Your) ) ) )</ |
(inc Your) ) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
rock: procedure options (main); /* 30 October 2013 */ |
rock: procedure options (main); /* 30 October 2013 */ |
||
declare move character (1), cm fixed binary; |
declare move character (1), cm fixed binary; |
||
Line 4,501: | Line 4,501: | ||
end; |
end; |
||
end rock; |
end rock; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog">play :- |
||
findall(P,beats(P,_),Prev), |
findall(P,beats(P,_),Prev), |
||
play(Prev). |
play(Prev). |
||
Line 4,530: | Line 4,530: | ||
beats(paper, rock). |
beats(paper, rock). |
||
beats(rock, scissors). |
beats(rock, scissors). |
||
beats(scissors, paper).</ |
beats(scissors, paper).</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Enumeration |
||
;choices are in listed according to their cycle, weaker followed by stronger |
;choices are in listed according to their cycle, weaker followed by stronger |
||
#rock |
#rock |
||
Line 4,616: | Line 4,616: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre style="height:40ex;overflow:scroll">Welcome to the game of rock-paper-scissors |
<pre style="height:40ex;overflow:scroll">Welcome to the game of rock-paper-scissors |
||
Line 4,668: | Line 4,668: | ||
The <code>rules</code> dictionary is of the form <code>'this': beaten by 'that', etc</code> as opposed to <code>'this': beats 'that'</code>. |
The <code>rules</code> dictionary is of the form <code>'this': beaten by 'that', etc</code> as opposed to <code>'this': beats 'that'</code>. |
||
< |
<syntaxhighlight lang="python">from random import choice |
||
rules = {'rock': 'paper', 'scissors': 'rock', 'paper': 'scissors'} |
rules = {'rock': 'paper', 'scissors': 'rock', 'paper': 'scissors'} |
||
Line 4,689: | Line 4,689: | ||
else: print("it's a tie!") |
else: print("it's a tie!") |
||
else: print("that's not a valid choice")</ |
else: print("that's not a valid choice")</syntaxhighlight> |
||
Output, where player always chooses Rock: |
Output, where player always chooses Rock: |
||
Line 4,709: | Line 4,709: | ||
This is another code. Output is as same as the above output. |
This is another code. Output is as same as the above output. |
||
< |
<syntaxhighlight lang="python">from random import randint |
||
hands = ['rock', 'scissors', 'paper']; judge = ['its a tie!', 'the computer beat you... :(', 'yay you win!'] |
hands = ['rock', 'scissors', 'paper']; judge = ['its a tie!', 'the computer beat you... :(', 'yay you win!'] |
||
Line 4,718: | Line 4,718: | ||
break |
break |
||
NPC = randint(0, 2) |
NPC = randint(0, 2) |
||
print('The computer played ' + hands[NPC] + '; ' + judge[YOU-NPC])</ |
print('The computer played ' + hands[NPC] + '; ' + judge[YOU-NPC])</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
This milks R's vectorisation quite heavily. However, this approach doesn't generalise well to the extra credit task. In particular, the last two lines of the loop would need a lot of work to be both non-ugly and working. |
This milks R's vectorisation quite heavily. However, this approach doesn't generalise well to the extra credit task. In particular, the last two lines of the loop would need a lot of work to be both non-ugly and working. |
||
< |
<syntaxhighlight lang="rsplus">play <- function() |
||
{ |
{ |
||
bias <- c(r = 1, p = 1, s = 1) |
bias <- c(r = 1, p = 1, s = 1) |
||
Line 4,737: | Line 4,737: | ||
} |
} |
||
} |
} |
||
play()</ |
play()</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require math) |
(require math) |
||
Line 4,775: | Line 4,775: | ||
(game-loop) |
(game-loop) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 4,783: | Line 4,783: | ||
Here is standard Rock-Paper-Scissors. |
Here is standard Rock-Paper-Scissors. |
||
<lang |
<syntaxhighlight lang="raku" line>my %vs = ( |
||
options => [<Rock Paper Scissors>], |
options => [<Rock Paper Scissors>], |
||
ro => { |
ro => { |
||
Line 4,822: | Line 4,822: | ||
print ( 'You win!', 'You Lose!','Tie.' )[$result]; |
print ( 'You win!', 'You Lose!','Tie.' )[$result]; |
||
say " - (W:{@stats[0]} L:{@stats[1]} T:{@stats[2]})\n", |
say " - (W:{@stats[0]} L:{@stats[1]} T:{@stats[2]})\n", |
||
};</ |
};</syntaxhighlight>Example output: |
||
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors? ro |
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors? ro |
||
You chose Rock, Computer chose Paper. |
You chose Rock, Computer chose Paper. |
||
Line 4,846: | Line 4,846: | ||
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]. |
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 |
<syntaxhighlight lang="raku" line>my %vs = ( |
||
options => [<Rock Paper Scissors Lizard Spock>], |
options => [<Rock Paper Scissors Lizard Spock>], |
||
ro => { |
ro => { |
||
Line 4,883: | Line 4,883: | ||
sp => [ 2, '' ] |
sp => [ 2, '' ] |
||
} |
} |
||
);</ |
);</syntaxhighlight> |
||
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li |
<pre>Round 1: [Ro]ck [Pa]per [Sc]issors [Li]zard [Sp]ock? li |
||
Line 4,907: | Line 4,907: | ||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
< |
<syntaxhighlight lang="rascal">import Prelude; |
||
rel[str, str] whatbeats = {<"Rock", "Scissors">, <"Scissors", "Paper">, <"Paper", "Rock">}; |
rel[str, str] whatbeats = {<"Rock", "Scissors">, <"Scissors", "Paper">, <"Paper", "Rock">}; |
||
Line 4,926: | Line 4,926: | ||
ComputerChoices += x; |
ComputerChoices += x; |
||
return "Computer played <computer>. <CheckWinner(human, computer)> wins!"; |
return "Computer played <computer>. <CheckWinner(human, computer)> wins!"; |
||
}</ |
}</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
< |
<syntaxhighlight lang="rascal">rascal>RPS("Rock") |
||
str: "Computer played Rock. Nobody wins!" |
str: "Computer played Rock. Nobody wins!" |
||
Line 4,956: | Line 4,956: | ||
rascal>RPS("Rock") |
rascal>RPS("Rock") |
||
str: "Computer played Paper. Paper wins!"</ |
str: "Computer played Paper. Paper wins!"</syntaxhighlight> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
<syntaxhighlight lang="rebol"> |
|||
<lang Rebol> |
|||
Red [Purpose: "Implement a rock-paper-scissors game with weighted probability"] |
Red [Purpose: "Implement a rock-paper-scissors game with weighted probability"] |
||
Line 4,980: | Line 4,980: | ||
remove find prior close ;removes what would have lost to player |
remove find prior close ;removes what would have lost to player |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,990: | Line 4,990: | ||
::* keeps track of the human player's responses (to hopefully make future computer winning choices) |
::* keeps track of the human player's responses (to hopefully make future computer winning choices) |
||
::* uses better "English"/grammer, i.e.: ''rock breaks scissors'', and ''paper covers rock''. |
::* uses better "English"/grammer, i.e.: ''rock breaks scissors'', and ''paper covers rock''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program plays rock─paper─scissors with a human; tracks what human tends to use. */ |
||
!= '────────'; err=! "***error***"; @.=0 /*some constants for this program. */ |
!= '────────'; err=! "***error***"; @.=0 /*some constants for this program. */ |
||
prompt= ! 'Please enter one of: Rock Paper Scissors (or Quit)' |
prompt= ! 'Please enter one of: Rock Paper Scissors (or Quit)' |
||
Line 5,023: | Line 5,023: | ||
if $.a1==t.c1 then say ! 'the computer wins. ' ! $.c1 b.c1 $.a1 |
if $.a1==t.c1 then say ! 'the computer wins. ' ! $.c1 b.c1 $.a1 |
||
else say ! 'you win! ' ! $.a1 b.a1 $.c1 |
else say ! 'you win! ' ! $.a1 b.a1 $.c1 |
||
end /*forever*/ /*stick a fork in it, we're all done. */</ |
end /*forever*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= with various responses from the user (output shown is a screen scraping):}} |
{{out|output|text= with various responses from the user (output shown is a screen scraping):}} |
||
<pre> |
<pre> |
||
Line 5,070: | Line 5,070: | ||
===extended, 5 choices=== |
===extended, 5 choices=== |
||
This REXX version supports more choices: <big> rock paper scissors lizard Spock </big> |
This REXX version supports more choices: <big> rock paper scissors lizard Spock </big> |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm plays rock─paper─scissors─lizard─Spock with human; tracks human usage trend. */ |
||
!= '────────'; err=! "***error***"; @.=0 /*some constants for this REXX program.*/ |
!= '────────'; err=! "***error***"; @.=0 /*some constants for this REXX program.*/ |
||
prompt=! 'Please enter one of: Rock Paper SCissors Lizard SPock (Vulcan) (or Quit)' |
prompt=! 'Please enter one of: Rock Paper SCissors Lizard SPock (Vulcan) (or Quit)' |
||
Line 5,116: | Line 5,116: | ||
end /*j*/ |
end /*j*/ |
||
end /*who*/ |
end /*who*/ |
||
end /*forever*/ /*stick a fork in it, we're all done. */</ |
end /*forever*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= is similar to the 1<sup>st</sup> REXX version.}} <br><br> |
{{out|output|text= is similar to the 1<sup>st</sup> REXX version.}} <br><br> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Rock-paper-scissors |
# Project : Rock-paper-scissors |
||
Line 5,332: | Line 5,332: | ||
app.quit() |
app.quit() |
||
</syntaxhighlight> |
|||
</lang> |
|||
[https://github.com/ring-lang/ring/tree/master/applications/rockpaperscissors Rock Paper Scissors - image] |
[https://github.com/ring-lang/ring/tree/master/applications/rockpaperscissors Rock Paper Scissors - image] |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class RockPaperScissorsGame |
||
CHOICES = %w[rock paper scissors quit] |
CHOICES = %w[rock paper scissors quit] |
||
BEATS = { |
BEATS = { |
||
Line 5,404: | Line 5,404: | ||
end |
end |
||
RockPaperScissorsGame.new</ |
RockPaperScissorsGame.new</syntaxhighlight> |
||
sample game where human always chooses rock: |
sample game where human always chooses rock: |
||
Line 5,454: | Line 5,454: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">pri$ = "RSPR" |
||
rps$ = "Rock,Paper,Sissors" |
rps$ = "Rock,Paper,Sissors" |
||
[loop] |
[loop] |
||
Line 5,484: | Line 5,484: | ||
print "Good Bye! I enjoyed the game" |
print "Good Bye! I enjoyed the game" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">extern crate rand; |
||
#[macro_use] |
#[macro_use] |
||
extern crate rand_derive; |
extern crate rand_derive; |
||
Line 5,569: | Line 5,569: | ||
} |
} |
||
println!("Thank you for the game!"); |
println!("Thank you for the game!"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
You can invoke this game with an arbitrary number of weapons: |
You can invoke this game with an arbitrary number of weapons: |
||
< |
<syntaxhighlight lang="scala">object RockPaperScissors extends App { |
||
import scala.collection.mutable.LinkedHashMap |
import scala.collection.mutable.LinkedHashMap |
||
def play(beats: LinkedHashMap[Symbol,Set[Symbol]], played: scala.collection.Map[Symbol,Int]) { |
def play(beats: LinkedHashMap[Symbol,Set[Symbol]], played: scala.collection.Map[Symbol,Int]) { |
||
Line 5,615: | Line 5,615: | ||
'spock -> Set('scissors, 'rock) |
'spock -> Set('scissors, 'rock) |
||
)) |
)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Your move ('rock, 'paper, 'scissors, 'lizard, 'spock): paper |
<pre>Your move ('rock, 'paper, 'scissors, 'lizard, 'spock): paper |
||
Line 5,643: | Line 5,643: | ||
Here's another code: (I refactored the above code, it is more functional, more testable ) |
Here's another code: (I refactored the above code, it is more functional, more testable ) |
||
< |
<syntaxhighlight lang="scala">object RockPaperScissors extends App { |
||
def beats = Map( |
def beats = Map( |
||
'rock -> Set('lizard, 'scissors), |
'rock -> Set('lizard, 'scissors), |
||
Line 5,702: | Line 5,702: | ||
override def main(args: Array[String]): Unit = |
override def main(args: Array[String]): Unit = |
||
play(input, display, random)(initPlayed, Result("Start", 0, 0, 0)) |
play(input, display, random)(initPlayed, Result("Start", 0, 0, 0)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,732: | Line 5,732: | ||
{{incorrect|Seed7|This example does not seem to use the weighted average AI from the task description.}} |
{{incorrect|Seed7|This example does not seem to use the weighted average AI from the task description.}} |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
$ include "keybd.s7i"; |
$ include "keybd.s7i"; |
||
Line 5,766: | Line 5,766: | ||
until command = 'q'; |
until command = 'q'; |
||
writeln("Goodbye! Thanks for playing!"); |
writeln("Goodbye! Thanks for playing!"); |
||
end func;</ |
end func;</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 5,788: | Line 5,788: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">const rps = %w(r p s) |
||
const msg = [ |
const msg = [ |
||
Line 5,841: | Line 5,841: | ||
default { aChoice = 0 } |
default { aChoice = 0 } |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
'''Sample run:''' |
'''Sample run:''' |
||
Line 5,980: | Line 5,980: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">enum Choice: CaseIterable { |
||
case rock |
case rock |
||
case paper |
case paper |
||
Line 6,057: | Line 6,057: | ||
game.play(choice, against: p2Choice) |
game.play(choice, against: p2Choice) |
||
print("Current score: \(game.p1Score) : \(game.p2Score)") |
print("Current score: \(game.p1Score) : \(game.p2Score)") |
||
}</ |
}</syntaxhighlight> |
||
'''Sample run:''' |
'''Sample run:''' |
||
<pre> |
<pre> |
||
Line 6,077: | Line 6,077: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
### Choices are represented by integers, which are indices into this list: |
### Choices are represented by integers, which are indices into this list: |
||
Line 6,140: | Line 6,140: | ||
# Update the state of how the human has played in the past |
# Update the state of how the human has played in the past |
||
lset states $humanMove [expr {[lindex $states $humanMove] + 1}] |
lset states $humanMove [expr {[lindex $states $humanMove] + 1}] |
||
}</ |
}</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
<pre> |
<pre> |
||
Line 6,174: | Line 6,174: | ||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
< |
<syntaxhighlight lang="ti83b">PROGRAM:RPS |
||
:{0,0,0}→L1 |
:{0,0,0}→L1 |
||
:{0,0,0}→L2 |
:{0,0,0}→L2 |
||
Line 6,238: | Line 6,238: | ||
:Disp L2(3) |
:Disp L2(3) |
||
:Disp "BYE" |
:Disp "BYE" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{omit from|GUISS}} |
{{omit from|GUISS}} |
||
Line 6,246: | Line 6,246: | ||
Rock Paper Scissors in TorqueScript: |
Rock Paper Scissors in TorqueScript: |
||
<syntaxhighlight lang="torquescript"> |
|||
<lang TorqueScript> |
|||
while(isobject(RockPaperScissors)) |
while(isobject(RockPaperScissors)) |
||
RockPaperScissors.delete(); |
RockPaperScissors.delete(); |
||
Line 6,387: | Line 6,387: | ||
return %result; |
return %result; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
To begin do: |
To begin do: |
||
<syntaxhighlight lang="torquescript"> |
|||
<lang TorqueScript> |
|||
RockPaperScissors.startGame(); |
RockPaperScissors.startGame(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Choose and play! |
Choose and play! |
||
<syntaxhighlight lang="torquescript"> |
|||
<lang TorqueScript> |
|||
choose("Rock"); |
choose("Rock"); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=> You chose rock computer chose paper, you lose! |
=> You chose rock computer chose paper, you lose! |
||
Line 6,408: | Line 6,408: | ||
{{incorrect|uBasic/4tH|This example does not seem to use the weighted average AI from the task description.}} |
{{incorrect|uBasic/4tH|This example does not seem to use the weighted average AI from the task description.}} |
||
<lang> 20 LET P=0: LET Q=0: LET Z=0 |
<syntaxhighlight lang="text"> 20 LET P=0: LET Q=0: LET Z=0 |
||
30 INPUT "Rock, paper, or scissors (1 = rock, 2 = paper, 3 = scissors)? ", A |
30 INPUT "Rock, paper, or scissors (1 = rock, 2 = paper, 3 = scissors)? ", A |
||
40 IF A>3 THEN GOTO 400 |
40 IF A>3 THEN GOTO 400 |
||
Line 6,423: | Line 6,423: | ||
340 Q=Q+1 : PRINT "You chose 'paper', I chose 'scissors'. I win!" : GOTO 30 |
340 Q=Q+1 : PRINT "You chose 'paper', I chose 'scissors'. I win!" : GOTO 30 |
||
360 Z=Z+1 : PRINT "We both chose 'scissors'. It's a draw." : GOTO 30 |
360 Z=Z+1 : PRINT "We both chose 'scissors'. It's a draw." : GOTO 30 |
||
400 PRINT "There were ";Z;" draws. I lost ";P;" times, you lost ";Q;" times." : END</ |
400 PRINT "There were ";Z;" draws. I lost ";P;" times, you lost ";Q;" times." : END</syntaxhighlight> |
||
A sample game: |
A sample game: |
||
Line 6,443: | Line 6,443: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Again Shell|4}} |
{{works with|Bourne Again Shell|4}} |
||
< |
<syntaxhighlight lang="bash">#!/bin/bash |
||
choices=(rock paper scissors) |
choices=(rock paper scissors) |
||
Line 6,505: | Line 6,505: | ||
echo "You picked ${choices[i]} $(( human_counts[ (i+1)%3 ] - 1 )) times." |
echo "You picked ${choices[i]} $(( human_counts[ (i+1)%3 ] - 1 )) times." |
||
echo "I picked ${choices[i]} $(( computer_counts[i] )) times." |
echo "I picked ${choices[i]} $(( computer_counts[i] )) times." |
||
done</ |
done</syntaxhighlight> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
Semi-translation of Go version: |
Semi-translation of Go version: |
||
< |
<syntaxhighlight lang="vlang">import rand |
||
import os |
import os |
||
Line 6,553: | Line 6,553: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,593: | Line 6,593: | ||
=={{header|Wee Basic}}== |
=={{header|Wee Basic}}== |
||
Due to how the code works, any key has to be entered for the computer's choice to be generated. |
Due to how the code works, any key has to be entered for the computer's choice to be generated. |
||
< |
<syntaxhighlight lang="wee basic">let entered=0 |
||
let keycode=0 |
let keycode=0 |
||
let rcounter=1 |
let rcounter=1 |
||
Line 6,665: | Line 6,665: | ||
endif |
endif |
||
endif |
endif |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 6,671: | Line 6,671: | ||
{{libheader|Wren-str}} |
{{libheader|Wren-str}} |
||
{{libheader|Wren-ioutil}} |
{{libheader|Wren-ioutil}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/str" for Str |
import "/str" for Str |
||
import "/ioutil" for Input |
import "/ioutil" for Input |
||
Line 6,732: | Line 6,732: | ||
games = games + 1 |
games = games + 1 |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,764: | Line 6,764: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">REM Yabasic 2.763 version |
||
WINNER = 1 : ACTION = 2 : LOSSER = 3 |
WINNER = 1 : ACTION = 2 : LOSSER = 3 |
||
Line 6,841: | Line 6,841: | ||
data "Paper","disproves","Spock" |
data "Paper","disproves","Spock" |
||
data "Spock","vaporizes","Rock" |
data "Spock","vaporizes","Rock" |
||
data "Rock","blunts","Scissors"</ |
data "Rock","blunts","Scissors"</syntaxhighlight> |