Skip to content
Open
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
51 changes: 51 additions & 0 deletions kadai3-1/abemotion/main.go
Original file line number Diff line number Diff line change
@@ -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
}