Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Save Game

exercises/save_game.livemd

Save Game

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

Navigation

Home Report An Issue File DrillsFile System Todo

Save Game

Many games have a Save function, which allows players to save the current state of the game. In some cases, the saved game is stored in a save file.

You’re going to implement a Game module which simulates saving a video game’s state using the file system. You should be able to store some arbitrary Elixir term in a given file, and retrieve it.

Example Solution

defmodule Game do
  def save(game_state, filename) do
    File.write!(filename, :erlang.term_to_binary(game_state))
  end

  def load(filename) do
    File.read!(filename) |> :erlang.binary_to_term()
  end
end

Implement the Game module as documented below.

defmodule Game do
  @moduledoc """
  Documentation for `Game`

  ## Examples

      game_state = %{name: "Peter Parker", level: 10, location: "New York"}
      
      Game.save(game_state, "save_file1")
      :ok
      
      Game.load("save_file1")
      %{name: "Peter Parker", level: 10, location: "New York"}
  """

  @doc """
  Save an elixir term into a given file name.
  """
  def save(data, filename) do
  end

  @doc """
  Retrieve an elixir term from a given file name.
  """
  def load(filename) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Save Game exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue File DrillsFile System Todo