Skip to content

kadai3-1-pei #27

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 10 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
17 changes: 17 additions & 0 deletions kadai3-1/pei/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ROOT=github.com/gopherdojo/dojo6/kadai3-1/pei
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
33 changes: 33 additions & 0 deletions kadai3-1/pei/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# typing

## Build

```
$ make build
```

## Usage

```
$ ./typing
```

## Development

### Build

```
$ make build
```

### Test

```
$ make test
```

### Clean

```
$ make clean
```
52 changes: 52 additions & 0 deletions kadai3-1/pei/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"os"
"time"

"github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/typing"
"github.com/gopherdojo/dojo6/kadai3-1/pei/pkg/wordsreader"
)

const (
ExitCodeOK = 0
ExitCodeError = 1
)

func main() {
os.Exit(execute())
}

func execute() int {
fmt.Println("Start Typing Game!")

wr := &wordsreader.WordsReader{FileName: "./textdata/words.txt"}
words, err := wr.Read()
if err != nil {
fmt.Errorf("%v", err)
return ExitCodeError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これだとどういうエラーが起きたのか分からないのでは?

}

typingCh := typing.Question(words)
timerCh := time.After(5 * time.Second)

var (
counter int
correctCounter int
)

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
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returnなくて動きます?

}
41 changes: 41 additions & 0 deletions kadai3-1/pei/pkg/typing/typing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package typing

import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)

// Question receives words and returns isCorrectCh
func Question(words []string) <-chan bool {
isCorrectCh := make(chan bool)

go func() {
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラー処理は?

answer := stdin.Text()

if err := stdin.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
isCorrectCh <- false
}

if word == answer {
isCorrectCh <- true
} else {
isCorrectCh <- false
}
}
}()

return isCorrectCh
}
3 changes: 3 additions & 0 deletions kadai3-1/pei/pkg/wordsreader/testdata/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a
b
c
1 change: 1 addition & 0 deletions kadai3-1/pei/pkg/wordsreader/testdata/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
33 changes: 33 additions & 0 deletions kadai3-1/pei/pkg/wordsreader/wordsreader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package wordsreader

import (
"bufio"
"os"
)

// WordsReader has FileName
type WordsReader struct {
FileName string
}

// Read file
func (wr WordsReader) Read() ([]string, error) {
var words []string

fp, err := os.Open(wr.FileName)
if err != nil {
return nil, err
}
defer fp.Close()

scanner := bufio.NewScanner(fp)
for scanner.Scan() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラー処理

words = append(words, scanner.Text())
}

if err := scanner.Err(); err != nil {
return nil, err
}

return words, nil
}
32 changes: 32 additions & 0 deletions kadai3-1/pei/pkg/wordsreader/wordsreader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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()

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)
}
})
}
}
40 changes: 40 additions & 0 deletions kadai3-1/pei/textdata/words.txt
Original file line number Diff line number Diff line change
@@ -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