-
Notifications
You must be signed in to change notification settings - Fork 17
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
kadai3-1 yoheimiyamoto #46
base: master
Are you sure you want to change the base?
kadai3-1 yoheimiyamoto #46
Conversation
yoheimiyamoto
commented
Nov 25, 2018
- 標準出力に英単語を出す(出すものは自由)
- 標準入力から1行受け取る
- 制限時間内に何問解けたか表示する
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.
コメントしましたので、参考にしていただければと思います
// words -> 出題するワード一覧 | ||
func Play(r io.Reader, w io.Writer, words []string) { | ||
inputCh := make(chan string) | ||
inputDone := make(chan struct{}, 1) |
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.
特に値を入れていることはなさそうなので、サイズの指定は必要なさそうです
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.
了解です。修正しましたっ
} | ||
close(inputDone) // inputのチャネルを閉じる | ||
fmt.Fprintln(w, "タイムアウト。ゲーム終了") | ||
fmt.Println(s.Result()) |
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.
ここまで w io.Writer
にのみ利用してきているので、ここも os.Stdout
ではなく w
に書き込んだほうがテストしやすさ的にも良いと思います
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.
そうですねっ。こちらも修正しました。
fmt.Fprintln(w, "不正解!") | ||
s.addIncorrect() | ||
} | ||
case <-time.After(5 * time.Second): |
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.
一つのワード入力の 制限時間5秒
であるならばよいのですが、ゲーム自体の制限時間であれば、このforの外で time.After
を実行しておかないとforに入るたびに5秒からカウントしてしまいます
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.
こちらも修正しましたっ
} | ||
}() | ||
|
||
// 上記を以下のようにgoroutine使わずに記述するとゲームがフリーズしてしまう理由が理解できていないです。 |
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.
doneがcloseされるまでブロックすることになるので、inputの実行箇所でとまることになります
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.
ありがとうございます。理解できましたっ
}() | ||
|
||
// ゲームが終了した場合、inputも終了させる。 | ||
go func() { |
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.
goroutineとして実行しているので、この処理自体ほとんど意味がなさそうです