Rock-paper-scissors: Difference between revisions

Content added Content deleted
m (Added the extra weapons extra credit. Discussed in the talk page and people seem to be OK with implementing it.)
(Updated D entry)
Line 806: Line 806:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.random, std.string, std.array;
<lang d>import std.stdio, std.random, std.string, std.array,
std.typecons, std.traits, std.conv, std.algorithm;


enum string[] order = ["rock", "paper", "scissors"];
enum Choice { rock, paper, scissors }
immutable order = [EnumMembers!Choice];
int[string] choiceFrequency; // mutable


uint[order.length] choiceFrequency;
immutable(string[string]) whatBeats;

nothrow pure static this() {
Choice whatBeats(in Choice ch) pure /*nothrow*/ {
whatBeats = ["paper": "scissors",
return order[(order.countUntil(ch) + 1) % $];
"scissors": "rock",
"rock": "paper"];
}
}


string checkWinner(in string a, in string b) pure nothrow {
Nullable!Choice checkWinner(in Choice a, in Choice b)
pure /*nothrow*/ {
if (b == whatBeats[a])
return b;
alias TResult = typeof(return);

else if (a == whatBeats[b])
return a;
if (b == whatBeats(a))
return "";
return TResult(b);
else if (a == whatBeats(b))
return TResult(a);
return TResult.init;
}
}


string getRandomChoice() /*nothrow*/ {
Choice getRandomChoice() /*nothrow*/ {
//if (choiceFrequency.empty)
if (choiceFrequency[].reduce!q{a + b} == 0)
return uniform!Choice;
if (choiceFrequency.length == 0)
return order[uniform(0, $)];
return order[choiceFrequency.dice].whatBeats;
const choices = choiceFrequency.keys;
const probabilities = choiceFrequency.values;
return whatBeats[choices[dice(probabilities)]];
}
}


void main() {
void main() {
writeln("Rock-paper-scissors game");
writeln("Rock-Paper-Scissors Game");

while (true) {
while (true) {
write("Your choice: ");
write("Your choice (empty to end game): ");
immutable string humanChoice = readln().strip().toLower();
immutable humanChoiceStr = readln.strip.toLower;
if (humanChoice.empty)
if (humanChoiceStr.empty)
break;
break;

if (humanChoice !in whatBeats) {
writeln("Wrong input: ", humanChoice);
Choice humanChoice;
try {
humanChoice = humanChoiceStr.to!Choice;
} catch (ConvException e) {
writeln("Wrong input: ", humanChoiceStr);
continue;
continue;
}
}


immutable compChoice = getRandomChoice();
immutable compChoice = getRandomChoice;
write("Computer picked ", compChoice, ", ");
write("Computer picked ", compChoice, ", ");


Line 855: Line 860:


immutable winner = checkWinner(humanChoice, compChoice);
immutable winner = checkWinner(humanChoice, compChoice);
if (winner.empty)
if (winner.isNull)
writeln("nobody wins!");
writeln("Nobody wins!");
else
else
writeln(winner, " wins!");
writeln(winner.get, " wins!");
}
}
}</lang>
}</lang>
Output example:
{{out|Output example}}
<pre>Rock-paper-scissors game
<pre>Rock-Paper-Scissors Game
Your choice: rock
Your choice (empty to end game): rock
Computer picked scissors, rock wins!
Computer picked scissors, rock wins!
Your choice: scissors
Your choice (empty to end game): scissors
Computer picked paper, scissors wins!
Computer picked paper, scissors wins!
Your choice:</pre>
Your choice (empty to end game):</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==