Rock-paper-scissors: Difference between revisions

Added Elixir
m (→‎{{header|Sidef}}: minor code fix)
(Added Elixir)
Line 12:
 
'''Extra credit:''' easy support for [[wp:Rock-paper-scissors#Additional_weapons|additional weapons]].
 
=={{header|Ada}}==
 
Line 1,445 ⟶ 1,446:
 
rock, paper or scissors?
</pre>
 
=={{header|Elixir}}==
{{trans|Erlang}}
<lang elixir>defmodule Rock_paper_scissors do
def play, do: loop([1,1,1])
defp loop([r,p,s]=odds) do
IO.gets("What is your move? (R,P,S,Q) ") |> String.upcase |> String.first
|> case do
"Q" -> IO.puts "Good bye!"
human when human in ["R","P","S"] ->
IO.puts "Your move is #{play_to_string(human)}."
computer = select_play(odds)
IO.puts "My move is #{play_to_string(computer)}"
case beats(human,computer) do
true -> IO.puts "You win!"
false -> IO.puts "I win!"
_ -> IO.puts "Draw"
end
case human do
"R" -> loop([r+1,p,s])
"P" -> loop([r,p+1,s])
"S" -> loop([r,p,s+1])
end
_ ->
IO.puts "Invalid play"
loop(odds)
end
end
defp beats("R","S"), do: true
defp beats("P","R"), do: true
defp beats("S","P"), do: true
defp beats(x,x), do: :draw
defp beats(_,_), do: false
defp play_to_string("R"), do: "Rock"
defp play_to_string("P"), do: "Paper"
defp play_to_string("S"), do: "Scissors"
defp select_play([r,p,s]) do
n = :rand.uniform(r+p+s)
cond do
n <= r -> "P"
n <= r+p -> "S"
true -> "R"
end
end
end
 
Rock_paper_scissors.play</lang>
 
'''Sample output:'''
<pre>
What is your move? (R,P,S,Q) r
Your move is Rock.
My move is Scissors
You win!
What is your move? (R,P,S,Q) p
Your move is Paper.
My move is Paper
Draw
What is your move? (R,P,S,Q) s
Your move is Scissors.
My move is Scissors
Draw
What is your move? (R,P,S,Q) q
Good bye!
</pre>
 
Line 2,674 ⟶ 2,744:
Can you see what will happen if, say, the 'human' is set to give 'Rock' EVERY time?
Try different %ages by altering the marked code lines.
 
 
=={{header|Locomotive Basic}}==
Line 2,703 ⟶ 2,772:
220 if rn<pcf(1) then achoice=2 else if rn<pcf(1)+pcf(2) then achoice=3 else achoice=1
230 goto 110</lang>
 
 
=={{header|Lua}}==
Line 2,790 ⟶ 2,858:
Press ENTER to continue or enter 'Q' to quit . . .
</pre>
 
=={{header|Mathematica}}==
<lang mathematica>DynamicModule[{record, play, text = "\nRock-paper-scissors\n",
Line 3,946 ⟶ 4,015:
end
</lang>
 
=={{header|Scala}}==
You can invoke this game with an arbitrary number of weapons:
Line 4,565 ⟶ 4,635:
:Disp "BYE"
</lang>
 
 
{{omit from|GUISS}}
 
=={{header|uBasic/4tH}}==
This implementation uses a 6-bits binary scheme, where the lower three bits represent the choice of the user and the higher three bits the choice of the computer:
Anonymous user