From a86bb4bec9f1c1ce5832af90a5c273e0737f866a Mon Sep 17 00:00:00 2001 From: abemotion Date: Mon, 10 Sep 2018 02:42:18 +0900 Subject: [PATCH] kadai3-1-abemotion --- kadai3-1/abemotion/main.go | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 kadai3-1/abemotion/main.go diff --git a/kadai3-1/abemotion/main.go b/kadai3-1/abemotion/main.go new file mode 100644 index 0000000..a16c0a0 --- /dev/null +++ b/kadai3-1/abemotion/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "math/rand" + "os" + "time" +) + +func main() { + ch := input(os.Stdin) + + questions := []string{"facebook", "google", "apple", "uber", "yahoo"} + rand.Seed(time.Now().UnixNano()) + + score := 0 + + t := time.After(time.Duration(10) * time.Second) + + for { + s := questions[rand.Intn(len(questions))] + fmt.Printf(">%s\n", s) + select { + case v := <-ch: + if s == v { + fmt.Println("Correct!") + score++ + } else { + fmt.Println("Wrong!") + } + case <-t: + fmt.Println("Time up!") + fmt.Println("Your score:", score) + return + } + } +} + +func input(r io.Reader) <-chan string { + ch := make(chan string) + go func() { + std := bufio.NewScanner(r) + for std.Scan() { + ch <- std.Text() + } + close(ch) + }() + return ch +}