Powered by AppSignal & Oban Pro

Games: Wordle

exercises/games_wordle.livemd

Games: Wordle

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Return Home Report An Issue

Games: Wordle

In your existing Games project, you’re going to create a Wordle game.

Wordle is a popular game where players get six attempts to guess a word. They are given feedback clues to guess the word.

  • Green if the letter is in the word and in the correct spot.
  • Yellow if the letter is in the word but in the incorrect spot.
  • Gray if the letter is not in the word.

For example for the word “TOAST” the guess “TARTS” would be

flowchart LR
style 1 fill:green
style 2 fill:yellow
style 3 fill:gray
style 4 fill:yellow
style 5 fill:yellow

1[T] --- 2[A] --- 3[R] --- 4[T] --- 5[S]

You should be able to start your project by running the following from the game folder in the command line.

iex -S mix

Given an answer and a guess, your Games.Wordle module will provide feedback as a list of color atoms :green, :yellow, and :grey.

                         #answer   #guess
iex> Games.Wordle.feedback("aaaaa", "aaaaa")
[:green, :green, :green, :green, :green]

iex> Games.Wordle.feedback("aaaaa", "aaaab")
[:green, :green, :green, :green, :grey]

iex> Games.Wordle.feedback("abdce", "edcba")
[:yellow, :yellow, :yellow, :yellow, :yellow]

# If there are duplicate characters in the guess prioritize exact matches.
iex> Games.Wordle.feedback("aaabb", "xaaaa")
[:grey, :green, :green, :yellow, :grey]

Test and implement a Games.Wordle module in your Games project.

Play

Use the IEx shell to start a game of Wordle. This should prompt the user to guess a five letter word.

iex> Games.Wordle.play()
Enter a five letter word:  

Randomly generate a word for the wordle game. You might randomly retrieve a word from a list of words such as ["toast", "tarts", "hello", "beats"].

Retrieve user input and provide feedback based on their guess. For example, this would be the output if the word toast were randomly generated.

Enter a give letter word: tarts
[:green, :yellow, :grey, :yellow, :yellow]

Bonus: Six Guesses

After six guesses, the player should lose the game if they do not get the correct answer.

Bonus: Color Feedback

Use IO.ANSI to provide actual colored feedback.

IO.puts(IO.ANSI.green() <> "green")
IO.puts(IO.ANSI.yellow() <> "yellow")
IO.puts(IO.ANSI.light_black() <> "grey")

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "games_wordle_reading"
    "exercises" -> "games_wordle_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 games-wordle-exercise
$ git add .
$ git commit -m "finish games wordle exercise"
$ git push origin games-wordle-exercise

Create a pull request from your games-wordle-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
ExUnit With Mix Doctests