Skip to content

Instantly share code, notes, and snippets.

@omnifroodle
Created May 27, 2011 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omnifroodle/994471 to your computer and use it in GitHub Desktop.
Save omnifroodle/994471 to your computer and use it in GitHub Desktop.
Earthdawn Dice
class Die
attr_reader :d, :face, :bonus, :modifier
def initialize(d)
parse_d(d)
@face = roll
if @d == @face
@bonus = Die.new(d)
end
end
def result
bonus = @bonus.nil? ? 0 : @bonus.result
total = @face - @modifier
total = 1 if total < 1
total + bonus
end
private
def parse_d(d)
case d
when String
@d, @modifier = d.split('-').map{|i| i.to_i}
else
@d = d
@modifier = 0
end
end
def roll
1 + rand(d)
end
end
require 'test_helper'
class DieTest < ActiveSupport::TestCase
should 'roll a bonus die' do
Die.any_instance.stubs(:roll).returns(6, 4)
die = Die.new(6)
assert die.result == 10
end
should 'roll multiple bonus dice' do
Die.any_instance.stubs(:roll).returns(6, 6, 6, 4)
die = Die.new(6)
assert die.result == 22
end
should 'accept modifier' do
Die.any_instance.stubs(:roll).returns(5)
die = Die.new("6-3")
assert die.modifier == 3
assert die.result == 2
end
should 'not be less then 1' do
Die.any_instance.stubs(:roll).returns(1)
die = Die.new("6-3")
assert die.modifier == 3
assert die.result > 0
end
should 'roll bonus on modified die' do
Die.any_instance.stubs(:roll).returns(6, 3)
die = Die.new('6-3')
assert die.result == 4
end
end
class Roll
include ActiveModel::AttributeMethods
STEPS = {
1 => ['6-3'],
2 => ['6-2'],
3 => ['6-1'],
4 => [6],
5 => [8],
6 => [10],
7 => [12],
8 => [6, 6],
9 => [8, 6]
}
attr_accessor :result, :step, :dice
def initialize(options = {})
@step = options[:step] || 4
@dice = STEPS[@step].map{|d| Die.new(d)}
@result = @dice.inject(0){|sum, n| sum + n.result}
end
end
require 'test_helper'
class RollTest < ActiveSupport::TestCase
test 'testing' do
assert true
end
should 'have a result' do
roll = Roll.new()
assert_not_nil roll.result
end
should 'accept a step number on create' do
roll = Roll.new(:step => 4)
assert roll.step = 4
end
should 'roll the right dice for several step numbers' do
roll = Roll.new(:step => 1)
assert roll.dice.first.d == 6, 'sixer for step 1'
assert roll.dice.first.modifier == 3, 'minus 3 for step 1'
roll = Roll.new(:step => 4)
assert roll.dice.first.d == 6, 'sixer for step 4'
roll = Roll.new(:step => 5)
assert roll.dice.first.d == 8, 'eight-sided for step 5'
roll = Roll.new(:step => 8)
assert roll.dice.map{|d| d.d} == [6,6], 'two sixes for step 8'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment