Skip to content

Instantly share code, notes, and snippets.

@h-okay
Created April 10, 2024 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-okay/0419d4b87cae498031cf563ba6fa5705 to your computer and use it in GitHub Desktop.
Save h-okay/0419d4b87cae498031cf563ba6fa5705 to your computer and use it in GitHub Desktop.
func Play(quiz Quiz) {
fmt.Println("You have", quiz.timer.secondsPerQuestion, "seconds per question.")
input := make(chan string, len(quiz.questions))
for _, question := range quiz.questions {
timer := time.NewTimer(time.Duration(quiz.timer.secondsPerQuestion) * time.Second)
fmt.Printf("Problem #%d: %s = ", question.id, question.question)
go getInput(input)
select {
case <-timer.C:
fmt.Println("Time limit exceed!")
case answer := <-input:
if answer == question.answer {
quiz.score++
}
}
}
fmt.Printf("You scored %d out of %d.\n", quiz.score, len(quiz.questions))
}
func getInput(input chan string) {
for {
in := bufio.NewReader(os.Stdin)
result, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}
input <- strings.TrimSpace(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment