Skip to content
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",
}