Skip to content

Commit

Permalink
Add example implementation of DndCharacter class
Browse files Browse the repository at this point in the history
Co-authored-by: KOTP <[email protected]>
  • Loading branch information
ccadden and kotp committed Jan 31, 2024
1 parent 01bf098 commit 6fe0cfe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion exercises/practice/dnd-character/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"authors": [],
"authors": [
"mr-sigma",
"kotp"
],
"files": {
"solution": [
"dnd_character.rb"
Expand Down
53 changes: 53 additions & 0 deletions exercises/practice/dnd-character/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Die
extend self

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

# This only calculates hit point modifier based on constitution
def modifier(constitution)
constitution/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

if $PROGRAM_NAME == __FILE__
puts DndCharacter.allocate.public_methods(false)
end

0 comments on commit 6fe0cfe

Please sign in to comment.