-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (78 loc) · 1.47 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
83
84
85
86
87
88
89
90
package main
import (
"bufio"
crand "crypto/rand"
"fmt"
"io"
"math"
"math/big"
"math/rand"
"os"
"time"
)
var word = [...]string{
"toukyoutokkyokyokakyoku",
"akamakigamiaomakigamikimakigami",
"sushi",
"tenpura",
"kaiken",
"nisshingeppo",
"hyappatuhyakutyu",
"kumiai",
"taiiku",
"kome",
}
const (
limitSec = 15
ExitAbort = 1
)
func init() {
if err := serRandSeed(); err != nil {
os.Exit(ExitAbort)
}
}
func serRandSeed() error {
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
rand.Seed(seed.Int64())
return err
}
func main() {
fmt.Printf("制限時間は%d秒です\n", limitSec)
ch := input(os.Stdin)
var correctCount int
go output(ch, &correctCount)
<-time.After(limitSec * time.Second)
finish(correctCount)
}
func input(r io.Reader) <-chan string {
ch := make(chan string)
go read(r, ch)
return ch
}
func read(reader io.Reader, ch chan string) {
s := bufio.NewScanner(reader)
for s.Scan() {
ch <- s.Text()
}
close(ch)
}
func output(typeWord <-chan string, correctCount *int) {
wordCount := len(word)
for {
answerWord := word[rand.Intn(wordCount)]
fmt.Println(answerWord)
fmt.Print("> ")
typeWord := <-typeWord
if typeWord == answerWord {
fmt.Println("correct answer!")
*correctCount++
} else {
fmt.Println("incorrect answer...")
}
fmt.Println("-----------------------------")
}
}
func finish(correctCount int) {
fmt.Println("\n-----------FINISH!-----------")
fmt.Printf("正解数:%d\n", correctCount)
}