Skip to content

kadai3-1 nKumaya #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(30 * 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