diff --git a/main b/main new file mode 100755 index 0000000..807c75f Binary files /dev/null and b/main differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..088dae4 --- /dev/null +++ b/main.go @@ -0,0 +1,128 @@ +package main + +import ( + "encoding/csv" + "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 +) + +/* + 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 holds your name. you have to type variables in go. + + 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 )=") + 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) + + for i, p := range problems { + fmt.Printf("Problem %d: %s = \n", i+1, p.q) + var answer string + fmt.Scanf("%s\n", &answer) + if answer == p.a { + fmt.Println("Correct!") + } + } + + score := 0 //here i am using := to type inference a score to 0 + num_quest := 2 //this is the total number of questions, will be used to calculate a percentage at the end. + fmt.Printf("Q1) ") + + var q1ans string + var q1ans2 string + + fmt.Scan(&q1ans, &q1ans2) + if q1ans+" "+q1ans2 == "Destiny Islands" { + fmt.Println("Correct!") + score++ + } else if q1ans+" "+q1ans2 == "destiny islands" { + fmt.Println("Correct!") + } else { + fmt.Println("Wrong!") + + } + + fmt.Printf(" ") + var worlds uint + fmt.Scan(&worlds) + + if worlds == 16 { + fmt.Println("Correct!") + score++ + } else { + fmt.Println("Wrong!") + } + + fmt.Printf("You scored %v out of %v \n", score, num_quest) + + percent := (float64(score) / float64(num_quest)) * 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) +} diff --git a/questions.csv b/questions.csv new file mode 100644 index 0000000..87358f4 --- /dev/null +++ b/questions.csv @@ -0,0 +1,5 @@ +"Where are Sora, Riku, and Kairi from?","Destiny Islands" +"How many worlds did Sora explore in KH2?",16 +"What is the name of Sora's original keyblade?","Kingdom Key" +"What is the name of the new enemy type introduced in KH2?","Nobody" +"Finish this quote: 'I know now, without a doubt, Kingdom Hearts is _____!'","light" diff --git a/questions.go b/questions.go deleted file mode 100644 index e69de29..0000000 diff --git a/tut.go b/tut.go deleted file mode 100644 index 8ceb2b4..0000000 --- a/tut.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import "fmt" // fmt is a package that contains basic functionality like printing to the terminal and taking in user input - -/* - 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 holds your name. you have to type variables in go. - - 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. - P*/ - fmt.Print("¿Este juego esta clasificado para mayores de 10 años. Cuantos años tienes?: ") - var age uint - - fmt.Scan(&age) - - if age >= 10 { - fmt.Println("Puedes jugar (=") - } else { - fmt.Println("No puedes jugar )=") - return - } - - score := 0 - num_quest := 2 - fmt.Printf("Where are Sora, Riku, and Kairi from? ") - - var q1ans string - var q1ans2 string - - fmt.Scan(&q1ans, &q1ans2) - if q1ans+" "+q1ans2 == "Destiny Islands" { - fmt.Println("Correct!") - score++ - } else if q1ans+" "+q1ans2 == "destiny islands" { - fmt.Println("Correct!") - } else { - fmt.Println("Wrong!") - - } - - fmt.Printf("How many worlds did Sora explore in KH2? ") - var worlds uint - fmt.Scan(&worlds) - - if worlds == 16 { - fmt.Println("Correct!") - score++ - } else { - fmt.Println("Wrong!") - } - - fmt.Printf("You scored %v out of %v \n", score, num_quest) - - percent := (float64(score) / float64(num_quest)) * 100 - fmt.Printf("You scored: %v%%. \n", percent) -}