34 lines
1.2 KiB
Plaintext
34 lines
1.2 KiB
Plaintext
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[]];
|
|
];
|
|
]
|