Skip to content

Commit

Permalink
kadai3-1 nKumaya
Browse files Browse the repository at this point in the history
  • Loading branch information
nKumaya committed Sep 8, 2018
1 parent 1153a9d commit 4789c98
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions kadai3-1/nKumaya/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
15 changes: 15 additions & 0 deletions kadai3-1/nKumaya/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 課題3-1 タイピングゲームを作ろう

## タイピングゲームの説明

```
go run main.go
```
ランダムに果物の名前が表示され30秒のタイピングゲームを行う。
制限時間になると入力した問題数と正解した問題数が出力される。

以下結果出力例

```
total_input : 16 correct_answer : 15
```
78 changes: 78 additions & 0 deletions kadai3-1/nKumaya/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"bufio"
"fmt"
"io"
"math/rand"
"os"
"time"
)

const (
ExitCodeOK = iota
)

var words = []string{"apple", "banana", "strawberry", "kiwi", "cherry", "melon", "plum", "pear", "pineapple", "grape", "peach"}

type inputer interface {
input(io.Reader) <-chan string
}

type question struct {
}

type Vocabulary []string

type Score struct {
correctInput int
totalInput int
}

func (w Vocabulary) chooseWord() string {
index := rand.Intn(len(w))
return w[index]
}

func (q *question) input(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
ch <- s.Text()
}
close(ch)
}()
return ch
}

func Run(candidates []string, i inputer) int {
var score Score
ch := i.input(os.Stdin)
timeout := time.After(35 * time.Second)
fmt.Println("---------Typing Start!! ---------")
for {
answer := Vocabulary.chooseWord(candidates)
fmt.Println(answer)
fmt.Print("> ")
select {
case input := <-ch:
if answer == input {
score.correctInput++
fmt.Printf("correct!!\n\n")
} else {
fmt.Printf("failed!\n\n")
}
score.totalInput++
case <-timeout:
fmt.Printf("\n\n---------Timed Out---------\n\n")
fmt.Printf("total_input : %d correct_answer : %d\n", score.totalInput, score.correctInput)
return ExitCodeOK
}
}
}

func main() {
q := &question{}
os.Exit(Run(words, q))
}
47 changes: 47 additions & 0 deletions kadai3-1/nKumaya/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"io"
"os"
"testing"
)

var q = question{}

func TestInput(t *testing.T) {
t.Helper()
file, err := os.Open("testdata/sample.txt")
if err != nil {
fmt.Println(err)
}
defer file.Close()
result := q.input(file)
expects := []string{"banana", "apple"}
for _, expected := range expects {
r := <-result
if r != expected {
t.Error(r)
t.Error(expected)
}
}
}

type testQuestion struct{}

func (m *testQuestion) input(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
ch <- "banana"
}()
return ch
}

func TestRun(t *testing.T) {
t.Helper()
m := &testQuestion{}
result := Run(words, m)
if result != ExitCodeOK {
t.Error(result)
}
}
2 changes: 2 additions & 0 deletions kadai3-1/nKumaya/testdata/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
banana
apple

0 comments on commit 4789c98

Please sign in to comment.