-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6841380
Showing
11 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |