Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemolcay committed Jul 15, 2019
0 parents commit 6841380
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This is a simple CLI quiz game written in Go.

![demo](demo.gif)

Questions, correct answers and the points awarded for the correct answers are provided via a CSV file formatted like this:

<pre>
5*3,15,1
44+54,98,2
42/7,6,2
</pre>
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/erdemolcay/quiz

go 1.12

require (
github.com/gookit/color v1.1.7 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/color v1.1.7 h1:WR5I/mhSHzemW2DzG54hTsUb7OzaREvkcmUG4/WST4Q=
github.com/gookit/color v1.1.7/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"github.com/erdemolcay/quiz/pkg/qns"
"log"
"os"

"github.com/erdemolcay/quiz/pkg/csv"
)

func main() {
if len(os.Args) < 2 {
log.Fatalln("Please specify CSV file path as the first argument!")
}

filePath := os.Args[1]

if _, err := os.Stat(filePath); os.IsNotExist(err) {
log.Fatalf("File: %s does not exist!\n", filePath)
}

questions := csv.Read(filePath)

qns.Intro(questions)

qns.Score(qns.Ask(questions))
}

48 changes: 48 additions & 0 deletions pkg/csv/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package csv

import (
"encoding/csv"
"io"
"log"
"os"
"strconv"
)

type Question struct {
QnText string
CorrectAnswer string
Points int
}

func Read(filePath string) []Question {
file, err := os.Open(filePath)

if err != nil {
log.Fatal(err)
}

reader := csv.NewReader(file)

questions := make([]Question, 0)

for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}

qnText := record[0]
correctAnswer := record[1]
points, err := strconv.Atoi(record[2])
if err != nil {
points = 0
}

questions = append(questions, Question{qnText, correctAnswer, points})
}

return questions
}
39 changes: 39 additions & 0 deletions pkg/qns/ask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package qns

import (
"fmt"
"github.com/erdemolcay/quiz/pkg/csv"
"log"
)

type Result struct {
Correct bool
Points int
UserAnswer string
CorrectAnswer string
}

func Ask(questions []csv.Question) ([]Result, int, int) {
results := make([]Result, 0)
correctAnswersNum := 0
totalScore := 0

for i, question := range questions {
userAnswer := ""
fmt.Printf("\n%d) %s? ", i+1, question.QnText)
_, err := fmt.Scan(&userAnswer)
if err != nil {
log.Fatal(err.Error())
}

if userAnswer == question.CorrectAnswer {
results = append(results, Result{true, question.Points, userAnswer, question.CorrectAnswer})
correctAnswersNum++
totalScore += question.Points
} else {
results = append(results, Result{false, 0, userAnswer, question.CorrectAnswer})
}
}

return results, correctAnswersNum, totalScore
}
15 changes: 15 additions & 0 deletions pkg/qns/intro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package qns

import (
"fmt"
"github.com/erdemolcay/quiz/pkg/csv"
"log"
)

func Intro(questions []csv.Question) {
fmt.Printf("You will be asked %d questions in this test.\n\nPress [Enter] when you are ready.", len(questions))
_, err := fmt.Scanln()
if err != nil {
log.Fatal(err.Error())
}
}
9 changes: 9 additions & 0 deletions pkg/qns/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package qns

import "fmt"

func Score(results []Result, correctAnswersNum, totalScore int) {
fmt.Printf("\nYou answered %d questions correctly. You have earned %d points.\n", correctAnswersNum, totalScore)

Table(results)
}
24 changes: 24 additions & 0 deletions pkg/qns/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package qns

import (
"fmt"
"github.com/olekukonko/tablewriter"
"os"
"strconv"
)

func Table(results []Result) {
fmt.Println("\nYou can see your detailed results below:")

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"#", "Your answer", "Correct answer", "Result", "Points earned"})
for i, result := range results {
if result.Correct {
table.Append([]string{strconv.Itoa(i+1), result.UserAnswer, result.CorrectAnswer, "Right", strconv.Itoa(result.Points)})
} else {
table.Append([]string{strconv.Itoa(i+1), result.UserAnswer, result.CorrectAnswer, "Wrong", strconv.Itoa(result.Points)})
}
}

table.Render()
}

0 comments on commit 6841380

Please sign in to comment.