-
Notifications
You must be signed in to change notification settings - Fork 0
/
PingPong.go
42 lines (37 loc) · 657 Bytes
/
PingPong.go
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
package main
import (
"fmt"
"time"
)
func main() {
playTime := time.Duration(2) //seconds
next := counter(1)
numberOfPlayers := 4
var Ball int
table := make(chan int)
for i := 0; i < numberOfPlayers; i++ {
go player(table, next)
}
table <- Ball
time.Sleep(playTime * time.Second)
<-table
}
func player(table chan int, next func() int) {
playerNumber := next()
for {
ball := <-table
ball++
time.Sleep(100 * time.Millisecond)
fmt.Printf("Player# %d Hit #%d \n", playerNumber, ball)
table <- ball
}
}
func counter(start int) func() int {
//closure
count := start - 1
f := func() int {
count++
return count
}
return f
}