21 game: Difference between revisions

1,726 bytes added ,  3 years ago
no edit summary
(Added Wren)
No edit summary
Line 3,428:
end
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>SeedRandom[1234];
ClearAll[ComputerChoose, HumanChoose]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
HumanChoose[] := ChoiceDialog["How many?", {1 -> 1, 2 -> 2, 3 -> 3, "Quit" -> -1}]
runningtotal = 0;
whofirst = ChoiceDialog["Who goes first?", {"You" -> 1, "Computer" -> 2}];
While[runningtotal < 21,
If[whofirst == 1,
choice = HumanChoose[];
If[choice == -1, Break[]];
Print["You choose = ", choice];
runningtotal += choice;
Print["Running total = ", runningtotal];
If[runningtotal == 21, Print["You won!"]; Break[]];
choice = ComputerChoose[runningtotal];
Print["Computer choose = ", choice];
runningtotal += choice;
Print["Running total = ", runningtotal];
If[runningtotal == 21, Print["Computer won!"]; Break[]];
,
choice = ComputerChoose[runningtotal];
Print["Computer choose = ", choice];
runningtotal += choice;
Print["Running total = ", runningtotal];
If[runningtotal == 21, Print["Computer won!"]; Break[]];
choice = HumanChoose[];
If[choice == -1, Break[]];
Print["You choose = ", choice];
runningtotal += choice;
Print["Running total = ", runningtotal];
If[runningtotal == 21, Print["You won!"]; Break[]];
];
]</lang>
{{out}}
<pre>You choose = 2
Running total = 2
Computer choose = 3
Running total = 5
You choose = 3
Running total = 8
Computer choose = 1
Running total = 9
You choose = 3
Running total = 12
Computer choose = 1
Running total = 13
You choose = 1
Running total = 14
Computer choose = 2
Running total = 16
You choose = 1
Running total = 17
Computer choose = 3
Running total = 20
You choose = 1
Running total = 21
You won!</pre>
 
 
=={{header|Nim}}==
1,111

edits