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

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
14 changes: 14 additions & 0 deletions kadai3/ramenjuniti/typinggame/Makefile
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions kadai3/ramenjuniti/typinggame/README.md
Original file line number Diff line number Diff line change
@@ -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
```
62 changes: 62 additions & 0 deletions kadai3/ramenjuniti/typinggame/main.go
Original file line number Diff line number Diff line change
@@ -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
}
83 changes: 83 additions & 0 deletions kadai3/ramenjuniti/typinggame/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
12 changes: 12 additions & 0 deletions kadai3/ramenjuniti/typinggame/words.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

var words = []string{
"ra-men",
"jirou",
"yasai",
"ninnniku",
"abura",
"karame",
"mashi",
"mashimashi",
}