Skip to content
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

Kadai3 1 ohsawa0515 #42

Open
wants to merge 2 commits 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
22 changes: 22 additions & 0 deletions kadai3/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions kadai3/typing/qa.go
Original file line number Diff line number Diff line change
@@ -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
}
}
45 changes: 45 additions & 0 deletions kadai3/typing/qa_test.go
Original file line number Diff line number Diff line change
@@ -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
}
78 changes: 78 additions & 0 deletions kadai3/typing/typing.go
Original file line number Diff line number Diff line change
@@ -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)
}