Skip to content

kadai3-1 yoheimiyamoto - #46

Open
yoheimiyamoto wants to merge 5 commits into
gopherdojo:masterfrom
yoheimiyamoto:kadai3-1-yoheimiyamoto
Open

kadai3-1 yoheimiyamoto#46
yoheimiyamoto wants to merge 5 commits into
gopherdojo:masterfrom
yoheimiyamoto:kadai3-1-yoheimiyamoto

Conversation

@yoheimiyamoto

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

@mizkei mizkei left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コメントしましたので、参考にしていただければと思います

// words -> 出題するワード一覧
func Play(r io.Reader, w io.Writer, words []string) {
inputCh := make(chan string)
inputDone := make(chan struct{}, 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

特に値を入れていることはなさそうなので、サイズの指定は必要なさそうです

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

了解です。修正しましたっ

}
close(inputDone) // inputのチャネルを閉じる
fmt.Fprintln(w, "タイムアウト。ゲーム終了")
fmt.Println(s.Result())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここまで w io.Writer にのみ利用してきているので、ここも os.Stdout ではなく w に書き込んだほうがテストしやすさ的にも良いと思います

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

そうですねっ。こちらも修正しました。

fmt.Fprintln(w, "不正解!")
s.addIncorrect()
}
case <-time.After(5 * time.Second):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一つのワード入力の 制限時間5秒 であるならばよいのですが、ゲーム自体の制限時間であれば、このforの外で time.After を実行しておかないとforに入るたびに5秒からカウントしてしまいます

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

こちらも修正しましたっ

}
}()

// 上記を以下のようにgoroutine使わずに記述するとゲームがフリーズしてしまう理由が理解できていないです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

doneがcloseされるまでブロックすることになるので、inputの実行箇所でとまることになります

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ありがとうございます。理解できましたっ

}()

// ゲームが終了した場合、inputも終了させる。
go func() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

goroutineとして実行しているので、この処理自体ほとんど意味がなさそうです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants