From cbb79b5763d54f86b5bc5a12a804d3c2228ce4ac Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Mon, 5 Aug 2019 22:03:05 +0900 Subject: [PATCH 01/10] Add textdata --- kadai3-1/pei/textdata/words.txt | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 kadai3-1/pei/textdata/words.txt diff --git a/kadai3-1/pei/textdata/words.txt b/kadai3-1/pei/textdata/words.txt new file mode 100644 index 0000000..59efafc --- /dev/null +++ b/kadai3-1/pei/textdata/words.txt @@ -0,0 +1,40 @@ +fix +add +remove +use +update +support +merge +make +move +don't +check +change +allow +clean +set +convert +rename +do +revert +avoid +new +unused +static +empty +old +small +initial +local +wrong +common +other +dead +rid +possible +unneeded +same +global +invalid +specific +extra From 44afd7ddc41e9bb27afb7133ae6245952cec6d7c Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Mon, 5 Aug 2019 22:51:36 +0900 Subject: [PATCH 02/10] Add Makefile --- kadai3-1/pei/Makefile | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 kadai3-1/pei/Makefile diff --git a/kadai3-1/pei/Makefile b/kadai3-1/pei/Makefile new file mode 100644 index 0000000..a287630 --- /dev/null +++ b/kadai3-1/pei/Makefile @@ -0,0 +1,17 @@ +ROOT=github.com/gopherdojo/dojo6/kadai3-1/pei +BIN=bin/typing +MAIN=main.go +TEST=... + +.PHONY: build +build: ${MAIN} + go build -o ${BIN} ${GOPATH}/src/${ROOT}/$? + +.PHONY: test +test: + go test -v -cover ${ROOT}/${TEST} + +.PHONY: clean +clean: + rm ${BIN} + go clean From d27876c6ea433cd0af93ce8979dd9dda80df0423 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Tue, 6 Aug 2019 02:43:34 +0900 Subject: [PATCH 03/10] Add Typing Game --- kadai3-1/pei/main.go | 44 +++++++++++++++++++++++++++++++ kadai3-1/pei/pkg/typing/typing.go | 34 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 kadai3-1/pei/main.go create mode 100644 kadai3-1/pei/pkg/typing/typing.go diff --git a/kadai3-1/pei/main.go b/kadai3-1/pei/main.go new file mode 100644 index 0000000..a6470af --- /dev/null +++ b/kadai3-1/pei/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "os" + "time" + + "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/typing" +) + +const ( + ExitCodeOK = 0 + ExitCodeError = 1 +) + +func main() { + os.Exit(execute()) +} + +func execute() int { + fmt.Println("Start Typing Game!") + + words := []string{"a", "b", "c"} + + typingCh := typing.Question(words) + timerCh := time.After(5 * time.Second) + + counter := 0 + correctCounter := 0 + + for { + counter++ + select { + case isCorrect := <-typingCh: + if isCorrect { + correctCounter++ + } + case <-timerCh: + fmt.Printf("\nScore: %d/%d \n", correctCounter, counter) + fmt.Println("End Typing Game!") + return ExitCodeOK + } + } +} diff --git a/kadai3-1/pei/pkg/typing/typing.go b/kadai3-1/pei/pkg/typing/typing.go new file mode 100644 index 0000000..e9255ed --- /dev/null +++ b/kadai3-1/pei/pkg/typing/typing.go @@ -0,0 +1,34 @@ +package typing + +import ( + "bufio" + "fmt" + "math/rand" + "os" + "time" +) + +func Question(words []string) <-chan bool { + isCorrectCh := make(chan bool) + + go func() { + stdin := bufio.NewScanner(os.Stdin) + rand.Seed(time.Now().UnixNano()) + + for { + word := words[rand.Intn(len(words))] + fmt.Println(word) + + stdin.Scan() + answer := stdin.Text() + if word == answer { + isCorrectCh <- true + } else { + isCorrectCh <- false + } + } + close(isCorrectCh) + }() + + return isCorrectCh +} From 2aa757ad8d0f2dc5a03d70f48d0a540ef412d34a Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Wed, 7 Aug 2019 01:26:16 +0900 Subject: [PATCH 04/10] Add pkg/wordreader --- .../pei/pkg/wordsreader/testdata/test1.txt | 3 ++ .../pei/pkg/wordsreader/testdata/test2.txt | 1 + kadai3-1/pei/pkg/wordsreader/wordsreader.go | 29 +++++++++++++++++++ .../pei/pkg/wordsreader/wordsreader_test.go | 24 +++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 kadai3-1/pei/pkg/wordsreader/testdata/test1.txt create mode 100644 kadai3-1/pei/pkg/wordsreader/testdata/test2.txt create mode 100644 kadai3-1/pei/pkg/wordsreader/wordsreader.go create mode 100644 kadai3-1/pei/pkg/wordsreader/wordsreader_test.go diff --git a/kadai3-1/pei/pkg/wordsreader/testdata/test1.txt b/kadai3-1/pei/pkg/wordsreader/testdata/test1.txt new file mode 100644 index 0000000..de98044 --- /dev/null +++ b/kadai3-1/pei/pkg/wordsreader/testdata/test1.txt @@ -0,0 +1,3 @@ +a +b +c diff --git a/kadai3-1/pei/pkg/wordsreader/testdata/test2.txt b/kadai3-1/pei/pkg/wordsreader/testdata/test2.txt new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/kadai3-1/pei/pkg/wordsreader/testdata/test2.txt @@ -0,0 +1 @@ +a diff --git a/kadai3-1/pei/pkg/wordsreader/wordsreader.go b/kadai3-1/pei/pkg/wordsreader/wordsreader.go new file mode 100644 index 0000000..db8641b --- /dev/null +++ b/kadai3-1/pei/pkg/wordsreader/wordsreader.go @@ -0,0 +1,29 @@ +package wordsreader + +import ( + "bufio" + "os" +) + +// WordsReader has FileName +type WordsReader struct { + FileName string +} + +// Read file +func (wr WordsReader) Read() ([]string, error) { + words := make([]string, 0) + + fp, err := os.Open(wr.FileName) + if err != nil { + return nil, err + } + defer fp.Close() + + scanner := bufio.NewScanner(fp) + for scanner.Scan() { + words = append(words, scanner.Text()) + } + + return words, nil +} diff --git a/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go new file mode 100644 index 0000000..ffcb2a6 --- /dev/null +++ b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go @@ -0,0 +1,24 @@ +package wordsreader + +import ( + "reflect" + "testing" +) + +func TestWordsReader_Read(t *testing.T) { + cases := []struct {fileName string; expected []string}{ + {fileName: "./testdata/test1.txt", expected: []string{"a", "b", "c"}}, + {fileName: "./testdata/test2.txt", expected: []string{"a"}}, + } + + for _, c := range cases { + c := c + t.Run(c.fileName, func(t *testing.T) { + t.Parallel() + wr := WordsReader{FileName: c.fileName} + if actual, _ := wr.Read(); !reflect.DeepEqual(c.expected, actual) { + t.Errorf("want %v, got %v", c.expected, actual) + } + }) + } +} From 5d5039472f77f98af1fb4c306f1871a023f1d40e Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Wed, 7 Aug 2019 01:26:58 +0900 Subject: [PATCH 05/10] Update Typing Game --- kadai3-1/pei/main.go | 7 ++++++- kadai3-1/pei/pkg/typing/typing.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/kadai3-1/pei/main.go b/kadai3-1/pei/main.go index a6470af..20d1b56 100644 --- a/kadai3-1/pei/main.go +++ b/kadai3-1/pei/main.go @@ -5,6 +5,7 @@ import ( "os" "time" + "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/wordsreader" "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/typing" ) @@ -20,7 +21,11 @@ func main() { func execute() int { fmt.Println("Start Typing Game!") - words := []string{"a", "b", "c"} + wr := &wordsreader.WordsReader{FileName: "./textdata/words.txt"} + words, err := wr.Read() + if err != nil { + return ExitCodeError + } typingCh := typing.Question(words) timerCh := time.After(5 * time.Second) diff --git a/kadai3-1/pei/pkg/typing/typing.go b/kadai3-1/pei/pkg/typing/typing.go index e9255ed..c26f41b 100644 --- a/kadai3-1/pei/pkg/typing/typing.go +++ b/kadai3-1/pei/pkg/typing/typing.go @@ -8,6 +8,7 @@ import ( "time" ) +// Question receives words and returns isCorrectCh func Question(words []string) <-chan bool { isCorrectCh := make(chan bool) From 46dfebe918cd8b45e3dbc853f5c6060d86c96c7f Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Wed, 7 Aug 2019 01:28:27 +0900 Subject: [PATCH 06/10] go fmt --- kadai3-1/pei/main.go | 2 +- kadai3-1/pei/pkg/wordsreader/wordsreader_test.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kadai3-1/pei/main.go b/kadai3-1/pei/main.go index 20d1b56..ee211a1 100644 --- a/kadai3-1/pei/main.go +++ b/kadai3-1/pei/main.go @@ -5,8 +5,8 @@ import ( "os" "time" - "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/wordsreader" "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/typing" + "github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/wordsreader" ) const ( diff --git a/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go index ffcb2a6..3a16241 100644 --- a/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go +++ b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go @@ -6,7 +6,10 @@ import ( ) func TestWordsReader_Read(t *testing.T) { - cases := []struct {fileName string; expected []string}{ + cases := []struct { + fileName string + expected []string + }{ {fileName: "./testdata/test1.txt", expected: []string{"a", "b", "c"}}, {fileName: "./testdata/test2.txt", expected: []string{"a"}}, } From 96248fd9b35f4c4491b66e9c6dc189d8af1504ef Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Wed, 7 Aug 2019 01:42:51 +0900 Subject: [PATCH 07/10] Add README --- kadai3-1/pei/Makefile | 2 +- kadai3-1/pei/README.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 kadai3-1/pei/README.md diff --git a/kadai3-1/pei/Makefile b/kadai3-1/pei/Makefile index a287630..dfd5492 100644 --- a/kadai3-1/pei/Makefile +++ b/kadai3-1/pei/Makefile @@ -1,5 +1,5 @@ ROOT=github.com/gopherdojo/dojo6/kadai3-1/pei -BIN=bin/typing +BIN=typing MAIN=main.go TEST=... diff --git a/kadai3-1/pei/README.md b/kadai3-1/pei/README.md new file mode 100644 index 0000000..7d5f11d --- /dev/null +++ b/kadai3-1/pei/README.md @@ -0,0 +1,33 @@ +# typing + +## Build + +``` +$ make build +``` + +## Usage + +``` +$ ./typing +``` + +## Development + +### Build + +``` +$ make build +``` + +### Test + +``` +$ make test +``` + +### Clean + +``` +$ make clean +``` From 5ec1ee36e5f4314169b64616c982ce881e88252b Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sun, 11 Aug 2019 22:58:31 +0900 Subject: [PATCH 08/10] Update pkg/wordsreader --- kadai3-1/pei/pkg/wordsreader/wordsreader.go | 6 +++++- kadai3-1/pei/pkg/wordsreader/wordsreader_test.go | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/kadai3-1/pei/pkg/wordsreader/wordsreader.go b/kadai3-1/pei/pkg/wordsreader/wordsreader.go index db8641b..a5d8124 100644 --- a/kadai3-1/pei/pkg/wordsreader/wordsreader.go +++ b/kadai3-1/pei/pkg/wordsreader/wordsreader.go @@ -12,7 +12,7 @@ type WordsReader struct { // Read file func (wr WordsReader) Read() ([]string, error) { - words := make([]string, 0) + var words []string fp, err := os.Open(wr.FileName) if err != nil { @@ -25,5 +25,9 @@ func (wr WordsReader) Read() ([]string, error) { words = append(words, scanner.Text()) } + if err := scanner.Err(); err != nil { + return nil, err + } + return words, nil } diff --git a/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go index 3a16241..15463fd 100644 --- a/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go +++ b/kadai3-1/pei/pkg/wordsreader/wordsreader_test.go @@ -18,8 +18,13 @@ func TestWordsReader_Read(t *testing.T) { c := c t.Run(c.fileName, func(t *testing.T) { t.Parallel() - wr := WordsReader{FileName: c.fileName} - if actual, _ := wr.Read(); !reflect.DeepEqual(c.expected, actual) { + + actual, err := WordsReader{FileName: c.fileName}.Read() + if err != nil { + t.Errorf("failed to open file") + } + + if !reflect.DeepEqual(c.expected, actual) { t.Errorf("want %v, got %v", c.expected, actual) } }) From db341b67e17faa551ece79ad7f9835617101470a Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sun, 11 Aug 2019 22:59:17 +0900 Subject: [PATCH 09/10] Update pkg/typing --- kadai3-1/pei/pkg/typing/typing.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kadai3-1/pei/pkg/typing/typing.go b/kadai3-1/pei/pkg/typing/typing.go index c26f41b..fc2451f 100644 --- a/kadai3-1/pei/pkg/typing/typing.go +++ b/kadai3-1/pei/pkg/typing/typing.go @@ -16,19 +16,25 @@ func Question(words []string) <-chan bool { stdin := bufio.NewScanner(os.Stdin) rand.Seed(time.Now().UnixNano()) + defer close(isCorrectCh) for { word := words[rand.Intn(len(words))] fmt.Println(word) stdin.Scan() answer := stdin.Text() + + if err := stdin.Err(); err != nil { + fmt.Fprintln(os.Stderr, err) + isCorrectCh <- false + } + if word == answer { isCorrectCh <- true } else { isCorrectCh <- false } } - close(isCorrectCh) }() return isCorrectCh From 2283dbbc19895b22085b467329977ed8c2e29a6e Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sun, 11 Aug 2019 22:59:33 +0900 Subject: [PATCH 10/10] Update main --- kadai3-1/pei/main.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kadai3-1/pei/main.go b/kadai3-1/pei/main.go index ee211a1..bb1531e 100644 --- a/kadai3-1/pei/main.go +++ b/kadai3-1/pei/main.go @@ -24,14 +24,17 @@ func execute() int { wr := &wordsreader.WordsReader{FileName: "./textdata/words.txt"} words, err := wr.Read() if err != nil { + fmt.Errorf("%v", err) return ExitCodeError } typingCh := typing.Question(words) timerCh := time.After(5 * time.Second) - counter := 0 - correctCounter := 0 + var ( + counter int + correctCounter int + ) for { counter++