Created
November 25, 2017 00:46
-
-
Save 4Kaylum/d635d902ffca9aea4c37f1537c46d349 to your computer and use it in GitHub Desktop.
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
from sys import argv | |
from random import randint | |
def dice_compiler(string:str) -> tuple: | |
''' | |
Compiles a string (XdN+C) into tuple (X, N, +C). Works when X is not present, or when C is not present | |
(by inserting 1 and +0 respectively) | |
Does not give the actual roll of the dice | |
Works only with strings that match the regex "^\d*d\d+([+-]\d+)?$" | |
Parameters: | |
string:str | |
The dice to be compiled into a tuple | |
Returns: | |
tuple[int, int, int] | |
A tuple of values dice counter, dice type, and addition modifier | |
To be passed into some kind of roll calculator | |
''' | |
# Conver to lowercase because it shoulw all be numbers anyway apart from the 'd' | |
string = string.lower() | |
# Get the +C from the end | |
if '+' in string: | |
split_modifier = string.split('+') | |
split_modifier[-1] = int(split_modifier[-1]) | |
elif '-' in string: | |
split_modifier = string.split('-') | |
split_modifier[-1] = - int(split_modifier[-1]) | |
else: | |
# There is no +C, let's add one | |
split_modifier = [string, 0] | |
# Get the XdN and modifier into seperate strings for later | |
dice_roll, modifier = split_modifier | |
# Get the dice type and how many | |
dice_split = dice_roll.split('d') | |
try: | |
dice_count = int(dice_split[0]) | |
except ValueError: | |
dice_count = 1 | |
dice_type = int(dice_split[1]) | |
# Return out to the user | |
return dice_count, dice_type, modifier | |
def dice_roller(dice_list:tuple) -> tuple: | |
''' | |
Rolls a tuple of dice, as compiled from dice_compiler | |
Parameters: | |
dice_list:tuple | |
The list to be rolled as (amount, type, modifier) | |
Returns: | |
tuple[list[int], int] | |
The first item being the rolls that were achieved, and the second being the sum | |
with the modifier added to it | |
''' | |
counter, dice_type, modifier = dice_list | |
output_list = [] | |
while counter > 0: | |
output_list.append(randint(1, dice_type)) | |
counter -= 1 | |
return output_list, sum(output_list) + modifier | |
if __name__ == '__main__': | |
if len(argv) == 1: | |
print('What dice do you want to roll?') | |
while True: | |
x = input(' :: ') | |
y = dice_compiler(x) | |
z = dice_roller(y) | |
if len(z[0]) == 1: | |
print('The dice roll {0} resulted in {1[0][0]}, with a (modified) sum of {1[1]}'.format(x, z)) | |
else: | |
print('The dice roll {0} resulted in {1} and {2}, with a (modified) sum of {3[1]}'.format( | |
x, | |
', '.join([str(i) for i in z[0][:-1]]), | |
z[0][-1], z) | |
) | |
else: | |
for x in argv[1:]: | |
y = dice_compiler(x) | |
z = dice_roller(y) | |
if len(z[0]) == 1: | |
print('The dice roll {0} resulted in {1[0][0]}, with a (modified) sum of {1[1]}'.format(x, z)) | |
else: | |
print('The dice roll {0} resulted in {1} and {2}, with a (modified) sum of {3[1]}'.format( | |
x, | |
', '.join([str(i) for i in z[0][:-1]]), | |
z[0][-1], z) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment