summaryrefslogtreecommitdiff
path: root/main.go
blob: 4431dd0f78873be92eb1598c72eafc15000be651 (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
package main

import (
	"flag"
	"fmt"
	"log"
	"time"
)

type Ball struct{ hits int }

func main() {
	numSecs := flag.Int("s", 1, "Number of seconds game should last")
	flag.Parse()

	game(*numSecs)
}

func game(numSecs int) {
	table := make(chan Ball)
	go player("ping", table)
	go player("pong", table)

	table <- Ball{}

	// Make the game last this long.
	time.Sleep(time.Duration(numSecs) * time.Second)

	// Game over: grab the ball.
	<-table
}

func player(name string, table chan Ball) {
	log.Printf("started player %s", name)

	for {
		ball := <-table
		ball.hits++
		fmt.Println(name, ball.hits)

		// Simulate some contact with the paddle.
		time.Sleep(100 * time.Millisecond)

		table <- ball
	}

	// FIXME: this is currently unreachable.
	log.Printf("finished player %s", name)
}