2025-02-23 15:32:03 +00:00
package main
import (
2025-02-23 16:21:26 +00:00
"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
2025-02-23 15:32:03 +00:00
)
/ *
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?: " )
2025-02-23 16:21:26 +00:00
var name string //this variable of string type holds your name
2025-02-23 15:32:03 +00:00
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 {
2025-02-23 16:21:26 +00:00
fmt . Println ( "Puedes jugar 😊" )
2025-02-23 15:32:03 +00:00
} else {
2025-02-23 16:21:26 +00:00
fmt . Println ( "No puedes jugar 😕 ¡Adios!" )
2025-02-23 15:32:03 +00:00
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)
2025-02-23 16:21:26 +00:00
reader := bufio . NewReader ( os . Stdin )
score := 0 //here i am using := to type inference a score to 0
2025-02-23 15:32:03 +00:00
for i , p := range problems {
fmt . Printf ( "Problem %d: %s = \n" , i + 1 , p . q )
2025-02-23 16:21:26 +00:00
userInput , _ := reader . ReadString ( '\n' )
userInput = strings . TrimSpace ( userInput )
userInput = strings . ToLower ( userInput )
2025-02-23 15:32:03 +00:00
2025-02-23 16:21:26 +00:00
answer := strings . ToLower ( strings . TrimSpace ( p . a ) )
2025-02-23 15:32:03 +00:00
2025-02-23 16:21:26 +00:00
if userInput == answer {
score ++
}
2025-02-23 15:32:03 +00:00
}
2025-02-23 16:21:26 +00:00
fmt . Printf ( "You scored %v out of %v \n" , score , len ( problems ) )
2025-02-23 15:32:03 +00:00
2025-02-23 16:21:26 +00:00
percent := ( float64 ( score ) / float64 ( len ( problems ) ) ) * 100
2025-02-23 15:32:03 +00:00
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 )
}