Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/21-game/Mathematica/21-game.math
Normal file
33
Task/21-game/Mathematica/21-game.math
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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[]];
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue