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

Item Generator

item_generator.livemd

Item Generator

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Generate Items

Define An Item Struct

You are building a game with a massive list of magical items.

An Item struct will have a type, effect, level, size, and style.

In the Elixir cell below, define an Item struct.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    defmodule Item do
    end

    assert Keyword.get(Item.__info__(:functions), :__struct__),
           "Ensure you use `defstruct`."

    assert match?(%{type: nil, effect: nil, level: nil, size: nil, style: nil}, struct(Item)),
           "Ensure you use `defstruct` with :type, :effect, :level, :size, and :style."
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
defmodule Item do
end

Generate Every Permutation of Items

You have been given the possible types, effects, levels, sizes, and styles. In the Elixir cell below, generate a list items with every possible combination of item properties. Each should be an Item struct.

Replace nil with your answer.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    types = [
      "sword",
      "halberd",
      "crossbow",
      "hammer",
      "mace",
      "longsword",
      "shortsword",
      "longbow"
    ]

    effects = [
      "protection",
      "healing",
      "speed",
      "power",
      "jump"
    ]

    styles = ["holy", "dark", "heroic", "crude", "mundane", "lavish"]
    sizes = 1..10
    levels = 1..100

    items = nil

    solutions_item_generator =
      for type <- types,
          effect <- effects,
          style <- styles,
          size <- sizes,
          level <- levels do
        %Item{type: type, effect: effect, style: style, size: size, level: level}
      end

    assert is_list(items), "`items` should be a list."

    expected_items = solutions_item_generator

    expected_length = length(expected_items)

    assert length(items) == expected_length,
           "There should be #{expected_length} permutations of items."

    Enum.each(items, fn item ->
      assert is_struct(item), "Each item should be an `Item` struct."
      assert match?(%{type: _, effect: _, style: _, size: _, level: _, __struct__: _}, item)
    end)
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
types = [
  "sword",
  "halberd",
  "crossbow",
  "hammer",
  "mace",
  "longsword",
  "shortsword",
  "longbow"
]

effects = [
  "protection",
  "healing",
  "speed",
  "power",
  "jump"
]

styles = ["holy", "dark", "heroic", "crude", "mundane", "lavish"]
sizes = 1..10
levels = 1..100

items = nil

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish item generator exercise"