Browse Source

Add exploding attribute to dice (no stats change yet)

pull/1/head
Arthur POULET 5 years ago
parent
commit
958228ec9a
No known key found for this signature in database
GPG Key ID: FC154EA63FF5D8BC
  1. 0
      .gitignore
  2. 0
      .travis.yml
  3. 24
      Makefile
  4. 2
      shard.yml
  5. 14
      spec/rollable/die.cr
  6. 30
      src/rollable/die.cr
  7. 2
      src/rollable/version.cr

0
.gitignore vendored

0
.travis.yml

24
Makefile

@ -0,0 +1,24 @@
NAME=`ls src/*/ -d | cut -f2 -d'/'`
all: deps_opt build
run:
crystal run src/$(NAME).cr
build:
crystal build src/$(NAME).cr --stats
release:
crystal build src/$(NAME).cr --stats --release
test:
crystal spec
deps:
crystal deps install
deps_update:
crystal deps update
deps_opt:
@[ -d lib/ ] || make deps
doc:
crystal docs
clean:
rm $(NAME)
.PHONY: all run build release test deps deps_update clean doc

2
shard.yml

@ -1,5 +1,5 @@
name: rollable
version: 0.1.3
version: 0.1.4
authors:
- Arthur Poulet <arthur.poulet@mailoo.org>

14
spec/rollable/die.cr

@ -3,6 +3,7 @@ describe Rollable::Die do
Rollable::Die.new(1..20).should be_a(Rollable::Die)
Rollable::Die.new(10..20).should be_a(Rollable::Die)
Rollable::Die.new(20).should be_a(Rollable::Die)
Rollable::Die.new(20, true).should be_a(Rollable::Die)
expect_raises { Rollable::Die.new(1001) }
end
@ -27,18 +28,25 @@ describe Rollable::Die do
Rollable::Die.new(1..20).like?(Rollable::Die.new(1..20)).should eq true
Rollable::Die.new(1..20).like?(Rollable::Die.new(-20..-1)).should eq true
Rollable::Die.new(1..20).like?(Rollable::Die.new(1..10)).should eq false
Rollable::Die.new(1..20, true).like?(Rollable::Die.new(1..20, false)).should eq true
end
it "negative?" do
Rollable::Die.new(1..20).negative?.should eq false
Rollable::Die.new(-1..20).negative?.should eq false
Rollable::Die.new(1..-20).negative?.should eq false
Rollable::Die.new(-1..-20).negative?.should eq true
Rollable::Die.new(1..20, true).negative?.should eq false
Rollable::Die.new(-1..-20, true).negative?.should eq true
end
it "fixed?" do
Rollable::Die.new(1..20).fixed?.should eq false
Rollable::Die.new(1..1).fixed?.should eq true
Rollable::Die.new(20..20).fixed?.should eq true
Rollable::Die.new(1).fixed?.should eq true
Rollable::Die.new(1..20, true).fixed?.should eq false
Rollable::Die.new(1, true).fixed?.should eq true
end
it "reverse" do
@ -46,12 +54,15 @@ describe Rollable::Die do
Rollable::Die.new(1..20).reverse.max.should eq -1
Rollable::Die.new(1..1).reverse.min.should eq -1
Rollable::Die.new(1..1).reverse.max.should eq -1
# TODO: Rollable::Die.new(1..20, true).reverse.min.should eq -20
# TODO: Rollable::Die.new(1..20, true).reverse.max.should eq -1
end
it "to_s" do
Rollable::Die.new(1..20).to_s.should eq "D20"
Rollable::Die.new(2..2).to_s.should eq "2"
Rollable::Die.new(2..4).to_s.should eq "D(2,4)"
Rollable::Die.new(1..20, true).to_s.should eq "!D20"
end
it "cmp" do
@ -84,5 +95,8 @@ describe Rollable::Die do
((Rollable::Die.new(1..4) <=> Rollable::Die.new(1..4)) == 0).should eq true
((Rollable::Die.new(2..3) <=> Rollable::Die.new(1..4)) < 0).should eq true
(Rollable::Die.new(2..6) == Rollable::Die.new(4..4)).should eq false
# TODO: (Rollable::Die.new(1..20, true) == Rollable::Die.new(1..20, false)).should eq false
# TODO: (Rollable::Die.new(1..20, true) > Rollable::Die.new(1..20, false)).should eq true
end
end

30
src/rollable/die.cr

@ -16,19 +16,22 @@ require "./is_rollable"
# TODO: make it a Struct ?
class Rollable::Die < Rollable::IsRollable
MAX = 1000
EXPLODING_ITERATIONS = 3
@faces : Range(Int32, Int32)
getter exploding : Bool
getter faces
def initialize(@faces)
def initialize(@faces, @exploding = false)
raise ParsingError.new "Cannot die with more than #{MAX} faces (#{@faces})" if @faces.size > MAX
end
def clone
Die.new(@faces)
Die.new(@faces, @exploding)
end
def initialize(nb_faces : Int32)
def initialize(nb_faces : Int32, @exploding = false)
raise ParsingError.new "Cannot die with more than #{MAX} faces (#{nb_faces})" if nb_faces > MAX
@faces = 1..nb_faces
end
@ -57,7 +60,7 @@ class Rollable::Die < Rollable::IsRollable
# Die.new(1..6).reverse # => Die.new -6..-1
# ```
def reverse : Die
Die.new -max..-min
Die.new -max..-min, @exploding
end
def reverse!
@ -90,21 +93,24 @@ class Rollable::Die < Rollable::IsRollable
# - It may be a dice ```(1..n) => "D#{n}"```
# - Else, ```(a..b) => "D(#{a},#{b})"```
def to_s : String
if self.size == 1
min.to_s
elsif self.min == 1
"D#{self.max}"
else
"D(#{self.min},#{self.max})"
end
string = if self.size == 1
min.to_s
elsif self.min == 1
"D#{self.max}"
else
"D(#{self.min},#{self.max})"
end
string = "!#{string}" if @exploding
return string
end
def ==(right : Die)
@faces == right.faces
@faces == right.faces && @exploding == right.exploding
end
{% for op in [">", "<", ">=", "<="] %}
def {{ op.id }}(right : Die)
return false if @exploding != right.exploding
average != right.average ?
average {{ op.id }} right.average :
max != right.max ?

2
src/rollable/version.cr

@ -1,3 +1,3 @@
module Rollable
VERSION = "0.1.2"
VERSION = "0.1.4"
end

Loading…
Cancel
Save