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-dobuzora #30

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions kadai3/dobuzora/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_bin
16 changes: 16 additions & 0 deletions kadai3/dobuzora/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
題課3-1
===

タイピングゲームを作ろう
- 標準出力に英単語を出す
- 標準入力から1文字受け取る
- 制限時間内に何問解けたかを表示する

## Install
```
export GOBIN=`pwd`/_bin
go install github.com/gopherdojo/dojo6/kadai3/dobuzora/cmd/type-game
_bin/cmd
```


20 changes: 20 additions & 0 deletions kadai3/dobuzora/cmd/type-game/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"os"

"github.com/gopherdojo/dojo6/kadai3/dobuzora/internal/game"
)

const timeLimit = 10

var (
reader = os.Stdin
writer = os.Stdout
questions = []string{"public", "void", "func", "return", "continue", "for", "go", "float"}
)

func main() {
g := game.New(reader, writer, timeLimit, questions)
g.Play()
}
3 changes: 3 additions & 0 deletions kadai3/dobuzora/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gopherdojo/dojo6/kadai3/dobuzora

go 1.12
106 changes: 106 additions & 0 deletions kadai3/dobuzora/internal/game/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Package game implements typing game for terminal.
*/
package game

import (
"bufio"
"context"
"fmt"
"io"
"math/rand"
"time"
)

type Game struct {
r io.Reader
w io.Writer
timeLimit time.Duration
questions []string
answerNum int
correctAnswerNum int
}

// New for game package
func New(reader io.Reader, writer io.Writer, timeLimit time.Duration, questions []string) *Game {
return &Game{
r: reader,
w: writer,
timeLimit: timeLimit,
questions: questions,
}
}

// Play is to start type game.
func (gm *Game) Play() {
gm.displayRule()
gm.shuffleQuestion()
gm.playGame()
}

// displayRule is to display rules of type game.
func (gm *Game) displayRule() {
fmt.Fprintf(gm.w, "画面に表示される英単語を入力しましょう!")
fmt.Fprintf(gm.w, "制限時間は%dです\n", gm.timeLimit)
}

// playGame is type-game core logic.
func (gm *Game) playGame() {
bc := context.Background()
ctx, cancel := context.WithTimeout(bc, gm.timeLimit*time.Second)
defer cancel()

ch := gm.input()

gameLoop:
for i := 0; i < len(gm.questions); i++ {
question := gm.questions[i]
fmt.Fprintf(gm.w, "%d問目: %s\n", i+1, question)
fmt.Fprint(gm.w, ">")

var in string
var ok bool
select {
case in, ok = <-ch:
if !ok {
break gameLoop
}
gm.answerNum++
case <-ctx.Done():
break gameLoop
}

if in == question {
gm.correctAnswerNum++
fmt.Fprint(gm.w, "正解!")
} else {
fmt.Fprint(gm.w, "不正解...")
}
fmt.Fprintf(gm.w, " 現在の正答率:%d/%d\n", gm.correctAnswerNum, gm.answerNum)
}
}

// displayResult is to display the result of type game.
func (gm *Game) displayResult() {
}

// shuffleQuestion is to shuffle given questions of typing game.
func (gm *Game) shuffleQuestion() {
n := len(gm.questions)
for i := n - 1; i >= 0; i-- {
j := rand.Intn(i + 1)
gm.questions[i], gm.questions[j] = gm.questions[j], gm.questions[i]
}
}

func (gm *Game) input() <-chan string {
ch := make(chan string)
go func() {
sc := bufio.NewScanner(gm.r)
for sc.Scan() {
ch <- sc.Text()
}
close(ch)
}()
return ch
}