added bufio and strings library to help me handle multi word answers to each question
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio" // used to read multi-word user input
|
|
"encoding/csv" //for handling the csv of questions
|
|
"flag" //this go package will allow me to add flags to the command
|
|
"fmt" // fmt is a package that contains basic functionality like printing to the terminal and taking in user input
|
|
"os" // os provides us a platform independent way to handle errors
|
|
"strings" // to trim spaces and handle case insensitivity
|
|
)
|
|
|
|
/*
|
|
CLI_Trivia.
|
|
|
|
This is a short Kingdom Hearts trivia game played on the command line.
|
|
*/
|
|
func main() {
|
|
|
|
/* Welcome message.
|
|
Println prints a welcome message to the terminal and appends a \n at the end of the line
|
|
*/
|
|
fmt.Println("¡Bienvenidos a Kingdom Hearts Trivia!")
|
|
|
|
/* What is your name?.
|
|
Print will prompt you to enter your name */
|
|
fmt.Print("¿Cual es tu nombre?: ")
|
|
var name string //this variable of string type holds your name
|
|
|
|
fmt.Scan(&name) //Scan will let you type your name in. "&" is an operator used to retrieve the memory address of name. the user's input will save to the "name" variable.
|
|
fmt.Printf("¡WEPA, %v, por fin llegaste!\n", name) //Printf will print the formatted name variable and display a greeting to the user with their name
|
|
|
|
/* Age Check.
|
|
User will be notified that this trivia game is for people 10 years old and up, then prompted to disclose their age.*/
|
|
fmt.Print("¿Este juego esta clasificado para mayores de 10 años. Cuantos años tienes?: ")
|
|
var age uint //declaring age to be an unsigned integer because nobody can be a negative number of years old
|
|
|
|
fmt.Scan(&age) //Scan will take in the users input and assign it to age variable
|
|
|
|
/* This conditional checks if the inputted age is greater than or equal to 10. if so, it will notify the user that they
|
|
can play. if not, it will return and end the game. */
|
|
if age >= 10 {
|
|
fmt.Println("Puedes jugar 😊")
|
|
} else {
|
|
fmt.Println("No puedes jugar 😕 ¡Adios!")
|
|
return
|
|
}
|
|
|
|
csvFilename := flag.String("csv", "questions.csv", "A csv file in the format of 'question,answer'")
|
|
|
|
flag.Parse()
|
|
|
|
file, err := os.Open(*csvFilename)
|
|
if err != nil {
|
|
exit(fmt.Sprintf("Failed to open CSV file: %s\n", *csvFilename))
|
|
|
|
}
|
|
|
|
r := csv.NewReader(file)
|
|
lines, err := r.ReadAll()
|
|
if err != nil {
|
|
exit("Failed to parse the provided CSV file.")
|
|
}
|
|
problems := parseLines(lines)
|
|
// fmt.Println(problems)
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
score := 0 //here i am using := to type inference a score to 0
|
|
|
|
for i, p := range problems {
|
|
fmt.Printf("Problem %d: %s = \n", i+1, p.q)
|
|
|
|
userInput, _ := reader.ReadString('\n')
|
|
userInput = strings.TrimSpace(userInput)
|
|
userInput = strings.ToLower(userInput)
|
|
|
|
answer := strings.ToLower(strings.TrimSpace(p.a))
|
|
|
|
if userInput == answer {
|
|
score++
|
|
}
|
|
}
|
|
|
|
fmt.Printf("You scored %v out of %v \n", score, len(problems))
|
|
|
|
percent := (float64(score) / float64(len(problems))) * 100
|
|
fmt.Printf("You scored: %v%%. \n", percent)
|
|
}
|
|
|
|
type problem struct {
|
|
q string
|
|
a string
|
|
}
|
|
|
|
func parseLines(lines [][]string) []problem {
|
|
|
|
ret := make([]problem, len(lines))
|
|
for i, line := range lines {
|
|
ret[i] = problem{
|
|
q: line[0],
|
|
a: line[1],
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func exit(msg string) {
|
|
fmt.Println(msg)
|
|
os.Exit(1)
|
|
}
|