-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
66 lines (55 loc) · 1.29 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"time"
"github.com/gopherdojo/dojo7/kadai3-1/shinta/typing"
)
const timeLimit = 5
/*
Typing displays the English word in question with the showtext method.
Compare with the value entered in the judge method.
*/
type Typing interface {
ShowText() string
Judge(input string) bool
}
func main() {
problems := []string{"apple", "bake", "cup", "dog", "egg", "fight", "green", "hoge", "idea", "japan"}
typing := typing.Redy(problems)
chInput := inputRoutine(os.Stdin)
chFinish := time.After(time.Duration(timeLimit) * time.Second)
execute(chInput, chFinish, os.Stdout, typing)
}
func execute(chInput <-chan string, chFinish <-chan time.Time, stdout io.Writer, t Typing) {
score := 0
for i := 1; ; i++ {
fmt.Fprintf(stdout, "[%03d]: %v\n", i, t.ShowText())
fmt.Fprint(stdout, "type>>")
select {
case inText := <-chInput:
if t.Judge(inText) {
score++
fmt.Fprintln(stdout, "Correct!")
} else {
fmt.Fprintln(stdout, "Miss!")
}
case <-chFinish:
fmt.Fprintln(stdout, "\nTime's up!!")
fmt.Fprintf(stdout, "You Scored: %v\n", score)
return
}
}
}
func inputRoutine(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
ch <- s.Text()
}
}()
return ch
}