Skip to content

kadai3 sawadashota #25

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

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
16 changes: 16 additions & 0 deletions kadai3-1/sawadashota/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

vendor/
21 changes: 21 additions & 0 deletions kadai3-1/sawadashota/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions kadai3-1/sawadashota/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
branch = "master"
name = "github.com/icrowley/fake"

[prune]
go-tests = true
unused-packages = true
19 changes: 19 additions & 0 deletions kadai3-1/sawadashota/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
タイピングゲーム
===

表示された英単語をタイピングするゲームです。

ゲームの始め方
---

Easy Mode(5文字以内)

```shell
$ go run main.go
```

Hard Mode(6文字以上)

```shell
$ go run main.go --hard
```
28 changes: 28 additions & 0 deletions kadai3-1/sawadashota/faker/faker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package faker

import "github.com/icrowley/fake"

// ShortWordLength is maximum chars to type easy
const ShortWordLength = 5

type Easy struct{}

// Word returns shorter than 5 chars
func (q *Easy) Word() string {
for {
if w := fake.Word(); len([]rune(w)) <= ShortWordLength {
return w
}
}
}

type Hard struct{}

// Word returns shorter than 5 chars
func (q *Hard) Word() string {
for {
if w := fake.Word(); len([]rune(w)) > ShortWordLength {
return w
}
}
}
88 changes: 88 additions & 0 deletions kadai3-1/sawadashota/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"time"

"github.com/gopherdojo/dojo2/kadai3-1/sawadashota/faker"
)

// AmountTime is amount of game rounds
const AmountTime = 5

// Timeout is how long seconds waiting for typing
const Timeout = 5

// isHard game mode
var isHard bool

type Faker interface {
Word() string
}

func init() {
flag.BoolVar(&isHard, "hard", false, "Hard mode")
flag.Parse()
}

func main() {
var f Faker
var word string

if isHard {
fmt.Println("Start Hard Mode!")
f = new(faker.Hard)
} else {
fmt.Println("Start Easy Mode!")
f = new(faker.Easy)
}

correctCount := 0

ch := input(os.Stdin)

fmt.Println("This is typing game!")
fmt.Printf("Plase type appearing word in %d seconds\n", Timeout)

for i := 0; i < AmountTime; i++ {
word = f.Word()

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

select {
case <-time.After(Timeout * time.Second):
fmt.Println("Time Over!")
case answer := <-ch:
if word == answer {
fmt.Println("Great!")
correctCount++
} else {
fmt.Println("Uh-Oh")
}
}
}

if correctCount == AmountTime {
fmt.Print("\nPerfect!\n")
return
}

fmt.Printf("Correct: %d, Incorrect: %d\n", correctCount, AmountTime-correctCount)
}

func input(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
ch <- s.Text()
}
close(ch)
}()
return ch
}
2 changes: 2 additions & 0 deletions kadai3-2/sawadashota/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
v1.0.11.tar.gz
vendor/
21 changes: 21 additions & 0 deletions kadai3-2/sawadashota/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions kadai3-2/sawadashota/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
branch = "master"
name = "golang.org/x/sync"

[prune]
go-tests = true
unused-packages = true
21 changes: 21 additions & 0 deletions kadai3-2/sawadashota/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
分割ダウンローダー
===

Download
---

```bash
$ go run main.go ${URL}
```

example

```bash
$ go run main.go https://github.com/sawadashota/gocmd/archive/v1.0.11.tar.gz
```

### Options

- `-p`: ダウンロードプロセス数(デフォルトはコア数)
- `-t`: タイムアウト(デフォルトは10秒)
- `-o`: 出力先(デフォルトはURLのファイル名)
85 changes: 85 additions & 0 deletions kadai3-2/sawadashota/download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package download

import (
"bytes"
"context"
"io"
"net/url"
"os"
"time"
)

type Downloader struct {
url *url.URL
Option
Data
}

type Data struct {
filesize uint
data [][]byte
}

type Option interface {
Proc() int
Timeout() time.Duration
Writer() io.Writer
Output() string
}

// New Downloader
func New(u *url.URL, opts Option) *Downloader {
return &Downloader{
url: u,
Option: opts,
Data: Data{},
}
}

// URL getter
func (d *Downloader) URL() *url.URL {
return d.url
}

// Run download
func (d *Downloader) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

filesize, err := d.FetchFileSize(d.url.String())
if err != nil {
return err
}

d.SetFileSize(filesize)
d.data = make([][]byte, d.Proc())

eg := d.get(ctx)
if err := eg.Wait(); err != nil {
return err
}

body := d.Merge()

f, err := os.Create(d.Output())
defer f.Close()

f.Write(body)

return nil
}

// SetFileSize is filesize setter
func (d *Data) SetFileSize(size uint) {
d.filesize = size
}

// Merge data slice
func (d *Data) Merge() []byte {
return bytes.Join(d.data, []byte(""))
}

// String return merged data
func (d *Data) String() string {
return string(d.Merge())
}
Loading