-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingpong.go
More file actions
66 lines (59 loc) · 1.13 KB
/
pingpong.go
File metadata and controls
66 lines (59 loc) · 1.13 KB
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
package main
import "fmt"
import "time"
import "sync"
import "math/rand"
var wg sync.WaitGroup
var vollies = 0
var max = 17
var min = 5
func init() {
rand.Seed(time.Now().Unix())
vollies = rand.Intn(max-min) + min
}
func main() {
messages := make(chan string)
wg.Add(1)
go func() {
defer wg.Done()
i := 0
for {
select {
case msg := <-messages:
fmt.Println("Pong")
if i > vollies {
fmt.Println("Game Over ! I win Mr. Ping")
messages <- "done"
return
} else if msg == "done" {
return
}
messages <- "Pong"
i++
case <-time.After(time.Second):
fmt.Println("You are slow. Let's get this game going!!! Waiting for Mr. Ping")
}
}
}()
//Waiting here for timeout in pong function
//
time.Sleep(time.Second * 1)
fmt.Println("Ping")
messages <- "Ping"
time.Sleep(time.Second * 2)
for {
select {
case msg := <-messages:
if msg == "Pong" {
fmt.Println("Ping")
messages <- "Ping"
} else if msg == "done" {
fmt.Println("Will get you next time Mr. Pong")
wg.Wait()
return
}
case <-time.After(time.Second):
fmt.Println("Waiting for Mr. Pong")
}
}
}