Skip to content

Commit 1da3995

Browse files
author
Shota Sawada
committed
課題3-1:タイピングゲームの実装
1 parent 1363207 commit 1da3995

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

Diff for: kadai3-1/sawadashota/.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Go template
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
vendor/

Diff for: kadai3-1/sawadashota/Gopkg.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: kadai3-1/sawadashota/Gopkg.toml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
branch = "master"
30+
name = "github.com/icrowley/fake"
31+
32+
[prune]
33+
go-tests = true
34+
unused-packages = true

Diff for: kadai3-1/sawadashota/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
タイピングゲーム
2+
===
3+
4+
表示された英単語をタイピングするゲームです。
5+
6+
ゲームの始め方
7+
---
8+
9+
Easy Mode(5文字以内)
10+
11+
```shell
12+
$ go run main.go
13+
```
14+
15+
Hard Mode(6文字以上)
16+
17+
```shell
18+
$ go run main.go --hard
19+
```

Diff for: kadai3-1/sawadashota/faker/faker.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package faker
2+
3+
import "github.com/icrowley/fake"
4+
5+
// ShortWordLength is maximum chars to type easy
6+
const ShortWordLength = 5
7+
8+
type Easy struct{}
9+
10+
// Word returns shorter than 5 chars
11+
func (q *Easy) Word() string {
12+
for {
13+
if w := fake.Word(); len([]rune(w)) <= ShortWordLength {
14+
return w
15+
}
16+
}
17+
}
18+
19+
type Hard struct{}
20+
21+
// Word returns shorter than 5 chars
22+
func (q *Hard) Word() string {
23+
for {
24+
if w := fake.Word(); len([]rune(w)) > ShortWordLength {
25+
return w
26+
}
27+
}
28+
}

Diff for: kadai3-1/sawadashota/main.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"time"
10+
11+
"github.com/gopherdojo/dojo2/kadai3-1/sawadashota/faker"
12+
)
13+
14+
// AmountTime is amount of game rounds
15+
const AmountTime = 5
16+
17+
// Timeout is how long seconds waiting for typing
18+
const Timeout = 5
19+
20+
// isHard game mode
21+
var isHard bool
22+
23+
type Faker interface {
24+
Word() string
25+
}
26+
27+
func init() {
28+
flag.BoolVar(&isHard, "hard", false, "Hard mode")
29+
flag.Parse()
30+
}
31+
32+
func main() {
33+
var f Faker
34+
var word string
35+
36+
if isHard {
37+
fmt.Println("Start Hard Mode!")
38+
f = new(faker.Hard)
39+
} else {
40+
fmt.Println("Start Easy Mode!")
41+
f = new(faker.Easy)
42+
}
43+
44+
correctCount := 0
45+
46+
ch := input(os.Stdin)
47+
48+
fmt.Println("This is typing game!")
49+
fmt.Printf("Plase type appearing word in %d seconds\n", Timeout)
50+
51+
for i := 0; i < AmountTime; i++ {
52+
word = f.Word()
53+
54+
fmt.Printf("\nWord: %s\n", word)
55+
fmt.Print("----> ")
56+
57+
select {
58+
case <-time.After(Timeout * time.Second):
59+
fmt.Println("Time Over!")
60+
case answer := <-ch:
61+
if word == answer {
62+
fmt.Println("Great!")
63+
correctCount++
64+
} else {
65+
fmt.Println("Uh-Oh")
66+
}
67+
}
68+
}
69+
70+
if correctCount == AmountTime {
71+
fmt.Print("\nPerfect!\n")
72+
return
73+
}
74+
75+
fmt.Printf("Correct: %d, Incorrect: %d\n", correctCount, AmountTime-correctCount)
76+
}
77+
78+
func input(r io.Reader) <-chan string {
79+
ch := make(chan string)
80+
go func() {
81+
s := bufio.NewScanner(r)
82+
for s.Scan() {
83+
ch <- s.Text()
84+
}
85+
close(ch)
86+
}()
87+
return ch
88+
}

0 commit comments

Comments
 (0)