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

kadai-3-1-sh-tatsuno #32

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/gopherdojo/dojo5
1 change: 1 addition & 0 deletions kadai3-1/sh-tatsuno/go-typing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
15 changes: 15 additions & 0 deletions kadai3-1/sh-tatsuno/go-typing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Typing Game

Gopher道場 #5 課題3-1 `タイピングゲームを作ろう` の実装です。

```
$ go run main.go -t 60 -f animals
```
などとすることでタイピングゲームを行うことができます。

なかなかgo routineに慣れず、みなさんのやり方も参考にしながら実装してみました。

## 困っている点

- goroutineを使う際のテストがよく分かりませんでした
- errorのチェックをする場合に、xerrors.Newで作ったものは内部的にframeという値を持ってしまうため、厳密にDeepReflectできず、どのように比較をしたらよろしいでしょうか
3 changes: 3 additions & 0 deletions kadai3-1/sh-tatsuno/go-typing/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gopherdojo/dojo5/kadai3-1/sh-tatsuno/go-typing

require golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
2 changes: 2 additions & 0 deletions kadai3-1/sh-tatsuno/go-typing/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
43 changes: 43 additions & 0 deletions kadai3-1/sh-tatsuno/go-typing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"flag"
"fmt"
"os"
"time"

"github.com/gopherdojo/dojo5/kadai3-1/sh-tatsuno/go-typing/typing_game"
"github.com/gopherdojo/dojo5/kadai3-1/sh-tatsuno/go-typing/words"
)

const (
ExitCodeOK = 0
ExitCodeError = 1
)

func main() {

os.Exit(run(os.Args[1:]))
}

func run(args []string) int {
f := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
timeLimit := f.Int("t", 60, "Time limit of the game (sec): default 60sec")
wordPath := f.String("f", "animals", "type of words. details in words directory: default animals")
f.Parse(args)

filePath := "words/wordlist/" + *wordPath + ".txt"
gameWords, err := words.Import(filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "words import error. err: %v", err)
return ExitCodeError
}

g := typing_game.NewTypingGame(gameWords, time.Duration(*timeLimit)*time.Second)
if err := g.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Execute error. err: %v", err)
return ExitCodeError
}

return ExitCodeOK
}
84 changes: 84 additions & 0 deletions kadai3-1/sh-tatsuno/go-typing/typing_game/typing_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package typing_game

import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)

type TypingGameInterface interface {
Execute() error
}

type TypingGame struct {
Words []string
TimeLimit time.Duration
}

// result
type resultList struct {
total int
corrects int
}

func NewTypingGame(ws []string, t time.Duration) TypingGame {
tg := TypingGame{
Words: ws,
TimeLimit: t,
}
return tg
}

func (t *TypingGame) shuffle() {
rand.Seed(time.Now().UnixNano())
for i := len(t.Words) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
t.Words[i], t.Words[j] = t.Words[j], t.Words[i]
}
}

func (t TypingGame) Execute() error {

r := &resultList{}
t.shuffle()

keyin := make(chan string)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
keyin <- s.Text()
}
}()

fmt.Print("TYPING start\n")
time.Sleep(500)
timeup := time.After(t.TimeLimit)

LOOP:
for _, word := range t.Words {

fmt.Printf("-> %s\n", word)

select {
case input := <-keyin:
r.total++
if input == word {
fmt.Print("Correct!\n\n")
r.corrects++
} else {
fmt.Print("Missed!\n\n")
}
case <-timeup:
fmt.Print("Time UP!\n\n")
break LOOP
}
}

fmt.Print("Typing Result\n\n")
fmt.Printf("- SCORE: %d points (total %d words)\n", r.corrects, r.total)
fmt.Printf("- Second : %d\n", t.TimeLimit/time.Second)

return nil
}
Loading