Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
Arthur POULET | 84ef972fec | |
Arthur POULET | 050af10ac2 | |
Arthur POULET | d85b7f5b6e | |
Arthur POULET | 9859028786 | |
Arthur POULET | 450c67004a | |
Arthur POULET | 766c1cb064 | |
Lucie Dispot | b13efe4adb | |
Arthur POULET | 98fb5f6683 | |
Arthur POULET | 4e61ef0657 |
18
Makefile
18
Makefile
|
@ -1,24 +1,16 @@
|
|||
NAME=`ls src/*/ -d | cut -f2 -d'/'`
|
||||
NAME=`ls -d src/*/ | cut -f2 -d'/'`
|
||||
|
||||
all: deps_opt build
|
||||
all: deps_opt test
|
||||
|
||||
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
|
||||
shards install
|
||||
deps_update:
|
||||
crystal deps update
|
||||
shards update
|
||||
deps_opt:
|
||||
@[ -d lib/ ] || make deps
|
||||
doc:
|
||||
crystal docs
|
||||
clean:
|
||||
rm $(NAME)
|
||||
|
||||
.PHONY: all run build release test deps deps_update clean doc
|
||||
.PHONY: all run build release test deps deps_update doc
|
||||
|
|
16
README.md
16
README.md
|
@ -2,16 +2,19 @@
|
|||
|
||||
Roll and parse dices
|
||||
|
||||
Works with crystal v0.23.0
|
||||
Works with crystal v1.0.0
|
||||
|
||||
## Installation [![travis](https://travis-ci.org/Nephos/crystal_rollable.svg)](https://travis-ci.org/Nephos/crystal_rollable)
|
||||
## Installation
|
||||
|
||||
[![travis](https://travis-ci.org/Nephos/crystal_rollable.svg)](https://travis-ci.org/Nephos/crystal_rollable)
|
||||
|
||||
Add this to your application's `shard.yml`:
|
||||
|
||||
```yaml
|
||||
dependencies:
|
||||
rollable:
|
||||
github: Nephos/crystal_rollable
|
||||
git: https://git.sceptique.eu/Sceptique/rollable
|
||||
branch: master
|
||||
```
|
||||
|
||||
|
||||
|
@ -20,7 +23,8 @@ dependencies:
|
|||
|
||||
```crystal
|
||||
require "rollable"
|
||||
Rollable::Roll.parse("2d6+4").test # => roll 2 dices and add 4 to the sum
|
||||
Rollable::Roll.parse("2d6+4").test # => Roll 2 dices and add 4 to the sum
|
||||
Rollable::Roll.parse("!1d20 + !1d8").test # => Exploding dices
|
||||
```
|
||||
|
||||
|
||||
|
@ -30,7 +34,7 @@ TODO: Write development instructions here
|
|||
|
||||
## Contributing
|
||||
|
||||
1. Fork it ( https://github.com/Nephos/crystal_rollable/fork )
|
||||
1. Fork it ( https://git.sceptique.eu/Sceptique/rollable/fork )
|
||||
2. Create your feature branch (git checkout -b my-new-feature)
|
||||
3. Commit your changes (git commit -am 'Add some feature')
|
||||
4. Push to the branch (git push origin my-new-feature)
|
||||
|
@ -38,4 +42,4 @@ TODO: Write development instructions here
|
|||
|
||||
## Contributors
|
||||
|
||||
- [Nephos](https://github.com/Nephos) Arthur Poulet - creator, maintainer
|
||||
- [Sceptique](https://git.sceptique.eu/Sceptique) Arthur Poulet - creator, maintainer
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
name: rollable
|
||||
version: 0.1.4
|
||||
version: 1.0.0
|
||||
|
||||
crystal: 1.0.0
|
||||
|
||||
authors:
|
||||
- Arthur Poulet <arthur.poulet@mailoo.org>
|
||||
- Arthur Poulet <arthur.poulet@sceptique.eu>
|
||||
|
||||
license: MIT
|
||||
|
|
|
@ -5,7 +5,7 @@ describe Rollable::Dice do
|
|||
d.min.should eq 2
|
||||
d.max.should eq 40
|
||||
d.average.should eq 21
|
||||
expect_raises { Rollable::Dice.new 1001, 20 }
|
||||
expect_raises(Exception) { Rollable::Dice.new 1001, 20 }
|
||||
end
|
||||
|
||||
it "details" do
|
||||
|
@ -47,9 +47,9 @@ describe Rollable::Dice do
|
|||
end
|
||||
|
||||
it "parse (error)" do
|
||||
expect_raises { Rollable::Dice.parse("yolo") }
|
||||
expect_raises { Rollable::Dice.parse("1d6+1", true) }
|
||||
expect_raises { Rollable::Dice.parse("--1d4") }
|
||||
expect_raises(Exception) { Rollable::Dice.parse("yolo") }
|
||||
expect_raises(Exception) { Rollable::Dice.parse("1d6+1", true) }
|
||||
expect_raises(Exception) { Rollable::Dice.parse("--1d4") }
|
||||
end
|
||||
|
||||
it "consume" do
|
||||
|
|
|
@ -4,7 +4,7 @@ describe Rollable::Die do
|
|||
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) }
|
||||
expect_raises(Exception) { Rollable::Die.new(1001) }
|
||||
end
|
||||
|
||||
it "min, max, average" do
|
||||
|
@ -24,6 +24,7 @@ describe Rollable::Die do
|
|||
min = rand 1..10
|
||||
max = rand min..20
|
||||
((min..max).includes? Rollable::Die.new(min..max).test).should eq(true)
|
||||
((min..max*Rollable::Die::EXPLODING_ITERATIONS).includes? Rollable::Die.new(min..max, true).test).should eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -8,17 +8,31 @@ describe Rollable::Roll do
|
|||
r.min.should eq 5
|
||||
r.max.should eq 24
|
||||
r.average.should eq 14.5
|
||||
10.times do
|
||||
100.times do
|
||||
(5..24).includes?(r.test).should eq true
|
||||
end
|
||||
end
|
||||
|
||||
it "initialize exploding" do
|
||||
r = Rollable::Roll.new [
|
||||
Rollable::Dice.new(1, 6, true),
|
||||
]
|
||||
r.should be_a Rollable::Roll
|
||||
min = 1
|
||||
max = 6*Rollable::Die::EXPLODING_ITERATIONS
|
||||
r.min.should eq min
|
||||
r.max.should eq max
|
||||
100.times do
|
||||
(min..max).includes?(r.test).should eq true
|
||||
end
|
||||
end
|
||||
|
||||
it "test (details)" do
|
||||
r = Rollable::Roll.new [Rollable::Dice.new(2, 6), Rollable::Dice.new(1, 4)]
|
||||
r.min_details.should eq([1, 1, 1])
|
||||
r.max_details.should eq([6, 6, 4])
|
||||
r.average_details.should eq([3.5, 3.5, 2.5])
|
||||
10.times do
|
||||
100.times do
|
||||
t = r.test_details
|
||||
(1..6).includes?(t[0]).should eq true
|
||||
(1..6).includes?(t[1]).should eq true
|
||||
|
@ -44,7 +58,7 @@ describe Rollable::Roll do
|
|||
end
|
||||
|
||||
it "parse (error)" do
|
||||
expect_raises { Rollable::Roll.parse("yolo") }
|
||||
expect_raises(Exception) { Rollable::Roll.parse("yolo") }
|
||||
Rollable::Roll.parse("yolo") { |_| true } rescue fail("must be catch in block")
|
||||
end
|
||||
|
||||
|
|
|
@ -115,8 +115,14 @@ class Rollable::Dice < Rollable::IsRollable
|
|||
end
|
||||
{% end %}
|
||||
|
||||
def <=>(right : Dice)
|
||||
average != right.average ? average - right.average <=> 0 : max != right.max ? max - right.max <=> 0 : min - right.min <=> 0
|
||||
def <=>(right : Dice) : Int32
|
||||
if average != right.average
|
||||
average - right.average > 0 ? 1 : -1
|
||||
elsif max != right.max
|
||||
max - right.max <=> 0
|
||||
else
|
||||
min - right.min <=> 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -87,14 +87,10 @@ class Rollable::Die < Rollable::IsRollable
|
|||
end
|
||||
|
||||
private def explode(&block)
|
||||
i = EXPLODING_ITERATIONS
|
||||
value = 0
|
||||
previous = [] of Int32
|
||||
while i > 0 && value != @faces.end
|
||||
EXPLODING_ITERATIONS.times do |_|
|
||||
value = @faces.to_a.sample
|
||||
previous << value
|
||||
i -= 1
|
||||
yield value, previous
|
||||
yield value
|
||||
break if value != @faces.end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -102,7 +98,7 @@ class Rollable::Die < Rollable::IsRollable
|
|||
def test : Int32
|
||||
if @exploding
|
||||
sum = 0
|
||||
explode { |value, _| sum += value }
|
||||
explode { |value| sum += value }
|
||||
sum
|
||||
else
|
||||
@faces.to_a.sample
|
||||
|
@ -153,8 +149,14 @@ class Rollable::Die < Rollable::IsRollable
|
|||
end
|
||||
{% end %}
|
||||
|
||||
def <=>(right : Die)
|
||||
average != right.average ? average - right.average <=> 0 : max != right.max ? max - right.max <=> 0 : min - right.min <=> 0
|
||||
def <=>(right : Die) : Int32
|
||||
if average != right.average
|
||||
average - right.average > 0 ? 1 : -1
|
||||
elsif max != right.max
|
||||
max - right.max <=> 0
|
||||
else
|
||||
min - right.min <=> 0
|
||||
end
|
||||
end
|
||||
end
|
||||
#
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
abstract class Rollable::IsRollable
|
||||
abstract def min : Int32
|
||||
abstract def max : Int32
|
||||
abstract def average : Int32
|
||||
abstract def average : Float64
|
||||
abstract def test : Int32
|
||||
end
|
||||
|
|
|
@ -80,7 +80,13 @@ class Rollable::Roll < Rollable::IsRollable
|
|||
{% end %}
|
||||
|
||||
def <=>(right : Roll) : Int32
|
||||
average != right.average ? average - right.average <=> 0 : max != right.max ? max - right.max <=> 0 : min - right.min <=> 0
|
||||
if average != right.average
|
||||
average - right.average > 0 ? 1 : -1
|
||||
elsif max != right.max
|
||||
max - right.max <=> 0
|
||||
else
|
||||
min - right.min <=> 0
|
||||
end
|
||||
end
|
||||
|
||||
def order!
|
||||
|
|
|
@ -15,7 +15,7 @@ class Rollable::Roll
|
|||
return list if str.nil?
|
||||
str = str.strip
|
||||
sign = str[0]
|
||||
if sign != '+' && sign != '-' && !list.empty?
|
||||
if sign != '+' && sign != '-' && sign != '!' && !list.empty?
|
||||
raise ParsingError.new("Parsing Error: roll, near to '#{str}'")
|
||||
end
|
||||
str = str[1..-1] if sign == '-' || sign == '+'
|
||||
|
|
Loading…
Reference in New Issue