summaryrefslogtreecommitdiff
path: root/main.go
blob: ee0d77e58aef0181aacffd27d3c09be65f7292f5 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main

import (
	"flag"
	"fmt"
	"log"
	"math"
	"math/rand"
	"sync"
	"time"
)

func main() {
	numPlayers := flag.Int("n", 2, "Number of players")
	sides := flag.Int("sides", 6, "How many sides the die should have")
	flag.Parse()

	if *numPlayers < 2 {
		log.Fatalf("we need at least 2 players")
	}

	if *sides < 2 {
		log.Fatalf("die needs at least two sides")
	}

	fmt.Println("Welcome to the game")
	fmt.Printf("There are %d players, each with a %d-sided die\n", *numPlayers, *sides)

	game(*numPlayers, *sides)
}

type Player struct {
	id    int
	score int
}

// The player with the lowest score wins.
func game(numPlayers, faces int) {
	// Get the game's winning number by throwing the die once.
	winningNumber := throwDie(faces)
	fmt.Printf("Winning number is %d\n", winningNumber)

	// The finishedPlayers channel communicates the number of turns a
	// player took to hit the winning number.
	finishedPlayers := make(chan Player)

	var guild sync.WaitGroup
	for i := range numPlayers {
		id := i + 1

		// Spawn a player.
		guild.Go(func() {
			var score int

			// Start rolling the dice!
			for {
				score++
				outcome := throwDie(faces)

				if outcome == winningNumber {
					fmt.Printf("Player %d threw a %d: the winning number! Their score is %d\n", id, winningNumber, score)
					finishedPlayers <- Player{id: id, score: score}
					return
				}

				fmt.Printf("Player %d threw a %d\n", id, outcome)
				time.Sleep(1 * time.Second)
			}
		})
	}

	// Launch a non-blocking daemon to wait for the guild to
	// finish, so that we can close the finishedPlayers channel.
	go stopGame(&guild, finishedPlayers)

	minPlayer := Player{score: math.MaxInt}

	// The range loop waits for [Player] score reports from the
	// workers (or else a [stopGame] request.)
	for player := range finishedPlayers {
		if player.score < minPlayer.score {
			minPlayer = player
		}
	}

	fmt.Printf("Player %d won with a score of %d\n", minPlayer.id, minPlayer.score)
	fmt.Println("Thanks for playing!")
}

func throwDie(faces int) int {
	return rand.Intn(faces) + 1
}

// stopGame waits for the members of the guild to finish, and then
// closes source for use. It functions as a "termination task."
func stopGame(guild *sync.WaitGroup, source chan Player) {
	guild.Wait()
	close(source)
}