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