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

kadai3-1 yoheimiyamoto #46

Open
wants to merge 5 commits into
base: master
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
4 changes: 4 additions & 0 deletions kadai3-1/yoheimiyamoto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
タイピングゲームを作ろう
標準出力に英単語を出す(出すものは自由)
標準入力から1行受け取る
制限時間内に何問解けたか表示する
13 changes: 13 additions & 0 deletions kadai3-1/yoheimiyamoto/main.go
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)

}
5 changes: 5 additions & 0 deletions kadai3-1/yoheimiyamoto/pingpong/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
Package pingpong ...

*/
package pingpong
65 changes: 65 additions & 0 deletions kadai3-1/yoheimiyamoto/pingpong/pingpong.go
Original file line number Diff line number Diff line change
@@ -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
}
}()
}
1 change: 1 addition & 0 deletions kadai3-1/yoheimiyamoto/pingpong/pingpong_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pingpong
38 changes: 38 additions & 0 deletions kadai3-1/yoheimiyamoto/pingpong/score.go
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)
}