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:
=={{header|D}}==
{{trans|Python}}
<lang d>import std.stdio, std.random, std.string, std.array
std.typecons, std.traits, std.conv, std.algorithm;
enum
immutable order = [EnumMembers!Choice];
int[string] choiceFrequency; // mutable▼
Choice whatBeats(in Choice ch) pure /*nothrow*/ {
return order[(order.countUntil(ch) + 1) % $];
}
pure /*nothrow*/ {
if (b == whatBeats[a])▼
alias TResult =
if (b ==
return
return TResult(a);
return TResult.init;
}
return uniform!Choice;
}
void main() {
writeln("Rock-
while (true) {
write("Your choice (empty to end game): ");
immutable
if (
break;
try {
humanChoice = humanChoiceStr.to!Choice;
} catch (ConvException e) {
writeln("Wrong input: ", humanChoiceStr);
continue;
}
immutable compChoice = getRandomChoice
write("Computer picked ", compChoice, ", ");
Line 855 ⟶ 860:
immutable winner = checkWinner(humanChoice, compChoice);
if (winner.
writeln("
else
writeln(winner.get, " wins!");
}
}</lang>
{{out|Output example
<pre>Rock-
Your choice (empty to end game): rock
Computer picked scissors, rock wins!
Your choice (empty to end game): scissors
Computer picked paper, scissors wins!
Your choice (empty to end game):</pre>
=={{header|Erlang}}==
|