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 |
enum Choice { rock, paper, scissors } |
||
immutable order = [EnumMembers!Choice]; |
|||
⚫ | |||
⚫ | |||
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"]; |
|||
} |
} |
||
Nullable!Choice checkWinner(in Choice a, in Choice b) |
|||
pure /*nothrow*/ { |
|||
⚫ | |||
alias TResult = typeof(return); |
|||
else if (a == whatBeats[b]) |
|||
if (b == whatBeats(a)) |
|||
return |
return TResult(b); |
||
⚫ | |||
return TResult(a); |
|||
return TResult.init; |
|||
} |
} |
||
Choice getRandomChoice() /*nothrow*/ { |
|||
if (choiceFrequency[].reduce!q{a + b} == 0) |
|||
return uniform!Choice; |
|||
if (choiceFrequency.length == 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- |
writeln("Rock-Paper-Scissors Game"); |
||
while (true) { |
while (true) { |
||
write("Your choice: "); |
write("Your choice (empty to end game): "); |
||
immutable |
immutable humanChoiceStr = readln.strip.toLower; |
||
if ( |
if (humanChoiceStr.empty) |
||
break; |
break; |
||
if (humanChoice !in whatBeats) { |
|||
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. |
if (winner.isNull) |
||
writeln(" |
writeln("Nobody wins!"); |
||
else |
else |
||
writeln(winner, " wins!"); |
writeln(winner.get, " wins!"); |
||
} |
} |
||
}</lang> |
}</lang> |
||
Output example |
{{out|Output example}} |
||
<pre>Rock- |
<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}}== |