-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (142 loc) · 3.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"math"
"os"
"strings"
"time"
)
func check(e error) {
if e != nil {
fmt.Println("FILE BROKE")
panic(e)
}
}
func importCSV(path string) map[string]string {
fmt.Println("chill...Opening Quiz")
time.Sleep(time.Second)
file, err := os.Open(path)
check(err)
defer file.Close()
quiz := make(map[string]string)
reader := csv.NewReader(file)
records, recordsErr := reader.ReadAll()
// Checks for the error
if recordsErr != nil {
fmt.Println("Error reading records")
}
// Loop to iterate through
// and print each of the string slice
for _, line := range records {
q := line[0]
a := line[1]
quiz[q] = a
}
return quiz
}
func ask(reader bufio.Reader, q string, a string) bool {
fmt.Print(q + "\n")
res, _ := reader.ReadString('\n')
res = strings.TrimRight(res, "\n")
res = strings.TrimSpace(res)
res = strings.ToLower(res)
a = strings.ToLower(a)
if strings.Compare(a, res) == 0 {
return true
} else {
return false
}
}
func createQuiz() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Helping you create a quiz!")
fmt.Println("What should the quiz be called?")
title, _ := reader.ReadString('\n')
title = strings.TrimRight(title, "\n")
fmt.Println("Really " + title + " ? okay....")
fmt.Println("You will now begin entering Question and Answer pairs")
quiz := make(map[string]string)
do := true
for do {
fmt.Print("Q: ")
q, _ := reader.ReadString('\n')
q = strings.TrimRight(q, "\n")
fmt.Print("A: ")
a, _ := reader.ReadString('\n')
a = strings.TrimRight(a, "\n")
quiz[q] = a
fmt.Println("Another? y/n")
res, _ := reader.ReadString('\n')
res = strings.TrimRight(res, "\n")
if res == "y" {
} else {
do = false
}
}
fmt.Println(quiz)
filePath := "Quizes/" + title + ".csv"
file, err := os.Create(filePath)
check(err)
defer file.Close()
for key, value := range quiz {
q := key
a := value
_, err := file.WriteString(q + "," + a + "\n")
check(err)
}
os.Exit(0)
}
func timer(t int, right *float64, total *float64) {
dur := time.Duration(t) * time.Second
t1 := time.NewTimer(dur)
<-t1.C
fmt.Println("Timer expired")
endGame(right, total)
}
func endGame(right *float64, total *float64) {
perc := math.Floor((*right / *total) * 100)
fmt.Printf("You got a %d/%d \n", int(*right), int(*total))
fmt.Printf("%d%%\n", int(perc))
os.Exit(0)
}
func startGame() bool {
reader := bufio.NewReader(os.Stdin)
if ask(*reader, "Ready? y/n", "y") {
return true
} else {
os.Exit(0)
return false
}
}
func game(fileFlag *string, timerFlag *int) {
right := float64(0)
quiz := importCSV(*fileFlag)
maxPossible := float64(len(quiz))
reader := bufio.NewReader(os.Stdin)
startGame()
go timer(*timerFlag, &right, &maxPossible)
for q, a := range quiz {
if ask(*reader, q, a) {
right++
} else {
}
}
endGame(&right, &maxPossible)
}
func main() {
fileFlag := flag.String("f", "./quiz.txt", "a filepath")
timerFlag := flag.Int("t", 30, "timer seconds")
flag.Parse()
fmt.Println("(1) Play Quiz (2) Create Quiz")
reader := bufio.NewReader(os.Stdin)
res, _ := reader.ReadString('\n')
res = strings.TrimRight(res, "\n")
if res == "1" {
game(fileFlag, timerFlag)
} else {
createQuiz()
}
}