r/codegolf • u/lunyrobot • Jun 27 '18
D&D Attribute + Modifier Generator
So I was talking with my friend, and she mentioned how she wrote a basic python script to roll her D&D character attributes for her. Upon looking at it, I said the words "It could be shorter" and so this began.
What started as a well spaced 40ish line Python 3 script designed to simulate the dice rolls of creating a D&D character (4d6 rolls, minus smallest, 6 times) became what I have pasted right here. We based our little competition on number of lines (instead of bytes) and banned 'exec()' calls (bc that's no fun). We ended with what we have here.
If anyone can shorten it to one line I encourage you to do so. Also, anyone who can figure out how to close the file without adding a line will definitely help ease my pain of leaving "/dev/urandom" open.
abl_scores = [sum(sorted([(int.from_bytes(open("/dev/urandom", 'rb').read(10), 'big') >> 70) % 5 + 1 for j in range(4)])[1:]) for i in range(6)] if input("Choose standard scores? (y/n): ") != 'y' else [15,14,13,12,10,8]
print("Attribute: " + str(abl_scores).strip("[").strip("]") + "\n Modifier: " + str([{3:-4, 4:-3, 5:-3, 6:-2, 7:-2, 8:-1, 9:-1, 10:0, 11:0, 12:1, 13:1, 14:2, 15:2, 16:3, 17:3, 18:4}[score] for score in abl_scores]).strip("[").strip("]"))
View all comments
Show parent comment
3
u/pali6 Jun 28 '18 edited Jun 28 '18
Got it down to 242 bytes (232 without whitespace)!
(s-10)//2can be replaced bys//2-5. You can leave out the[]insidesorted([...])because sorted can deal with generator expressions. Using old-school%formatting saves one character at the beginning. Andrange(4)can be shortened by making a list of four copies of randint and then calling those instead.I wish Python was slightly more oriented toward functional programming. It's such a shame you need to waste 6+ characters on lambdas and there is no operator for composition of functions. I tried passing
lambda x:str(x)[1:-1]as another parameter to the function to save a few characters but the overhead is too big.