xylafur / dnd-generator

Tool to help us in our campaign
1 stars 0 forks source link

One function for die roll instead of separate #4

Closed TetroVolt closed 6 years ago

TetroVolt commented 6 years ago

Do we really need separate function calls for die roll and exceptions. Doesnt the random.randint module already bound the results?

ghost commented 6 years ago

Sure, but the point of the different ones was to incorporate some of the extra logic. Plus, in the future, we can implement passing in modifiers for hit dice, and weapon damage. For instance, let's say you wanted to roll 3d6+4. You could call random.randint three times and add 4, or just call roll_d6(weapon['damage']) once.

Future proofing basically.

ghost commented 6 years ago

And, the d20 method can do with advantage of disadvantage. That's useful right there.

TetroVolt commented 6 years ago

What I meant is that all of that can be achieved with this

from random import randint as r¬                                                 
+   1 ¬                                                                                
+   2 # roll functions¬                                                                
+   3 roll_d      = lambda n:r(1,n)   # roll a dice : [1,n]¬                           
+   4 roll_d0     = lambda n:r(0,n-1) # roll a dice : [0,n-1]¬                         
+   5 roll_d_adv  = lambda n:max(roll_d(n), roll_d(n))   # roll a dice with adv¬       
+   6 roll_d0_adv = lambda n:max(roll_d0(n), roll_d0(n)) # roll a 0dice with adv¬      
+   7 roll_d_dis  = lambda n:min(roll_d(n), roll_d(n)) # roll a dice with dis¬         
+   8 roll_d0_dis = lambda n:min(roll_d(n), roll_d(n)) # roll a 0dice with dis¬        
+   9 ¬                                                                                
+  10 def roll_die(die, count=1, adv=False, dis=False, calc_sum=True):¬                
+  11     assert(die >= 4) # if we are doing only d4 and above¬                        
+  12     assert(count >= 1) #·¬                                                       
+  13 ¬                                                                                
+  14     if adv != dis:¬                                                              
+  15         if adv: # advantage¬                                                     
+  16             ret = [roll_d_adv(die) for i in range(count)]¬                       
+  17         else:   # dis¬                                                           
+  18             ret = [roll_d_dis(die) for i in range(count)]¬                       
+  19 ¬                                                                                
+  20     else: # advantage and disadvantage cancel out in 5e¬                         
+  21         ret = [ roll_d(die) for i in range(count) ]¬                             
+  22 ¬                                                                                
+  23     return sum(ret) if calc_sum else ret¬
TetroVolt commented 6 years ago

Like, I know dnd is designed such that every reason to choose a random number is achievable with the standard seven but from a generator standpoint, if we were to want to pick from a non standard die number, this function would generalize better.

ghost commented 6 years ago

Hmmmm. I do like this implementation. We may need to rework it a bit if we decided to add modifiers and such later on, but it IS much cleaner for now. I say go for it unless @xylafur has an opinion.

ghost commented 6 years ago

Closing and taking further comments to the opened pull request.