diff --git a/kadai3-1/po3rin/README.md b/kadai3-1/po3rin/README.md new file mode 100644 index 0000000..5303161 --- /dev/null +++ b/kadai3-1/po3rin/README.md @@ -0,0 +1,7 @@ +# Typing Game for Engineer + +## Start + +```bash +$ go run main.go +``` \ No newline at end of file diff --git a/kadai3-1/po3rin/main.go b/kadai3-1/po3rin/main.go new file mode 100644 index 0000000..6f84327 --- /dev/null +++ b/kadai3-1/po3rin/main.go @@ -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) + } + } +} diff --git a/kadai3-1/po3rin/question/question.go b/kadai3-1/po3rin/question/question.go new file mode 100644 index 0000000..6b6d8c2 --- /dev/null +++ b/kadai3-1/po3rin/question/question.go @@ -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", +}