Skip to content

D&D Character

Roll a Dungeons & Dragons character: six abilities, each the best three of four dice, and hitpoints from the constitution’s modifier.

Adapted from Exercism’s problem specification (MIT). Listed under Games & Simulation.

// FILE: dnd_character.nomi
//# A Dungeons & Dragons character with six random abilities, each the best
//# three of four six-sided dice, and hitpoints from its constitution.

import std::random.Generator

/// A character's six abilities and its hitpoints.
pub struct Character
    strength:     Int
    dexterity:    Int
    constitution: Int
    intelligence: Int
    wisdom:       Int
    charisma:     Int
    hitpoints:    Int
end

/// The modifier an ability `score` gives: half its distance from 10,
/// rounded down.
pub fn modifier(score: Int) -> Int
    Int.floor_div(score - 10, 2)
end

/// One ability score: four dice rolled, the lowest dropped, the rest added.
pub fn ability() -> Generator<Int>
    Generator.int(1..=6)
    |> Generator.list(4)
    |> Generator.map(fn rolls -> rolls |> Iter.sort() |> Iter.drop(1) |> Iter.sum())
end

/// A character with six abilities drawn in order, strength first.
pub fn character() -> Generator<Character>
    ability()
    |> Generator.list(6)
    |> Generator.map(fn scores ->
        [strength, dexterity, constitution, intelligence, wisdom, charisma] = scores else
            unreachable "Generator.list(6) draws six scores"
        end

        {
            strength:,
            dexterity:,
            constitution:,
            intelligence:,
            wisdom:,
            charisma:,
            hitpoints: 10 + modifier(constitution),
        }
    end)
end

// FILE: dnd_character_test.nomi
import
    std::random.{Generator, Seed}
    dnd_character
    dnd_character.Character
end

/// The values `generator` draws from the seeds 1 to 200.
fn draws<T>(generator: Generator<T>) -> List<T>
    1..=200
    |> Iter.map(fn n ->
        (value, _seed) = Generator.step(generator, Seed.from_int(n))
        value
    end)
    |> Iter.to_list()
end

fn is_in_range(score: Int) -> Bool
    score in 3..=18
end

fn is_valid(character: Character) -> Bool
    abilities = [
        character.strength,
        character.dexterity,
        character.constitution,
        character.intelligence,
        character.wisdom,
        character.charisma,
    ]

    Iter.all_match(abilities, is_in_range)
        and character.hitpoints == 10 + dnd_character.modifier(character.constitution)
end

test "ability modifier"
    [
        ("ability modifier for score 3 is -4",  3,  -4),
        ("ability modifier for score 4 is -3",  4,  -3),
        ("ability modifier for score 5 is -3",  5,  -3),
        ("ability modifier for score 6 is -2",  6,  -2),
        ("ability modifier for score 7 is -2",  7,  -2),
        ("ability modifier for score 8 is -1",  8,  -1),
        ("ability modifier for score 9 is -1",  9,  -1),
        ("ability modifier for score 10 is 0",  10, 0),
        ("ability modifier for score 11 is 0",  11, 0),
        ("ability modifier for score 12 is +1", 12, 1),
        ("ability modifier for score 13 is +1", 13, 1),
        ("ability modifier for score 14 is +2", 14, 2),
        ("ability modifier for score 15 is +2", 15, 2),
        ("ability modifier for score 16 is +3", 16, 3),
        ("ability modifier for score 17 is +3", 17, 3),
        ("ability modifier for score 18 is +4", 18, 4),
    ]
    |> subtests (description, score, expected), description
        assert dnd_character.modifier(score) == expected
    end
end

test "random ability is within range"
    assert dnd_character.ability()
        |> draws()
        |> Iter.all_match(is_in_range)
end

test "random character is valid"
    assert dnd_character.character()
        |> draws()
        |> Iter.all_match(is_valid)
end

test "each ability is only calculated once"
    seed = Seed.from_int(7)
    (first, _) = Generator.step(dnd_character.character(), seed)
    (again, _) = Generator.step(dnd_character.character(), seed)

    assert first == again
end
  • Randomness is std::random’s: ability and character return a Generator, a recipe that Generator.step runs against a Seed. Nothing here reads entropy, so the tests draw from fixed seeds and every run checks the same 200 characters.
  • An ability is Generator.int(1..=6) drawn four times with Generator.list, then sorted, the lowest dropped with Iter.drop(1), and the rest summed.
  • A character is six abilities drawn with Generator.list(6) and taken apart with a binding else on one list pattern, in the order the struct declares them. The else is unreachable, since Generator.list(6) always draws six.
  • Character is a struct, so an ability is a field read and is calculated once by construction. The two canonical “each ability is only calculated once” cases (strength alone, and every ability) are one test: the same seed draws an equal character twice.
  • modifier is Int.floor_div(score - 10, 2): the modifier rounds down, and / truncates toward zero, so a score of 3 would give -3 where the modifier is -4.
  • The canonical modifier cases are one subtests table; the property cases (“within range”, “is valid”) check every draw from seeds 1 to 200.