1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# Dicegame
A game simulation I wrote to help me better understand Go concurrency
concepts.
Based loosely on the `makeThumbnails6` example in Chapter 8 (page 238)
of *The Go Programming Language* (Addison-Wesley, 2016).
# Usage
The following invocation runs the simulation with two players, each of
which plays with a six-sided die:
`go run . -n 2 -sides 6`
Since 2 and 6 are the default values, in this case the following is
equivalent:
`go run .`
Of course, `go run . -help` will print a detailed listing of available
command-line arguments.
# The Scene: What Is Being Simulated?
Several people agree to convene to play a game of dice. Beforehand,
they decided on the number of sides a given die should have. Once they
do that, each player brings with them a die of that many sides (for
example, 6, 12, etc.; even a coin can count as a die in this case.)
One of the players (picked arbitrarily) rolls their die once. The
number that it shows becomes the winning number. Each player is then
tasked with rolling their die until they hit that number. The player
who hits the number with the fewest number of rolls wins. In case of a
tie, the first one to roll the winning number wins.
For example, three players get together and decide to use an ordinary
six-sided die each. The die is rolled, resulting in a 4. The players
then each go off into their corners and roll their die until they hit
a 4. Player 1 does it in three rolls, Player 2 in one (lucky!), and
Player 3 in seven. Player 2 wins.
# An Example Session
```
$ go run .
Welcome to the game
There are 2 players, each with a 6-sided die
Winning number is 6
Player 1 threw a 4
Player 2 threw a 1
Player 1 threw a 2
Player 2 threw a 4
Player 2 threw a 3
Player 1 threw a 3
Player 2 threw a 2
Player 1 threw a 3
Player 2 threw a 3
Player 1 threw a 4
Player 2 threw a 2
Player 1 threw a 1
Player 1 threw a 4
Player 2 threw a 6: the winning number! Their score is 7
Player 1 threw a 1
Player 1 threw a 3
Player 1 threw a 5
Player 1 threw a 6: the winning number! Their score is 11
Player 2 won with a score of 7
Thanks for playing!
```
|