From 6f98552c9bd577b152f40d2e3f9cfd3bf352f22f Mon Sep 17 00:00:00 2001 From: kaznishi Date: Wed, 4 Jul 2018 19:04:49 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=94=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=82=B2=E3=83=BC=E3=83=A0=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai3-1/main.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 kadai3-1/main.go diff --git a/kadai3-1/main.go b/kadai3-1/main.go new file mode 100644 index 0000000..746eecb --- /dev/null +++ b/kadai3-1/main.go @@ -0,0 +1,85 @@ +package main + +import ( + "fmt" + "os" + "io" + "bufio" + "time" + "math/rand" + "strconv" +) + +var wordList = []string{ + "apple", + "brother", + "cross", + "dinner", + "evening", + "final", + "great", + "happy", + "idea", + "judge", + "knight", + "least", + "mountain", + "new", + "open", + "pen", + "question", + "response", + "star", + "teacher", + "unique", + "victory", + "winner", + "xbox", + "yahoo", + "zero", +} + +func main() { + chTyping := input(os.Stdin) + chTimer := time.After(30 * time.Second) + var correct int + + for { + q := question() + fmt.Println("Let's input : " + q) + fmt.Print(">") + select { + case in := <-chTyping: + if q == in { + correct += 1 + fmt.Println("Yeah!! (count: " + strconv.Itoa(correct) + ")") + } else { + fmt.Println("Wrong. (count: " + strconv.Itoa(correct) + ")") + } + case <-chTimer: + fmt.Println("") + fmt.Println("Time Up!!!") + fmt.Println("Correct Count is ... " + strconv.Itoa(correct)) + return + } + } + +} + +func question() string { + rand.Seed(time.Now().UnixNano()) + return wordList[rand.Int() % len(wordList)] +} + +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 +} +