diff --git a/kadai3-1/nKumaya/.gitignore b/kadai3-1/nKumaya/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/kadai3-1/nKumaya/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/kadai3-1/nKumaya/README.md b/kadai3-1/nKumaya/README.md new file mode 100644 index 0000000..24ae34f --- /dev/null +++ b/kadai3-1/nKumaya/README.md @@ -0,0 +1,15 @@ +# 課題3-1 タイピングゲームを作ろう + +## タイピングゲームの説明 + +``` +go run main.go +``` +ランダムに果物の名前が表示され30秒のタイピングゲームを行う。 +制限時間になると入力した問題数と正解した問題数が出力される。 + +以下結果出力例 + +``` +total_input : 16 correct_answer : 15 +``` diff --git a/kadai3-1/nKumaya/main.go b/kadai3-1/nKumaya/main.go new file mode 100644 index 0000000..02cfa21 --- /dev/null +++ b/kadai3-1/nKumaya/main.go @@ -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)) +} diff --git a/kadai3-1/nKumaya/main_test.go b/kadai3-1/nKumaya/main_test.go new file mode 100644 index 0000000..fed8cdc --- /dev/null +++ b/kadai3-1/nKumaya/main_test.go @@ -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) + } +} diff --git a/kadai3-1/nKumaya/testdata/sample.txt b/kadai3-1/nKumaya/testdata/sample.txt new file mode 100644 index 0000000..e4ceb84 --- /dev/null +++ b/kadai3-1/nKumaya/testdata/sample.txt @@ -0,0 +1,2 @@ +banana +apple