Skip to content

gopher道場 #3-1 #42

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 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
7 changes: 7 additions & 0 deletions kadai3-1/po3rin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Typing Game for Engineer

## Start

```bash
$ go run main.go
```
65 changes: 65 additions & 0 deletions kadai3-1/po3rin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"bufio"
"context"
"dojo2/kadai3-1/po3rin/question"
"fmt"
"io"
"math/rand"
"os"
"time"
)

var questions = question.List
var num int

const sec = 30000

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
}

func makeQuetion() string {
rand.Seed(time.Now().UnixNano())
q := questions[rand.Intn(len(questions))]
return q
}

func main() {
fmt.Printf("typing game start ! %v millsecound !", sec)
ch := input(os.Stdin)
q := makeQuetion()
fmt.Println("==============")
fmt.Println(q)
fmt.Print(">")

bc := context.Background()
tc := sec * time.Millisecond
ctx, cancel := context.WithTimeout(bc, tc)
defer cancel()

for {
select {
case answer := <-ch:
q := makeQuetion()
fmt.Println("==============")
fmt.Println(q)
fmt.Print(">")
if answer != q {
num++
}
case <-ctx.Done():
fmt.Printf("end! number of correct is %v\n", num)
os.Exit(1)
}
}
}
26 changes: 26 additions & 0 deletions kadai3-1/po3rin/question/question.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package question

// List is question list
var List = []string{
"ruby",
"python",
"node",
"go",
"php",
"nim",
"docker",
"rustc",
"docker-compose",
"scala",
"rails",
"git",
"npm",
"vim",
"brew",
"yarn",
"pip",
"dep",
"aws",
"gcloud",
"kubectl",
}