|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/csv" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "math/rand" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type problem struct { |
| 17 | + question string |
| 18 | + answer string |
| 19 | +} |
| 20 | + |
| 21 | +var ( |
| 22 | + problemsFile = flag.String("problems", "problems.csv", "A CSV file containing problems and their solutions") |
| 23 | + quizTime = flag.Int("time", 30, "The time in seconds this quiz will run") |
| 24 | + shuffle = flag.Bool("shuffle", false, "Wheteher or not to shuffle the problems") |
| 25 | + osR = bufio.NewReader(os.Stdin) |
| 26 | +) |
| 27 | + |
| 28 | +func readProblems(csvFile *os.File) []problem { |
| 29 | + csvR := csv.NewReader(csvFile) |
| 30 | + |
| 31 | + problems := make([]problem, 0) |
| 32 | + for { |
| 33 | + record, err := csvR.Read() |
| 34 | + if err == io.EOF { |
| 35 | + break |
| 36 | + } |
| 37 | + if err != nil { |
| 38 | + log.Fatal(err) |
| 39 | + } |
| 40 | + if record[0] != "" && record[1] != "" { |
| 41 | + problems = append(problems, problem{record[0], strings.ToLower(record[1]}) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return problems |
| 46 | +} |
| 47 | + |
| 48 | +func shuffleProblems(problems []problem) { |
| 49 | + r := rand.New(rand.NewSource(time.Now().Unix())) |
| 50 | + for i1 := 0; i1 < len(problems); i1++ { |
| 51 | + i2 := r.Intn(len(problems)) |
| 52 | + |
| 53 | + problem1 := problems[i1] |
| 54 | + problem2 := problems[i2] |
| 55 | + |
| 56 | + problems[i1] = problem2 |
| 57 | + problems[i2] = problem1 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func askQuestion(q string, tries int8) string { |
| 62 | + fmt.Printf("\n%s: ", q) |
| 63 | + input, err := osR.ReadString('\n') |
| 64 | + if err != nil { |
| 65 | + if tries < 5 { |
| 66 | + log.Println("Your answer could not be processed, please try again") |
| 67 | + return askQuestion(q, tries+1) |
| 68 | + } |
| 69 | + log.Fatal("Something is wrong with this program, going to exit...") |
| 70 | + } |
| 71 | + return strings.ToLower(strings.TrimSpace(strings.TrimRight(input, "\n"))) |
| 72 | +} |
| 73 | + |
| 74 | +func askQuestions(problems []problem, timer, correctAnswersChan, done chan interface{}) { |
| 75 | + index := 0 |
| 76 | + for { |
| 77 | + select { |
| 78 | + case <-timer: |
| 79 | + fmt.Println("\nTime's up!") |
| 80 | + return |
| 81 | + |
| 82 | + default: |
| 83 | + if index >= len(problems) { |
| 84 | + close(done) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + problem := problems[index] |
| 89 | + answer := askQuestion(problem.question, 0) |
| 90 | + if answer == problem.answer { |
| 91 | + correctAnswersChan <- true |
| 92 | + fmt.Println("Correct!") |
| 93 | + } else { |
| 94 | + fmt.Println("False...") |
| 95 | + } |
| 96 | + index = index + 1 |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func main() { |
| 102 | + flag.Parse() |
| 103 | + |
| 104 | + if !strings.HasSuffix(*problemsFile, "csv") { |
| 105 | + log.Fatalf("Provided problems file '%s' is not a CSV file", *problemsFile) |
| 106 | + } |
| 107 | + |
| 108 | + csvFile, err := os.Open(*problemsFile) |
| 109 | + if err != nil { |
| 110 | + log.Fatalf("Could not open '%s'", *problemsFile) |
| 111 | + } |
| 112 | + |
| 113 | + problems := readProblems(csvFile) |
| 114 | + totalProblems := len(problems) |
| 115 | + if *shuffle { |
| 116 | + shuffleProblems(problems) |
| 117 | + } |
| 118 | + |
| 119 | + timer := make(chan interface{}) |
| 120 | + correctAnswersChan := make(chan interface{}) |
| 121 | + done := make(chan interface{}) |
| 122 | + |
| 123 | + fmt.Println("Press ENTER to start the quiz...") |
| 124 | + osR.ReadString('\n') |
| 125 | + |
| 126 | + correctAnswers := 0 |
| 127 | + go func() { |
| 128 | + for { |
| 129 | + select { |
| 130 | + case _ = <-correctAnswersChan: |
| 131 | + correctAnswers = correctAnswers + 1 |
| 132 | + case <-done: |
| 133 | + return |
| 134 | + } |
| 135 | + } |
| 136 | + }() |
| 137 | + |
| 138 | + go askQuestions(problems, timer, correctAnswersChan, done) |
| 139 | + |
| 140 | + time.Sleep(time.Duration(*quizTime) * time.Second) |
| 141 | + close(timer) |
| 142 | + close(done) |
| 143 | + |
| 144 | + if totalProblems == correctAnswers { |
| 145 | + fmt.Println("\nCongratulations! You answered all questions correctly!") |
| 146 | + } else { |
| 147 | + fmt.Printf( |
| 148 | + "\nYou answered %d questions correctly but failed to do so for %d questions, try again", |
| 149 | + correctAnswers, |
| 150 | + totalProblems-correctAnswers, |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + os.Exit(0) |
| 155 | +} |
0 commit comments