Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active August 29, 2015 14:08
Show Gist options
  • Save Heimdell/a8a0ebc500856edd6c2c to your computer and use it in GitHub Desktop.
Save Heimdell/a8a0ebc500856edd6c2c to your computer and use it in GitHub Desktop.
:- op(400, xfx, d). % so we write "1 d 6"
throw(Count d Dice, Throw) :-
count(Randoms, Count), % alloc list Randoms of length Count
each(random, Randoms), % fill with randoms
each(randomToThrow(Dice), Randoms, Throws),
sum(Throws, Throw).
randomToThrow(Dice, Rand, Throw) :-
Throw = round(Dice * Rand + 0.5).
add(X, Y, Z) :- Z is X + Y.
sum(Xs, X) :- foldl(add, Xs, 0, X).
%----------------------------------------------------------------------------%
throw_attribute(X) :-
count(Throws, 4),
each(throw(1 d 6), Throws),
mySort(Throws, [_ | Ascend]),
foldl(add, Ascend, 0, X).
mySort -->
predsort(strictLtOrGt).
% standart sort sorts [6,6,6,6] to [6], which is wrong
%
strictLtOrGt(S, L, R) :-
compare(X, L, R),
( X = (=) -> S = (<) ; S = X).
%----------------------------------------------------------------------------%
saving_throw(Difficulty, Bonuses) :-
Delta is Difficulty - Bonuses,
saving_throw(Delta).
saving_throw(Difficulty) :-
throw(1 d 20, X),
( X = 20 ->
writeln([authomatic, save])
; X = 1 ->
writeln([authomatic, failure]),
!,
fail
; X >= Difficulty
).
% rename
count --> length.
each --> maplist.
each(X) --> maplist(X).
foldl1(_, [], Accum, Accum).
foldl1(Op, [H | T], Accum, Result) :-
call(Op, H, Accum, Tmp),!,
foldl1(Op, T, Tmp, Result).
foldl2(_, []) --> !.
foldl2(Op, [H | T]) -->
call(Op, H),!,
foldl2(Op, T).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment