Avatar of artemkorsakov

artemkorsakov's solution

to D&D Character in the C# Track

Published at Apr 27 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

For a game of Dungeons & Dragons, each player starts by generating a character they can play with. This character has, among other things, six abilities; strength, dexterity, constitution, intelligence, wisdom and charisma. These six abilities have scores that are determined randomly. You do this by rolling four 6-sided dice and record the sum of the largest three dice. You do this six times, once for each ability.

Your character's initial hitpoints are 10 + your character's constitution modifier. You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down.

Write a random character generator that follows the rules above.

For example, the six throws of four dice may look like:

  • 5, 3, 1, 6: You discard the 1 and sum 5 + 3 + 6 = 14, which you assign to strength.
  • 3, 2, 5, 3: You discard the 2 and sum 3 + 5 + 3 = 11, which you assign to dexterity.
  • 1, 1, 1, 1: You discard the 1 and sum 1 + 1 + 1 = 3, which you assign to constitution.
  • 2, 1, 6, 6: You discard the 1 and sum 2 + 6 + 6 = 14, which you assign to intelligence.
  • 3, 5, 3, 4: You discard the 3 and sum 5 + 3 + 4 = 12, which you assign to wisdom.
  • 6, 6, 6, 6: You discard the 6 and sum 6 + 6 + 6 = 18, which you assign to charisma.

Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6.

Notes

Most programming languages feature (pseudo-)random generators, but few programming languages are designed to roll dice. One such language is Troll.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit DndCharacter.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Simon Shine, Erik Schierboom https://github.com/exercism/problem-specifications/issues/616#issuecomment-437358945

DndCharacterTest.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using System.Linq;
using Xunit;

public class DndCharacterTest
{
    [Fact]
    public void Ability_modifier_for_score_3_is_minus_4()
    {
        Assert.Equal(-4, DndCharacter.Modifier(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_4_is_minus_3()
    {
        Assert.Equal(-3, DndCharacter.Modifier(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_5_is_minus_3()
    {
        Assert.Equal(-3, DndCharacter.Modifier(5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_6_is_minus_2()
    {
        Assert.Equal(-2, DndCharacter.Modifier(6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_7_is_minus_2()
    {
        Assert.Equal(-2, DndCharacter.Modifier(7));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_8_is_minus_1()
    {
        Assert.Equal(-1, DndCharacter.Modifier(8));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_9_is_minus_1()
    {
        Assert.Equal(-1, DndCharacter.Modifier(9));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_10_is_0()
    {
        Assert.Equal(0, DndCharacter.Modifier(10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_11_is_0()
    {
        Assert.Equal(0, DndCharacter.Modifier(11));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_12_is_1()
    {
        Assert.Equal(1, DndCharacter.Modifier(12));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_13_is_1()
    {
        Assert.Equal(1, DndCharacter.Modifier(13));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_14_is_2()
    {
        Assert.Equal(2, DndCharacter.Modifier(14));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_15_is_2()
    {
        Assert.Equal(2, DndCharacter.Modifier(15));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_16_is_3()
    {
        Assert.Equal(3, DndCharacter.Modifier(16));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_17_is_3()
    {
        Assert.Equal(3, DndCharacter.Modifier(17));
    }

    [Fact(Skip = "Remove to run test")]
    public void Ability_modifier_for_score_18_is_4()
    {
        Assert.Equal(4, DndCharacter.Modifier(18));
    }

    [Fact(Skip = "Remove to run test")]
    public void Random_ability_is_within_range()
    {
        for (var i = 0; i < 10; i++)
        {
            Assert.InRange(DndCharacter.Ability(), 3, 18);
        }
    }

    [Fact(Skip = "Remove to run test")]
    public void Random_character_is_valid()
    {
        for (var i = 0; i < 10; i++)
        {
            var sut = DndCharacter.Generate();
            Assert.InRange(sut.Strength, 3, 18);
            Assert.InRange(sut.Dexterity, 3, 18);
            Assert.InRange(sut.Constitution, 3, 18);
            Assert.InRange(sut.Intelligence, 3, 18);
            Assert.InRange(sut.Wisdom, 3, 18);
            Assert.InRange(sut.Charisma, 3, 18);
            Assert.Equal(sut.Hitpoints, 10 + DndCharacter.Modifier(sut.Constitution));
        }
    }

    [Fact(Skip = "Remove to run test")]
    public void Each_ability_is_only_calculated_once()
    {
        for (var i = 0; i < 10; i++)
        {
            var sut = DndCharacter.Generate();
            Assert.Equal(sut.Strength, sut.Strength);
            Assert.Equal(sut.Dexterity, sut.Dexterity);
            Assert.Equal(sut.Constitution, sut.Constitution);
            Assert.Equal(sut.Intelligence, sut.Intelligence);
            Assert.Equal(sut.Wisdom, sut.Wisdom);
            Assert.Equal(sut.Charisma, sut.Charisma);
        }
    }
}
using System;
using System.Linq;

public class DndCharacter
{
    public int Strength { get; } = Ability();
    public int Dexterity { get; } = Ability();
    public int Constitution { get; } = Ability();
    public int Intelligence { get; } = Ability();
    public int Wisdom { get; } = Ability();
    public int Charisma { get; } = Ability();
    public int Hitpoints { get; private set; }

    public static int Modifier(int score) => (int)Math.Floor(((double)score - 10) / 2);

    public static int Ability()
    {
        int[] dices = new int[] { getDice(), getDice(), getDice(), getDice() };
        return dices.Sum() - dices.Min();
    }

    public static DndCharacter Generate()
    {
        var dndCharacter = new DndCharacter();
        dndCharacter.Hitpoints = 10 + Modifier(dndCharacter.Constitution);
        return dndCharacter;
    }

    private static int getDice() => random.Next(6) + 1;
    private static Random random = new Random();
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?