-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
kadai3-1-pei #27
Changes from all commits
cbb79b5
44afd7d
d27876c
2aa757a
5d50394
46dfebe
96248fd
5ec1ee3
db341b6
2283dbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||
``` |
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 | ||
} | ||
|
||
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 | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returnなくて動きます? |
||
} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a | ||
b | ||
c |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a |
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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) | ||
} | ||
}) | ||
} | ||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これだとどういうエラーが起きたのか分からないのでは?