Guessing Games
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Guess the Word
Create a word guessing game. Manually bind a guess and an answer. Return "Correct" if the guess is equal to the answer, and otherwise return "Incorrect."
Ensure your solutions works for both an incorrect and correct player guess.
Example Solution
guess = "hello"
answer = "hello"
guess == answer && "Correct!" || "Incorrect."
Enter your solution below.
Bonus: Guess the Random Word
We can use Enum.random/1 to pick a random value from a list.
Enum.random(["hello", "hi", "howdy"])
Armed with this knowledge, you’re going to create a word guessing game.
-
Bind a variable
guessto the player’s guess as a string. -
Bind a variable
answerto a random word (you may create a list of random words to choose from) -
If the
guessmatches the answer, return"Correct!", otherwise return"Incorrect.".Example Solution
guess = "hello" answer = Enum.random(["hello", "hi", "howdy"]) guess == answer && "Correct!" || "Incorrect."
Enter your solution below.
Bonus: Guess the Number
We can generate a random integer between two values by providing a range to Enum.random/1.
Enum.random(1..10)
Now let’s build a number guessing game
-
Bind a variable
answerto a random integer between1and10. -
Bind a variable
guessto the player’s guess. -
If the
guessequalsanswer, return"Correct!". -
If the
guessis lower than the answer, return"Too low!". -
If the
guessis higher than the answer, return"Too high!".Example Solution
condallows us to handle several different conditions.guess = Enum.random(1..10) answer = 7 cond do guess == answer -> "Correct!" guess < answer -> "Too low!" guess > answer -> "Too high!" endHowever, we can also solve this problem using purely boolean operators.
guess = Enum.random(1..10) answer = 7 guess == answer && "Correct" || guess < answer && "Too low!" || guess > answer && "Too high!"We can optionally simplify the solution by omitting the last condition. However, this could lead to unhandled bugs.
Enter your solution below.
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "guessing_games_reading"
"exercises" -> "guessing_games_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b guessing-games-exercise
$ git add .
$ git commit -m "finish guessing games exercise"
$ git push origin guessing-games-exercise
Create a pull request from your guessing-games-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Mad Libs | Atoms |