diff --git a/kadai3-1/yoheimiyamoto/README.md b/kadai3-1/yoheimiyamoto/README.md new file mode 100644 index 0000000..f17eb64 --- /dev/null +++ b/kadai3-1/yoheimiyamoto/README.md @@ -0,0 +1,4 @@ +タイピングゲームを作ろう +標準出力に英単語を出す(出すものは自由) +標準入力から1行受け取る +制限時間内に何問解けたか表示する diff --git a/kadai3-1/yoheimiyamoto/main.go b/kadai3-1/yoheimiyamoto/main.go new file mode 100644 index 0000000..86f67f1 --- /dev/null +++ b/kadai3-1/yoheimiyamoto/main.go @@ -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) + +} diff --git a/kadai3-1/yoheimiyamoto/pingpong/doc.go b/kadai3-1/yoheimiyamoto/pingpong/doc.go new file mode 100644 index 0000000..3312dd2 --- /dev/null +++ b/kadai3-1/yoheimiyamoto/pingpong/doc.go @@ -0,0 +1,5 @@ +/* +Package pingpong ... + +*/ +package pingpong diff --git a/kadai3-1/yoheimiyamoto/pingpong/pingpong.go b/kadai3-1/yoheimiyamoto/pingpong/pingpong.go new file mode 100644 index 0000000..8daa115 --- /dev/null +++ b/kadai3-1/yoheimiyamoto/pingpong/pingpong.go @@ -0,0 +1,65 @@ +package pingpong + +import ( + "bufio" + "fmt" + "io" + "time" +) + +// Play ... +// words -> 出題するワード一覧 +func Play(r io.Reader, w io.Writer, words []string) { + inputCh := make(chan string) + scoreCh := make(chan score) + + input(r, inputCh) + + fmt.Fprintln(w, "ゲームスタート!制限時間は5秒!") + go game(w, words, inputCh, scoreCh) + + s := <-scoreCh + fmt.Fprintln(w, "ゲーム終了") + fmt.Fprintln(w, s.Result()) +} + +func game(w io.Writer, words []string, inputCh <-chan string, scoreCh chan<- score) { + var s score + + go func() { + select { + case <-time.After(5 * time.Second): + fmt.Fprintln(w, "タイムアウト!") + scoreCh <- s + } + }() + + for { + // wordsをすべて回答したらゲームを終了させる + if s.count() == len(words) { + scoreCh <- s + break + } + word := words[s.count()] + fmt.Fprintln(w, word) + i := <-inputCh + if i == word { + fmt.Fprintln(w, "正解!") + s.addCorrect() + } else { + fmt.Fprintln(w, "不正解!") + s.addIncorrect() + } + } +} + +// 標準入力から受け取ったテキストをchチャネルに送信。 +func input(r io.Reader, ch chan<- string) { + scanner := bufio.NewScanner(r) + go func() { + for scanner.Scan() { + t := scanner.Text() + ch <- t + } + }() +} diff --git a/kadai3-1/yoheimiyamoto/pingpong/pingpong_test.go b/kadai3-1/yoheimiyamoto/pingpong/pingpong_test.go new file mode 100644 index 0000000..dd4e821 --- /dev/null +++ b/kadai3-1/yoheimiyamoto/pingpong/pingpong_test.go @@ -0,0 +1 @@ +package pingpong diff --git a/kadai3-1/yoheimiyamoto/pingpong/score.go b/kadai3-1/yoheimiyamoto/pingpong/score.go new file mode 100644 index 0000000..9e521de --- /dev/null +++ b/kadai3-1/yoheimiyamoto/pingpong/score.go @@ -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) +}