diff --git a/kadai3/ramenjuniti/typinggame/Makefile b/kadai3/ramenjuniti/typinggame/Makefile new file mode 100644 index 0000000..13ae418 --- /dev/null +++ b/kadai3/ramenjuniti/typinggame/Makefile @@ -0,0 +1,14 @@ +BIN := typinggame + +.PHONY: test +test: + go test -cover -v ./... + +.PHONY: build +build: test + go build -o $(BIN) + +.PHONY: clean +clean: + rm $(BIN) + go clean \ No newline at end of file diff --git a/kadai3/ramenjuniti/typinggame/README.md b/kadai3/ramenjuniti/typinggame/README.md new file mode 100644 index 0000000..9ed4547 --- /dev/null +++ b/kadai3/ramenjuniti/typinggame/README.md @@ -0,0 +1,32 @@ +# Typing Game + +## Description + +タイピングゲームです。 + +一行ずつ判定します。 + +`Score` は、どれぐらい出題単語を正しくタイプできているかどうかで加算されます。 + +例えば、`abcdef`と出題された時に`abcd`とタイプした場合、Score は 4 ポイント加算されます。 + +`type/sec`は、1 秒間に正しく入力したタイプ数です。 + +## Usage + +```bash +make build +./typinggame -t {制限時間} +``` + +## Build + +```bash +make build +``` + +## Test + +```bash +make test +``` diff --git a/kadai3/ramenjuniti/typinggame/main.go b/kadai3/ramenjuniti/typinggame/main.go new file mode 100644 index 0000000..c9185b3 --- /dev/null +++ b/kadai3/ramenjuniti/typinggame/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "math/rand" + "os" + "time" +) + +func main() { + t := flag.Int("t", 10, "制限時間(秒)") + flag.Parse() + + var score int + l := time.After(time.Duration(*t) * time.Second) + in := input(os.Stdin) + + rand.Seed(time.Now().UnixNano()) + + fmt.Println("Start!!") +L: + for { + q := words[rand.Intn(len(words))] + fmt.Println(q) + fmt.Print(">> ") + select { + case a := <-in: + score += judge(q, a) + case <-l: + fmt.Println("finish!!") + break L + } + } + fmt.Printf("Score: %v, type/sec: %2.1f\n", score, float64(score)/float64(*t)) +} + +func judge(q, a string) int { + var score int + for i := 0; i < len(q); i++ { + if i == len(a) { + break + } + if q[i] == a[i] { + score++ + } + } + return score +} + +func input(r io.Reader) <-chan string { + c := make(chan string) + go func() { + s := bufio.NewScanner(r) + for s.Scan() { + c <- s.Text() + } + }() + return c +} diff --git a/kadai3/ramenjuniti/typinggame/main_test.go b/kadai3/ramenjuniti/typinggame/main_test.go new file mode 100644 index 0000000..823fc2b --- /dev/null +++ b/kadai3/ramenjuniti/typinggame/main_test.go @@ -0,0 +1,83 @@ +package main + +import ( + "bytes" + "testing" +) + +func TestJudge(t *testing.T) { + tests := []struct { + name string + q string + a string + score int + }{ + { + name: "case1", + q: "abc", + a: "abc", + score: 3, + }, + { + name: "case2", + q: "abc", + a: "ab", + score: 2, + }, + { + name: "case3", + q: "ab", + a: "abc", + score: 2, + }, + { + name: "case4", + q: "", + a: "abc", + score: 0, + }, + { + name: "case5", + q: "abc", + a: "", + score: 0, + }, + { + name: "case6", + q: "", + a: "", + score: 0, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := judge(test.q, test.a); got != test.score { + t.Errorf("got %v, want %v", got, test.score) + } + }) + } +} + +func TestInput(t *testing.T) { + tests := []struct { + name string + in string + out string + }{ + { + name: "case1", + in: "abc", + out: "abc", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + r := bytes.NewBuffer([]byte(test.in)) + if got := <-input(r); got != test.out { + t.Errorf("got %v, want %v", got, test.out) + } + }) + } +} diff --git a/kadai3/ramenjuniti/typinggame/words.go b/kadai3/ramenjuniti/typinggame/words.go new file mode 100644 index 0000000..703b485 --- /dev/null +++ b/kadai3/ramenjuniti/typinggame/words.go @@ -0,0 +1,12 @@ +package main + +var words = []string{ + "ra-men", + "jirou", + "yasai", + "ninnniku", + "abura", + "karame", + "mashi", + "mashimashi", +}