-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
62 lines (55 loc) · 905 Bytes
/
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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"math/rand"
"os"
"time"
)
func main() {
t := flag.Int("t", 10, "制限時間(秒)")
flag.Parse()
var score int
l := time.After(time.Duration(*t) * time.Second)
in := input(os.Stdin)
rand.Seed(time.Now().UnixNano())
fmt.Println("Start!!")
L:
for {
q := words[rand.Intn(len(words))]
fmt.Println(q)
fmt.Print(">> ")
select {
case a := <-in:
score += judge(q, a)
case <-l:
fmt.Println("finish!!")
break L
}
}
fmt.Printf("Score: %v, type/sec: %2.1f\n", score, float64(score)/float64(*t))
}
func judge(q, a string) int {
var score int
for i := 0; i < len(q); i++ {
if i == len(a) {
break
}
if q[i] == a[i] {
score++
}
}
return score
}
func input(r io.Reader) <-chan string {
c := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
c <- s.Text()
}
}()
return c
}