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

dojo7 [課題3-1] タイピングゲームを作ろう #33

Open
wants to merge 4 commits into
base: kadai2-asuke-yasukuni
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
1 change: 1 addition & 0 deletions kadai3_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
16 changes: 16 additions & 0 deletions kadai3_1/asuke-yasukuni/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## dojo7 [課題3-1] タイピングゲームを作ろう

課題3-2が重いため、かなり簡素な感じにしました。
インターフェースの概念に慣れてないせいで、チャネルが絡むテストを書くのが以外に難しかったです。

最終的には割とちゃんと書けた気がします(気がするだけかもしれない)。

## 回答

- 標準出力に英単語を出す(出すものは自由)
- OK
- 標準入力から1行受け取る
- OK
- 制限時間内に何問解けたか表示する
- OK

3 changes: 3 additions & 0 deletions kadai3_1/asuke-yasukuni/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dojo7/kadai3_1/asuke-yasukuni

go 1.12
13 changes: 13 additions & 0 deletions kadai3_1/asuke-yasukuni/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"github.com/dojo7/kadai3_1/asuke-yasukuni/typing"
)

func main() {
typing.SetGame([]string{
"tanuki",
"gouge",
"english",
}).Do()
}
86 changes: 86 additions & 0 deletions kadai3_1/asuke-yasukuni/typing/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package typing

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

type InputReader interface {
Input() <-chan string
}

type Reader struct{}
Copy link
Member

Choose a reason for hiding this comment

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

こうかいしなくても良さそう

Copy link
Author

Choose a reason for hiding this comment

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

確かに


func (r *Reader) Input() <-chan string {
Copy link
Member

Choose a reason for hiding this comment

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

empty structの場合はレシーバポインタにしちゃうとポインタ分スタックにメモリを確保するので func (Reader) Input <-chan stringのようにしておくと良さそう

Copy link
Author

Choose a reason for hiding this comment

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

おお、なるほど、そういう書き方が。

ch := make(chan string)

go func() {
Copy link
Member

Choose a reason for hiding this comment

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

タイムアウトした際にこのゴールーチンが終了しなさそう。

Copy link
Author

Choose a reason for hiding this comment

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

おふ。。

s := bufio.NewScanner(os.Stdin)
for s.Scan() {
Copy link
Member

Choose a reason for hiding this comment

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

エラー処理

Copy link
Author

Choose a reason for hiding this comment

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

あー。 s.Err() で取れるのか。

ch <- s.Text()
}
close(ch)
}()
return ch
}

type OutputWriter interface {
Output(string)
}

type Writer struct{}

func (w Writer) Output(outputText string) {
Copy link
Member

Choose a reason for hiding this comment

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

func (Writer)

fmt.Println(outputText)
}

type Game struct {
Time time.Duration
Words []string
Reader InputReader
Writer OutputWriter
}

func SetGame(words []string) *Game {
return &Game{
Time: 5,
Words: words,
Reader: &Reader{},
Writer: Writer{},
}
}

func (g *Game) Do() (clearCount int) {
ch := g.Reader.Input()
timeUp := time.After(g.Time * time.Second)

t := time.Now().UnixNano()
rand.Seed(t)

L:
for {
i := rand.Intn(len(g.Words))
selectWord := g.Words[i]
g.Writer.Output(selectWord)

select {
case text := <-ch:
if selectWord == text {
clearCount++
g.Writer.Output("OK")
} else {
g.Writer.Output("NG")
}
case <-timeUp:
g.Writer.Output("========")
g.Writer.Output("time up")
g.Writer.Output(fmt.Sprintf("clear count %d", clearCount))
break L
}
}

return
}
55 changes: 55 additions & 0 deletions kadai3_1/asuke-yasukuni/typing/game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package typing

import (
"testing"
"time"
)

type TestReader struct {
TestWord []string
}

func (r *TestReader) Input() <-chan string {
ch := make(chan string)
go func() {
for _, word := range r.TestWord {
ch <- word
}
time.Sleep(1 * time.Second)
close(ch)
}()
return ch
}

type TestWriter struct{}

func (w TestWriter) Output(outputText string) {}

func TestTyping(t *testing.T) {

var testCase = []struct {
Name string
Question []string
Word []string
Count int
}{
{"all clear", []string{"hoge", "hoge", "hoge", "hoge"}, []string{"hoge", "hoge", "hoge", "hoge"}, 4},
{"all fail", []string{"neeko", "enga", "els", "gezi"}, []string{"neko", "english", "eigo", "genzin"}, 0},
{"1 clear", []string{"hoge"}, []string{"neko", "hoge", "eigo", "genzin"}, 1},
}

for _, tc := range testCase {
game := &Game{
Time: 1,
Words: tc.Question,
Reader: &TestReader{TestWord: tc.Word},
Writer: TestWriter{},
}
t.Run(tc.Name, func(t *testing.T) {
count := game.Do()
if count != tc.Count {
t.Errorf("%d == %d not equal", count, tc.Count)
}
})
}
}