From cf9a2dc1d3dc34f81e2e9683c82c8a70fef9e1fe Mon Sep 17 00:00:00 2001 From: Chris Cadden Date: Sat, 27 Jan 2024 14:24:57 -0500 Subject: [PATCH] Add example implementation of DndCharacter class Co-authored-by: KOTP --- .../practice/dnd-character/.meta/example.rb | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/exercises/practice/dnd-character/.meta/example.rb b/exercises/practice/dnd-character/.meta/example.rb index e69de29bb2..f1627306dd 100644 --- a/exercises/practice/dnd-character/.meta/example.rb +++ b/exercises/practice/dnd-character/.meta/example.rb @@ -0,0 +1,43 @@ +module Die + module_function + + def roll(roll: 4, die: 6, remove_lowest: 1) + roll.times.map { rand(1..die) }.sort.pop(roll - remove_lowest).sum + end +end + +module Modifiable + def modifier(ability_score) + ability_score / 2 - 5 + end +end + +class DndCharacter + extend Modifiable + + BASE_HITPOINTS = 10 + + private_constant :BASE_HITPOINTS + + private + def initialize + @strength = Die.roll + @dexterity = Die.roll + @constitution = Die.roll + @intelligence = Die.roll + @wisdom = Die.roll + @charisma = Die.roll + + @hitpoints = BASE_HITPOINTS + self.class.modifier(constitution) + end + + public + + attr_reader :strength, + :dexterity, + :constitution, + :intelligence, + :wisdom, + :charisma, + :hitpoints +end