From d8ee010c053850a89b2a91cf26a48450867b896f Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 14:03:27 +0100 Subject: [PATCH 1/6] rewrote readme for escript running with mix --- README.md | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) 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` From 1107023742a4f481cea261b70def552e2a392459 Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 14:05:59 +0100 Subject: [PATCH 2/6] edited mix.exs to add escript and run --- mix.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/mix.exs b/mix.exs index 0f206eb..b2b1fd7 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: QuizMaster], build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] From 185a9e22e3211250a79e58326d9d0561a95f929c Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 14:27:03 +0100 Subject: [PATCH 3/6] extracted actions for correct input into method called correct_input_action, similarly extracted print_incorrect_message into incorrect_input_action which also asks the player if they want to start over --- lib/trivia_app.ex | 33 ++++++++++++++++++++++++++------- test/trivia_app_test.exs | 8 +++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/trivia_app.ex b/lib/trivia_app.ex index 65c635f..27fcd74 100644 --- a/lib/trivia_app.ex +++ b/lib/trivia_app.ex @@ -16,7 +16,8 @@ defmodule TriviaApp do 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 +27,25 @@ 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 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 +58,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/test/trivia_app_test.exs b/test/trivia_app_test.exs index 74bb993..bb7706c 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 @@ -46,4 +46,10 @@ defmodule TriviaAppTest do 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", fn -> + TriviaApp.start_quiz + end) =~ "perfect score" + end end From c23e274f1fb6bdb6c38701535be82ff76898b90d Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 14:54:19 +0100 Subject: [PATCH 4/6] added more questions so now there are ten, added more inputs to tests which ask for all answers to reach end --- lib/trivia_app.ex | 12 ++++++++++-- test/trivia_app_test.exs | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/trivia_app.ex b/lib/trivia_app.ex index 27fcd74..d152a3e 100644 --- a/lib/trivia_app.ex +++ b/lib/trivia_app.ex @@ -5,8 +5,16 @@ 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 diff --git a/test/trivia_app_test.exs b/test/trivia_app_test.exs index bb7706c..38b6326 100644 --- a/test/trivia_app_test.exs +++ b/test/trivia_app_test.exs @@ -42,13 +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", fn -> + assert capture_io("b\ny\na\nc\nc\nb\nc\nc\na\nb\nc\na", fn -> TriviaApp.start_quiz end) =~ "perfect score" end From d254b4f4c921917414fbb17cb7b34223a14377b0 Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 14:59:41 +0100 Subject: [PATCH 5/6] changed name of main module in escript to be correct, reformatted questions --- lib/trivia_app.ex | 20 ++++++++++---------- mix.exs | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/trivia_app.ex b/lib/trivia_app.ex index d152a3e..e268308 100644 --- a/lib/trivia_app.ex +++ b/lib/trivia_app.ex @@ -5,16 +5,16 @@ defmodule TriviaApp do end @questions [ - %{ 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"} + %{ 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 diff --git a/mix.exs b/mix.exs index b2b1fd7..051007e 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule TriviaApp.Mixfile do [app: :trivia_app, version: "0.1.0", elixir: "~> 1.3", - escript: [main_module: QuizMaster], + escript: [main_module: TriviaApp], build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] From e20f4e75dd8bd4e797b0938517e08a2be2aaef45 Mon Sep 17 00:00:00 2001 From: Daisy Molving Date: Mon, 5 Sep 2016 15:02:51 +0100 Subject: [PATCH 6/6] added private function to introduce quiz, called before questions are asked --- lib/trivia_app.ex | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/trivia_app.ex b/lib/trivia_app.ex index e268308..4e9e0f4 100644 --- a/lib/trivia_app.ex +++ b/lib/trivia_app.ex @@ -18,6 +18,7 @@ defmodule TriviaApp do ] def start_quiz do + introduce_quiz score = 0 ask_question(@questions, score) end @@ -41,6 +42,10 @@ defmodule TriviaApp do 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)