-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
82 lines (69 loc) · 1.23 KB
/
main.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
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
package main
import (
"bufio"
"context"
"fmt"
"io"
"math/rand"
"os"
"time"
)
func main() {
var counter int
ch := input(os.Stdin)
words, err := RegisterWords()
if err != nil {
fmt.Println("err")
}
wordNum := len(words)
rand.Seed(time.Now().UnixNano())
var displayWord string
displayWord = words[rand.Intn(wordNum)]
fmt.Print(displayWord, " : ")
bc := context.Background()
t := 10 * time.Second
ctx, cancel := context.WithTimeout(bc, t)
defer cancel()
for {
select {
case v := <-ch:
fmt.Println(v, "come")
if v == displayWord {
counter += 1
fmt.Println("o")
} else {
fmt.Println("x")
}
displayWord = words[rand.Intn(wordNum)]
fmt.Print(displayWord, " : ")
case <-ctx.Done():
fmt.Println("\ntime up!\nscore :", counter)
goto finish
}
}
finish:
}
func RegisterWords() ([]string, error) {
f, err := os.Open("list.csv")
if err != nil {
return nil, err
}
var words []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
words = append(words, scanner.Text())
}
err = f.Close()
return words, err
}
func input(r io.Reader) chan string {
c := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
c <- s.Text()
}
close(c)
}()
return c
}