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 Feb 10, 2024
1 parent 5bea145 commit cf9a2dc
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions exercises/practice/dnd-character/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cf9a2dc

Please sign in to comment.