-
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?
Changes from 4 commits
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,4 @@ | ||
タイピングゲームを作ろう | ||
標準出力に英単語を出す(出すものは自由) | ||
標準入力から1行受け取る | ||
制限時間内に何問解けたか表示する |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/YoheiMiyamoto/dojo4/kadai3-1/yoheimiyamoto/pingpong" | ||
) | ||
|
||
func main() { | ||
words := []string{"one", "two", "three", "four", "five"} | ||
pingpong.Play(os.Stdin, os.Stdout, words) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* | ||
Package pingpong ... | ||
|
||
*/ | ||
package pingpong |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package pingpong | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"time" | ||
) | ||
|
||
// Play ... | ||
// words -> 出題するワード一覧 | ||
func Play(r io.Reader, w io.Writer, words []string) { | ||
inputCh := make(chan string) | ||
inputDone := make(chan struct{}, 1) | ||
input(r, inputCh, inputDone) | ||
|
||
var s score | ||
|
||
fmt.Fprintln(w, "ゲームスタート!制限時間は5秒!") | ||
GAME: | ||
for { | ||
// wordsをすべて回答したらゲームを終了させる | ||
if s.count() == (len(words) - 1) { | ||
break GAME | ||
} | ||
|
||
word := words[s.count()] | ||
fmt.Fprintln(w, word) | ||
|
||
select { | ||
case i := <-inputCh: | ||
if i == word { | ||
fmt.Fprintln(w, "正解!") | ||
s.addCorrect() | ||
} else { | ||
fmt.Fprintln(w, "不正解!") | ||
s.addIncorrect() | ||
} | ||
case <-time.After(5 * time.Second): | ||
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. 一つのワード入力の 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. こちらも修正しましたっ |
||
break GAME | ||
} | ||
} | ||
close(inputDone) // inputのチャネルを閉じる | ||
fmt.Fprintln(w, "タイムアウト。ゲーム終了") | ||
fmt.Println(s.Result()) | ||
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. ここまで 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. そうですねっ。こちらも修正しました。 |
||
} | ||
|
||
// 標準入力から受け取ったテキストをchチャネルに送信。 | ||
func input(r io.Reader, ch chan<- string, done <-chan struct{}) { | ||
scanner := bufio.NewScanner(r) | ||
go func() { | ||
for scanner.Scan() { | ||
t := scanner.Text() | ||
ch <- t | ||
} | ||
}() | ||
|
||
// ゲームが終了した場合、inputも終了させる。 | ||
go func() { | ||
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. goroutineとして実行しているので、この処理自体ほとんど意味がなさそうです |
||
select { | ||
case <-done: | ||
return | ||
} | ||
}() | ||
|
||
// 上記を以下のようにgoroutine使わずに記述するとゲームがフリーズしてしまう理由が理解できていないです。 | ||
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. doneがcloseされるまでブロックすることになるので、inputの実行箇所でとまることになります 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. ありがとうございます。理解できましたっ |
||
// select { | ||
// case <-done: | ||
// return | ||
// } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pingpong |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pingpong | ||
|
||
import "fmt" | ||
|
||
// 正解、不正解のスコアをカウント | ||
type score struct { | ||
correct int | ||
incorrect int | ||
} | ||
|
||
// 正解をカウントアップ | ||
func (s *score) addCorrect() { | ||
s.correct++ | ||
} | ||
|
||
// 不正解をカウントアップ | ||
func (s *score) addIncorrect() { | ||
s.incorrect++ | ||
} | ||
|
||
func (s *score) count() int { | ||
return s.correct + s.incorrect | ||
} | ||
|
||
// 正解率の算出 | ||
func (s score) rate() float32 { | ||
if s.correct == 0 { | ||
return 0 | ||
} | ||
sum := float32(s.correct) + float32(s.incorrect) | ||
return float32(s.correct) / sum * 100 | ||
} | ||
|
||
// 結果を出力 | ||
func (s score) Result() string { | ||
sum := s.correct + s.incorrect | ||
return fmt.Sprintf(`正解率: %.2f %%(%d/%d)`, s.rate(), s.correct, sum) | ||
} |
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.
了解です。修正しましたっ