Class: ParaDice::Results::CancelOpposing

Inherits:
Object
  • Object
show all
Defined in:
lib/para_dice/results/cancel_opposing.rb

Overview

Results class to have some faces cancel other faces out of the results,

can also be used to drop faces entirely by defining them as their own opposite

Constant Summary collapse

DEFAULT_OPPOSING =

set empty here so it will work if no values passed to it.

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_opposing = DEFAULT_OPPOSING) ⇒ CancelOpposing

Returns a new instance of CancelOpposing.

Parameters:

  • opposing (Array<Array>)

    defaults to DEFAULT_OPPOSING=[]



19
20
21
# File 'lib/para_dice/results/cancel_opposing.rb', line 19

def initialize(default_opposing = DEFAULT_OPPOSING)
  @default_opposing = default_opposing
end

Instance Attribute Details

#default_opposingObject

Returns the value of attribute default_opposing.



14
15
16
# File 'lib/para_dice/results/cancel_opposing.rb', line 14

def default_opposing
  @default_opposing
end

#rw default_opposing(default_opposing) ⇒ Array<Array>

default to DEFAULT_OPPOSING = []

Returns:

  • (Array<Array>)

    list of pairs of faces that cancel each other out use [[‘blank’,‘blank’]] as opposing to eliminate blank results use [[‘good’,‘bad’], [‘hero’,‘villain’]] to have good and bad, heroes villains to cancel each other out



14
# File 'lib/para_dice/results/cancel_opposing.rb', line 14

attr_accessor :default_opposing

Instance Method Details

#resolve(faces, opposing = default_opposing) ⇒ Array<String>

Parameters:

  • faces (Array<#to_s>)
  • opposing (Array<Array>) (defaults to: default_opposing)

    defaults to default_opposing

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/para_dice/results/cancel_opposing.rb', line 26

def resolve(faces, opposing = default_opposing)
  summary = Utility.to_summary(faces)
  opposing.each do |one_face, another_face|
    one_face     = one_face.to_s
    another_face = another_face.to_s
    next unless summary.key?(one_face) && summary.key?(another_face)
    if summary[one_face] == summary[another_face]
      summary.delete(one_face)
      summary.delete(another_face)
    else
      low, high     = [one_face, another_face].sort_by { |e| summary[e] }
      summary[high] -= summary[low]
      summary.delete(low)
    end
  end

  Utility.from_summary(summary)
end