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

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

type Ball struct{ hits int }

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

	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) {
	for {
		ball := <-table
		ball.hits++
		fmt.Println(name, ball.hits)

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

		table <- ball
	}
}