-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (154 loc) · 3.74 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
160
161
162
163
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
// Quiz is a abstract struct for storing
// the quiz information.
type Quiz struct {
Timer *time.Timer
CustomInputTime time.Duration
QuestionAndAnswer map[string]string
TotalQuestions int
QuestionsAnswered int
Marks int
Right int
Wrong int
}
// newQuiz returns a new Quiz instance
// with initialized map parameter.
func newQuiz() *Quiz {
return &Quiz{
QuestionAndAnswer: make(map[string]string, 100),
Timer: time.NewTimer(5 * time.Minute),
}
}
func main() {
q := newQuiz()
file := flag.String("file", "problems.csv", "Give the path of input csv file")
t := flag.Int("time", 30, "Give custom game duration time(In Seconds)")
flag.Parse()
s := fmt.Sprintf("%ds", *t)
duration, err := time.ParseDuration(s)
if err != nil {
fmt.Println(err)
}
q.CustomInputTime = duration
if q.readFromCSV(*file) != nil {
log.Fatal(err)
}
q.userInterface()
}
// userInterface is the command line
// interface for the quiz game application.
func (q *Quiz) userInterface() {
var input int
for {
if !q.Timer.Reset(5 * time.Minute) {
fmt.Println("Timer reset failed")
}
fmt.Printf("\n\t WELCOME TO ONLINE QUIZ \n\n")
fmt.Printf("********************************************\n")
fmt.Printf("\n\t\t1.Start quiz\n")
fmt.Printf("\t\t2.Exit\n\n")
fmt.Printf("\t\tEnter your choice:\n\n")
fmt.Scan(&input)
switch input {
case 1:
q.quizGame()
default:
os.Exit(0)
}
}
}
// readFromCSV read data from a csv file based on our input.
func (q *Quiz) readFromCSV(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
r := csv.NewReader(bufio.NewReader(file))
records, err := r.ReadAll()
if err != nil {
return err
}
for i, k := range records {
for j, l := range k {
if j == 0 {
q.QuestionAndAnswer[l] = records[i][j+1]
}
}
}
q.TotalQuestions = len(q.QuestionAndAnswer)
return nil
}
// quizGame game is a function where the
// actual game logic is taken place.
func (q *Quiz) quizGame() {
var input, flag string
q.Marks = 0
q.Right = 0
q.Wrong = 0
q.QuestionsAnswered = 0
e:
fmt.Println("Shall we start the game? (y/n)")
fmt.Scan(&flag)
flag = strings.TrimSpace(strings.ToLower(flag))
if flag != "y" && flag != "n" {
fmt.Println("Invalid entry")
goto e
} else if flag == "n" {
return
}
if q.Timer.Reset(q.CustomInputTime) {
fmt.Printf("\nYour time start now...You have total of %d Seconds", q.CustomInputTime/1000000000)
}
// Creating a go routine for checking the time
go func() {
select {
case <-q.Timer.C:
q.Timer.Stop()
fmt.Println("\nSorry time out, Please try again later!!!")
fmt.Printf("\nTotal questions: %d , You Answered: %d", q.TotalQuestions, q.QuestionsAnswered)
fmt.Printf("\nRight answers: %d, Wrong answers: %d", q.Right, q.Wrong)
fmt.Println("\n\tYour total points: ", q.Marks)
os.Exit(0)
}
}()
for question, answer := range q.QuestionAndAnswer {
k:
fmt.Printf("\nwhat is the result of %s : ", question)
fmt.Scan(&input)
input = strings.TrimSpace(input)
inp, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Invalid input!")
goto k
}
ans, err := strconv.Atoi(answer)
if err != nil {
log.Fatal(err)
}
if inp == ans {
fmt.Println("Right answer!")
q.Right++
q.Marks++
q.QuestionsAnswered++
continue
}
q.Wrong++
q.QuestionsAnswered++
fmt.Println("Wrong answer!")
fmt.Printf("Right answer is %s \n", answer)
}
fmt.Printf("\nTotal questions: %d, You Answered: %d", q.TotalQuestions, q.QuestionsAnswered)
fmt.Printf("\nRight answers: %d, Wrong answers: %d", q.Right, q.Wrong)
fmt.Println("\n\tYour total points: ", q.Marks)
}