From ff63b62a7c66b85b35b1fa1921769350aa767b2d Mon Sep 17 00:00:00 2001 From: Shuichi Ohsawa Date: Mon, 14 May 2018 16:37:34 +0900 Subject: [PATCH 1/2] add files --- kadai3/main.go | 22 ++++++++++++ kadai3/typing/qa.go | 41 ++++++++++++++++++++++ kadai3/typing/typing.go | 78 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 kadai3/main.go create mode 100644 kadai3/typing/qa.go create mode 100644 kadai3/typing/typing.go diff --git a/kadai3/main.go b/kadai3/main.go new file mode 100644 index 0000000..eaf000a --- /dev/null +++ b/kadai3/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "context" + "flag" + "os" + "time" + + "github.com/ohsawa0515/gotyping/typing" +) + +var timeout int + +func main() { + flag.IntVar(&timeout, "t", 30, "Timeout(sec)") + flag.Parse() + + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + defer cancel() + + typing.Run(ctx, os.Stdin) +} diff --git a/kadai3/typing/qa.go b/kadai3/typing/qa.go new file mode 100644 index 0000000..e8104eb --- /dev/null +++ b/kadai3/typing/qa.go @@ -0,0 +1,41 @@ +package typing + +type QA struct { + Good int + Bad int + Counter int + Questions []string +} + +// NewQA - +func NewQA(questions []string) *QA { + return &QA{ + Good: 0, + Bad: 0, + Counter: -1, + Questions: questions, + } +} + +// MakeQuestion makes question from given English word list. +func (qa *QA) MakeQuestion() string { + // 出題範囲数を超えたら初めに戻る + if len(qa.Questions) <= qa.Counter+1 { + qa.Counter = 0 + } else { + qa.Counter++ + } + + return qa.Questions[qa.Counter] +} + +// CheckAnswer returns whether or not it matches the answer of the input. +func (qa *QA) CheckAnswer(question, answer string) bool { + if question == answer { + qa.Good++ + return true + } else { + qa.Bad++ + return false + } +} diff --git a/kadai3/typing/typing.go b/kadai3/typing/typing.go new file mode 100644 index 0000000..38e3a99 --- /dev/null +++ b/kadai3/typing/typing.go @@ -0,0 +1,78 @@ +package typing + +import ( + "bufio" + "context" + "fmt" + "io" +) + +var words = []string{ + "advertisement", + "lid", + "southeast", + "perish", + "inhabit", + "extent", + "room", + "balance", + "onto", + "breeze", + "protein", + "genetic", + "setup", + "kindergarten", + "satisfactory", + "appetite", + "civilization", + "bookcase", + "galaxy", + "suburban", + "infectious", + "jerk", +} + +const CORRECT_MESSAGE = "✓ That's right!" +const INCORRECT_MESSAGE = "✗ Oh, bad" + +func read(r io.Reader) <-chan string { + ch := make(chan string) + go func() { + defer close(ch) + s := bufio.NewScanner(r) + for s.Scan() { + ch <- s.Text() + } + }() + + return ch +} + +// Run starts typing game. +func Run(ctx context.Context, r io.Reader) { + qa := NewQA(words) + ch := read(r) +LOOP: + for { + question := qa.MakeQuestion() + fmt.Printf("%s > ", question) + + select { + case answer, ok := <-ch: + if !ok { + break LOOP + } + if qa.CheckAnswer(question, answer) { + fmt.Println(CORRECT_MESSAGE) + } else { + fmt.Println(INCORRECT_MESSAGE) + } + case <-ctx.Done(): + fmt.Println("Timeout") + break LOOP + } + } + + fmt.Printf("Good: %d\n", qa.Good) + fmt.Printf("Bad: %d\n", qa.Bad) +} From c202a0d9b4a8dea8b4a11d840741005f2fa5ebc9 Mon Sep 17 00:00:00 2001 From: Shuichi Ohsawa Date: Mon, 14 May 2018 16:38:05 +0900 Subject: [PATCH 2/2] add test --- kadai3/typing/qa_test.go | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 kadai3/typing/qa_test.go diff --git a/kadai3/typing/qa_test.go b/kadai3/typing/qa_test.go new file mode 100644 index 0000000..c01dce5 --- /dev/null +++ b/kadai3/typing/qa_test.go @@ -0,0 +1,45 @@ +package typing + +import ( + "fmt" +) + +var testWords = []string{ + "abcdefg", + "hijk", + "lmnopqastu", +} + +var testAnswers = []string{ + "abcdefg", + "hijl", // typo + "lmnopqastu", +} + +func ExampleQA_MakeQuestion() { + qa := NewQA(testWords) + + for i := 0; i < len(testWords)+2; i++ { + fmt.Println(qa.MakeQuestion()) + } + + // Output: + // abcdefg + // hijk + // lmnopqastu + // abcdefg + // hijk +} + +func ExampleQA_CheckAnswer() { + qa := NewQA(testWords) + for _, a := range testAnswers { + q := qa.MakeQuestion() + fmt.Println(qa.CheckAnswer(q, a)) + } + + // Output: + // true + // false + // true +}