-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename to Alea, fully split parser from evaluator.
- Loading branch information
1 parent
07943a0
commit f3522d5
Showing
50 changed files
with
651 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import Dice | ||
import Alea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Alea do | ||
def expression(string) when is_binary(string) do | ||
string |> Alea.Expression.new() | ||
end | ||
|
||
def roll(%Alea.Expression{} = expression) do | ||
expression |> Alea.Expression.Evaluate.evaluate() | ||
end | ||
|
||
def roll(string) when is_binary(string) do | ||
string |> expression |> roll | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Alea.Dice.Pool do | ||
defstruct dice: [], modifiers: [], rolls: [] | ||
|
||
def roll_all(%Alea.Dice.Pool{} = pool) do | ||
%Alea.Dice.Pool{ | ||
pool | ||
| dice: [], | ||
rolls: (pool.rolls ++ pool.dice) |> Enum.map(&Alea.Die.Roll.roll/1) | ||
} | ||
end | ||
|
||
def evaluate(%Alea.Dice.Pool{modifiers: [modifier | modifiers]} = pool) do | ||
pool = %Alea.Dice.Pool{pool | modifiers: modifiers} | ||
Alea.Dice.Pool.Modifier.apply(modifier, pool) | ||
end | ||
|
||
def evaluate(%Alea.Dice.Pool{modifiers: []} = pool) do | ||
Alea.Dice.Pool.roll_all(pool) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defprotocol Alea.Dice.Pool.Modifier do | ||
def apply(modifier, pool) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Alea.Dice.Pool.Modifier.Drop do | ||
defstruct [:number, mode: :random] | ||
|
||
def new(number, mode) do | ||
%__MODULE__{ | ||
number: number, | ||
mode: mode | ||
} | ||
end | ||
|
||
def rolls(%__MODULE__{} = drop, rolls) do | ||
case drop.mode do | ||
:random -> Enum.take_random(rolls, length(rolls) - drop.number) | ||
:low -> Enum.sort_by(rolls, & &1.result) |> Enum.drop(drop.number) | ||
:high -> Enum.sort_by(rolls, & &1.result) |> Enum.drop(-drop.number) | ||
end | ||
end | ||
|
||
defimpl Alea.Dice.Pool.Modifier do | ||
def apply(%Alea.Dice.Pool.Modifier.Drop{} = drop, pool) do | ||
pool = Alea.Dice.Pool.roll_all(pool) | ||
%Alea.Dice.Pool{pool | rolls: Alea.Dice.Pool.Modifier.Drop.rolls(drop, pool.rolls)} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Alea.Dice.Pool.Modifier.Keep do | ||
defstruct [:number, mode: :random] | ||
|
||
def new(number, mode) do | ||
%__MODULE__{ | ||
number: number, | ||
mode: mode | ||
} | ||
end | ||
|
||
def rolls(%__MODULE__{} = keep, rolls) do | ||
case keep.mode do | ||
:random -> Enum.take_random(rolls, keep.number) | ||
:low -> Enum.sort_by(rolls, & &1.result) |> Enum.take(keep.number) | ||
:high -> Enum.sort_by(rolls, & &1.result) |> Enum.take(-keep.number) | ||
end | ||
end | ||
|
||
defimpl Alea.Dice.Pool.Modifier do | ||
def apply(%Alea.Dice.Pool.Modifier.Keep{} = keep, pool) do | ||
pool = Alea.Dice.Pool.roll_all(pool) | ||
%Alea.Dice.Pool{pool | rolls: Alea.Dice.Pool.Modifier.Keep.rolls(keep, pool.rolls)} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defprotocol Alea.Die.Roll do | ||
defstruct [:die, :result] | ||
|
||
def roll(die) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defprotocol Alea.Die.Sides do | ||
def random(faces) | ||
def min(faces) | ||
def max(faces) | ||
def count(faces) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Alea.Die.Sides.Faces do | ||
defstruct [:number] | ||
|
||
def new(number) when is_integer(number) and number > 0 do | ||
%__MODULE__{number: number} | ||
end | ||
|
||
defimpl Alea.Die.Sides do | ||
def random(%Alea.Die.Sides.Faces{} = sides) do | ||
:rand.uniform(sides.number) | ||
end | ||
|
||
def min(%Alea.Die.Sides.Faces{} = _sides) do | ||
1 | ||
end | ||
|
||
def max(%Alea.Die.Sides.Faces{} = sides) do | ||
sides.number | ||
end | ||
|
||
def count(%Alea.Die.Sides.Faces{} = sides) do | ||
sides.number | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Alea.Die.Sides.List do | ||
defstruct [:options] | ||
|
||
def new(options) when is_list(options) do | ||
%__MODULE__{options: Enum.sort(options)} | ||
end | ||
|
||
defimpl Alea.Die.Sides do | ||
def random(%Alea.Die.Sides.List{} = list) do | ||
list.options |> Enum.random() | ||
end | ||
|
||
def min(%Alea.Die.Sides.List{} = list) do | ||
list.options |> List.first() | ||
end | ||
|
||
def max(%Alea.Die.Sides.List{} = list) do | ||
list.options |> List.last() | ||
end | ||
|
||
def count(%Alea.Die.Sides.List{} = list) do | ||
list.options |> length | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Alea.Die.Sides.Range do | ||
defstruct [:first, :last] | ||
|
||
def new(first, last) when is_integer(first) and is_integer(last) do | ||
if first < last do | ||
%__MODULE__{first: first, last: last} | ||
else | ||
%__MODULE__{first: last, last: first} | ||
end | ||
end | ||
|
||
defimpl Alea.Die.Sides do | ||
def random(%Alea.Die.Sides.Range{} = range) do | ||
Range.new(range.first, range.last) |> Enum.random() | ||
end | ||
|
||
def min(%Alea.Die.Sides.Range{} = range) do | ||
range.first | ||
end | ||
|
||
def max(%Alea.Die.Sides.Range{} = range) do | ||
range.last | ||
end | ||
|
||
def count(%Alea.Die.Sides.Range{} = range) do | ||
range.first - range.last + 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Alea.Die.Types.Normal do | ||
defstruct [:sides] | ||
|
||
defimpl Alea.Die.Roll do | ||
def roll(%Alea.Die.Types.Normal{} = die) do | ||
%Alea.Die.Roll{ | ||
die: die, | ||
result: Alea.Die.Sides.random(die.sides) | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.