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 by yusukemisawa #36

Open
wants to merge 1 commit 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
20 changes: 20 additions & 0 deletions kadai3/yusukemisa/typingGame/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## goTypingGame
タイピングゲームのGo実装

## Features
- [x] 標準出力に英単語を出す(出すものは自由)
- [x] 標準出力に定義した英単語を順番に表示する
- [x] 標準入力から1行受け取る
- [x] 入力を受け取った場合、表示に使用した単語と一致するか比較し、結果を出力する
- [x] 制限時間内に何問解けたか表示する
- [x] 30秒のタイマーをセットし、タイムアップした場合強制的にゲームの結果を表示して終了

## How to use

```
$ go get github.com/yusukemisa/goTypingGame

$ go install github.com/yusukemisa/goTypingGame

$ goTypingGame
```
75 changes: 75 additions & 0 deletions kadai3/yusukemisa/typingGame/game/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package game

import (
"bufio"
"fmt"
"io"
"os"
"time"
)

var correctNum int

//Game は正解数とゲーム終了の契機を保持します
type Game struct {
CorrectNum int
GameOverReason string
}

//StartWithTimer the typing game
func StartWithTimer(timer time.Duration) *Game {
//Game終了かタイムアップイベントを受け取る
done := make(chan string)

go setTimer(done, timer)
go playGame(done)

//Game終了かタイムアップするまでブロック
gameOver := <-done
close(done)

//Game結果
return &Game{
CorrectNum: correctNum,
GameOverReason: gameOver,
}
}

func setTimer(done chan<- string, timer time.Duration) {
time.Sleep(time.Second * timer)
done <- "TIME UP!!!"
}

//play the game
//done:ゲーム終了時このチャネルに通知を出す
func playGame(done chan<- string) {
ch := input(os.Stdin)
for i, word := range getQuestion() { //テストするときgetQuestionモックにしたい
//出題
fmt.Printf("Q%v %v\n>", i+1, word)
if answer, ok := <-ch; ok {
//答え合わせ
if answer == word {
fmt.Println("GREAT WORK!!!")
correctNum++
} else {
fmt.Println("おまえは何をやっているんだ!?")
}
} else {
break
}
}
done <- "YOU HAVE ANSERED ALL QUESTION!!!"
}

func input(r io.Reader) <-chan string {
recv := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
recv <- s.Text()
}
close(recv)
}()
return recv
}
17 changes: 17 additions & 0 deletions kadai3/yusukemisa/typingGame/game/question.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package game

//出題する単語集
var words = []string{
"golang",
"gopher",
"car",
"cat",
"dog",
}

//GetQuestion provide word for The Game
//TODO:ひとまず配列を順番に出すだけ
//出題ロジックを拡張できるように関数として作っておく
func getQuestion() []string {
return words
}
15 changes: 15 additions & 0 deletions kadai3/yusukemisa/typingGame/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"

"github.com/yusukemisa/goTypingGame/game"
)

func main() {
fmt.Println("★★★★★★★★ TYPING GAME START! ★★★★★★★★")
gameResult := game.StartWithTimer(30)
fmt.Printf("\n%v\n", gameResult.GameOverReason)
fmt.Printf("Your correct answer is %v\n", gameResult.CorrectNum)
fmt.Println("★★★★★★★★ TYPING GAME END! ★★★★★★★★")
}