Skip to content

Commit 4789c98

Browse files
committed
kadai3-1 nKumaya
1 parent 1153a9d commit 4789c98

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

kadai3-1/nKumaya/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

kadai3-1/nKumaya/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 課題3-1 タイピングゲームを作ろう
2+
3+
## タイピングゲームの説明
4+
5+
```
6+
go run main.go
7+
```
8+
ランダムに果物の名前が表示され30秒のタイピングゲームを行う。
9+
制限時間になると入力した問題数と正解した問題数が出力される。
10+
11+
以下結果出力例
12+
13+
```
14+
total_input : 16 correct_answer : 15
15+
```

kadai3-1/nKumaya/main.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"math/rand"
8+
"os"
9+
"time"
10+
)
11+
12+
const (
13+
ExitCodeOK = iota
14+
)
15+
16+
var words = []string{"apple", "banana", "strawberry", "kiwi", "cherry", "melon", "plum", "pear", "pineapple", "grape", "peach"}
17+
18+
type inputer interface {
19+
input(io.Reader) <-chan string
20+
}
21+
22+
type question struct {
23+
}
24+
25+
type Vocabulary []string
26+
27+
type Score struct {
28+
correctInput int
29+
totalInput int
30+
}
31+
32+
func (w Vocabulary) chooseWord() string {
33+
index := rand.Intn(len(w))
34+
return w[index]
35+
}
36+
37+
func (q *question) input(r io.Reader) <-chan string {
38+
ch := make(chan string)
39+
go func() {
40+
s := bufio.NewScanner(r)
41+
for s.Scan() {
42+
ch <- s.Text()
43+
}
44+
close(ch)
45+
}()
46+
return ch
47+
}
48+
49+
func Run(candidates []string, i inputer) int {
50+
var score Score
51+
ch := i.input(os.Stdin)
52+
timeout := time.After(35 * time.Second)
53+
fmt.Println("---------Typing Start!! ---------")
54+
for {
55+
answer := Vocabulary.chooseWord(candidates)
56+
fmt.Println(answer)
57+
fmt.Print("> ")
58+
select {
59+
case input := <-ch:
60+
if answer == input {
61+
score.correctInput++
62+
fmt.Printf("correct!!\n\n")
63+
} else {
64+
fmt.Printf("failed!\n\n")
65+
}
66+
score.totalInput++
67+
case <-timeout:
68+
fmt.Printf("\n\n---------Timed Out---------\n\n")
69+
fmt.Printf("total_input : %d correct_answer : %d\n", score.totalInput, score.correctInput)
70+
return ExitCodeOK
71+
}
72+
}
73+
}
74+
75+
func main() {
76+
q := &question{}
77+
os.Exit(Run(words, q))
78+
}

kadai3-1/nKumaya/main_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"testing"
8+
)
9+
10+
var q = question{}
11+
12+
func TestInput(t *testing.T) {
13+
t.Helper()
14+
file, err := os.Open("testdata/sample.txt")
15+
if err != nil {
16+
fmt.Println(err)
17+
}
18+
defer file.Close()
19+
result := q.input(file)
20+
expects := []string{"banana", "apple"}
21+
for _, expected := range expects {
22+
r := <-result
23+
if r != expected {
24+
t.Error(r)
25+
t.Error(expected)
26+
}
27+
}
28+
}
29+
30+
type testQuestion struct{}
31+
32+
func (m *testQuestion) input(r io.Reader) <-chan string {
33+
ch := make(chan string)
34+
go func() {
35+
ch <- "banana"
36+
}()
37+
return ch
38+
}
39+
40+
func TestRun(t *testing.T) {
41+
t.Helper()
42+
m := &testQuestion{}
43+
result := Run(words, m)
44+
if result != ExitCodeOK {
45+
t.Error(result)
46+
}
47+
}

kadai3-1/nKumaya/testdata/sample.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
banana
2+
apple

0 commit comments

Comments
 (0)