diff --git a/README.md b/README.md index 80f2c4d..6386302 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,7 @@ -# TriviaApp +To Run This App: -**TODO: Add description** +Install Elixir -## Installation - -If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: - - 1. Add `trivia_app` to your list of dependencies in `mix.exs`: - - ```elixir - def deps do - [{:trivia_app, "~> 0.1.0"}] - end - ``` - - 2. Ensure `trivia_app` is started before your application: - - ```elixir - def application do - [applications: [:trivia_app]] - end - ``` +Run `mix escript.build` +Run `./quiz_master` diff --git a/lib/trivia_app.ex b/lib/trivia_app.ex index 65c635f..4e9e0f4 100644 --- a/lib/trivia_app.ex +++ b/lib/trivia_app.ex @@ -5,18 +5,28 @@ defmodule TriviaApp do end @questions [ - %{ question: "Samite is a type of what: a) Fabric? b) Stone? c) Dog? d) Cake?", answer: "a" }, - %{ question: "Vermillion is a shade of which colour: a) Green? b) Blue? c) Red? d) Yellow?", answer: "c" } + %{ question: "Samite is a type of what: \na) Fabric \nb) Stone \nc) Dog \nd) Cake ", answer: "a" }, + %{ question: "Vermillion is a shade of which colour: \na) Green \nb) Blue \nc) Red \nd) Yellow ", answer: "c" }, + %{ question: "An anemometer is a gauge used for recording the speed of what: \na) Light \nb) Spacecraft \nc) Wind \nd) Athletes ", answer: "c"}, + %{ question: "English novelist William Godwin fathered which novelist daughter: \na) George Eliot \nb) Mary Shelley \nc) Emily Brontë \nd) Jane Austen ", answer: "b"}, + %{ question: "Scurvy is a deficiency in what: \na) Vitamin A \nb) Vitamin B \nc) Vitamin C \nd) Vitamin D ", answer: "c"}, + %{ question: "What does an ornithologist study? \na) Diseases \nb) Ancient Pottery \nc) Birds \nd) Teeth ", answer: "c"}, + %{ question: "What is the scientific name for red blood cells? \na) Erythrocytes \nb) Leukocytes \nc) Thrombocytes \nd) Keratinocytes ", answer: "a"}, + %{ question: "What are the names of Mars’ two satellites? \na) Pallas and Vesta \nb) Phobos and Deimos \nc) Ganymede and Callisto \nd) Triton and Nereid ", answer: "b"}, + %{ question: "What is the atomic number for Lithium? \na) 7 \nb) 18 \nc) 3 \nd) 24 ", answer: "c"}, + %{ question: "Asteroids that orbit the sun without crossing Earth’s orbit is are called: \na) Amor \nb) Aten \nc) Apollo \nd) Icarus ", answer: "a"} ] def start_quiz do + introduce_quiz score = 0 ask_question(@questions, score) end def ask_question([], _), do: perfect_score_message def ask_question([current_question | next_questions], score) do - validate_input(request_answer(current_question[:question]), current_question[:answer], score, next_questions) + request_answer(current_question[:question]) + |> validate_input(current_question[:answer], score, next_questions) end def request_answer(question) do @@ -26,11 +36,29 @@ defmodule TriviaApp do def validate_input(user_input, correct_answer, score, next_questions) do cond do String.strip(user_input) == correct_answer -> - print_correct_answer_message - print_score(score + 1) - ask_question(next_questions, score + 1) + correct_input_action(score, next_questions) :else -> - print_incorrect_answer_message + incorrect_input_action + end + end + + defp introduce_quiz do + IO.puts("Welcome to the quiz! Here are your Questions: ") + end + + defp correct_input_action(score, next_questions) do + print_correct_answer_message + print_score(score + 1) + ask_question(next_questions, score + 1) + end + + defp incorrect_input_action do + print_incorrect_answer_message + cond do + String.strip(start_over_message) == "y" -> + start_quiz + :else -> + IO.puts("Goodbye!") end end @@ -43,11 +71,15 @@ defmodule TriviaApp do end defp print_incorrect_answer_message do - IO.puts("OOPS! That answer is incorrect!") + IO.puts("OOPS! That answer is incorrect! ") + end + + defp start_over_message do + IO.gets("Would you like to start again? Type y for yes, or n for no: ") end defp perfect_score_message do - IO.puts("Congratulations! You have finished the quiz with a perfect score") + IO.puts("Congratulations! You have finished the quiz with a perfect score!") end end diff --git a/mix.exs b/mix.exs index 0f206eb..051007e 100644 --- a/mix.exs +++ b/mix.exs @@ -5,6 +5,7 @@ defmodule TriviaApp.Mixfile do [app: :trivia_app, version: "0.1.0", elixir: "~> 1.3", + escript: [main_module: TriviaApp], build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] diff --git a/test/trivia_app_test.exs b/test/trivia_app_test.exs index 74bb993..38b6326 100644 --- a/test/trivia_app_test.exs +++ b/test/trivia_app_test.exs @@ -10,7 +10,7 @@ defmodule TriviaAppTest do end test "validates user input by checking it against incorrect answer" do - assert capture_io(fn -> + assert capture_io("b\na", fn -> TriviaApp.validate_input("a", "b", 0, [%{question: "huh?", answer: "a"}]) end) =~ "incorrect" end @@ -42,7 +42,13 @@ defmodule TriviaAppTest do end test "given correct answers only, the game ends with a perfect score" do - assert capture_io("a\nc", fn -> + assert capture_io("a\nc\nc\nb\nc\nc\na\nb\nc\na", fn -> + TriviaApp.start_quiz + end) =~ "perfect score" + end + + test "given incorrect answers, but also given the order to start again, quiz starts again" do + assert capture_io("b\ny\na\nc\nc\nb\nc\nc\na\nb\nc\na", fn -> TriviaApp.start_quiz end) =~ "perfect score" end