RosettaCodeData/Task/Rock-paper-scissors/Mathematica/rock-paper-scissors.math
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

12 lines
562 B
Text

DynamicModule[{record, play, text = "\nRock-paper-scissors\n",
choices = {"Rock", "Paper", "Scissors"}},
Evaluate[record /@ choices] = {1, 1, 1};
play[x_] :=
Module[{y = RandomChoice[record /@ choices -> RotateLeft@choices]},
record[x]++;
text = "Your Choice:" <> x <> "\nComputer's Choice:" <> y <> "\n" <>
Switch[{x, y}, Alternatives @@ Partition[choices, 2, 1, 1],
"You lost.",
Alternatives @@ Reverse /@ Partition[choices, 2, 1, 1],
"You win.", _, "Draw."]];
Column@{Dynamic[text], ButtonBar[# :> play[#] & /@ choices]}]