patternpythonMinor
Character generator
Viewed 0 times
charactergeneratorstackoverflow
Problem
I'm a total newbie teaching myself via Zed Shaw's Learn Python The Hard Way and I've gotten bored over a week long memorization lesson, so I thought I would make a character generator for a pen and paper RPG I'm writing. I havent really read about
```
##########################################################
# Cyberpanky N.O.W Python Character Generator by Ray Weiss
#
# Created 9/24/2012
#
# Much thanks to Connor Daliposon
# Mho made a very readable D&D Character Generator
# That a Python newbie like me could understand
##########################################################
# Imports
import random
from pprint import pprint
# Hey there
print """
Hello world & welcome to the CYBERPANKY N.O.W. character generator.
Programmed by Ray Weiss.
To quit at anytime, press CNTRL-C.
"""
print "Lets figure out your stats first. Press enter to continue."
raw_input()
# Stat Roller
# Rolls 3D6 and adds the sum together and prints
def roll_stats():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
d = random.randint(1, 6)
list = [a, b, c, d]
list.sort()
add = sum(list[1:4])
return add
# Modifiers
def pow_mod():
a = "|+1 to hit on mele attack rolls| "
b = "|+1 damage on mele attack rolls|"
if pow >= 15 and pow = 17:
return a + b
else:
return " ~no modifiers~"
def iq_mod():
a = "|-500$ to invest in cyberspace| "
b = "|Reroll Street Doc abilities|"
if iq >= 15 and iq = 17:
return a + b
else:
return " ~no modifiers~"
def agi_mod():
a = "|+1 to hit with ranged weapons| "
b = "|-1 to hit with ranged weapons|"
if agi >= 12:
return a
if
if statements or things like that, so I am experimenting here. ```
##########################################################
# Cyberpanky N.O.W Python Character Generator by Ray Weiss
#
# Created 9/24/2012
#
# Much thanks to Connor Daliposon
# Mho made a very readable D&D Character Generator
# That a Python newbie like me could understand
##########################################################
# Imports
import random
from pprint import pprint
# Hey there
print """
Hello world & welcome to the CYBERPANKY N.O.W. character generator.
Programmed by Ray Weiss.
To quit at anytime, press CNTRL-C.
"""
print "Lets figure out your stats first. Press enter to continue."
raw_input()
# Stat Roller
# Rolls 3D6 and adds the sum together and prints
def roll_stats():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
d = random.randint(1, 6)
list = [a, b, c, d]
list.sort()
add = sum(list[1:4])
return add
# Modifiers
def pow_mod():
a = "|+1 to hit on mele attack rolls| "
b = "|+1 damage on mele attack rolls|"
if pow >= 15 and pow = 17:
return a + b
else:
return " ~no modifiers~"
def iq_mod():
a = "|-500$ to invest in cyberspace| "
b = "|Reroll Street Doc abilities|"
if iq >= 15 and iq = 17:
return a + b
else:
return " ~no modifiers~"
def agi_mod():
a = "|+1 to hit with ranged weapons| "
b = "|-1 to hit with ranged weapons|"
if agi >= 12:
return a
if
Solution
def roll_stats():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
d = random.randint(1, 6)
list = [a, b, c, d]Call it
dice or something more descriptive then list. You can also use list = [random.randint(1,6) for x in xrange(4)] rather then creating the variables separately.list.sort()
add = sum(list[1:4])Instead of sorting it and slicing it use:
list.remove(min(list))return addDon't assign variables just to return them on the next line, use
return sum(list[1:4])def pow_mod():
a = "|+1 to hit on mele attack rolls| "
b = "|+1 damage on mele attack rolls|"
if pow >= 15 and pow < 17:Don't take input to a function from global variables, pass it as a parameter
return a
if pow >= 17:
return a + b
else:
return " ~no modifiers~"You return strings, but conceptually you are returning a list of modifiers. I suggest returning the list. You can then format the list anyway you like in the caller.
You've got a number of functions very similiar to this, it suggests moving some of the details into data structures. I'd do it like this:
POW_MODIFIERS = [
# specify minimum and maximum stat the modifier applies to
(15, 9999, "+1 to hit on mele attack rolls"),
(17, 9999, "+1 damage on mele attack rolls")
]
def calc_modifiers(stat, modifiers):
return [modifier for minimum, maximum, modifier in modifiers if minimum <= state <= maximum]
def format_modifiers(modifiers):
if modifiers:
return ' '.join('|%s|' % modifier for modifier in modifiers)
else:
return ' ~no modifiers~ 'By creating lists like POW_MODIFIERS for all your stats, you can reuse the functions for all the different stats. You should be able to apply similiar techniques to your other decisions. I'd also look at storing the data in a seperate file, perhaps using JSON.
Code Snippets
def roll_stats():
a = random.randint(1, 6)
b = random.randint(1, 6)
c = random.randint(1, 6)
d = random.randint(1, 6)
list = [a, b, c, d]list.sort()
add = sum(list[1:4])def pow_mod():
a = "|+1 to hit on mele attack rolls| "
b = "|+1 damage on mele attack rolls|"
if pow >= 15 and pow < 17:return a
if pow >= 17:
return a + b
else:
return " ~no modifiers~"POW_MODIFIERS = [
# specify minimum and maximum stat the modifier applies to
(15, 9999, "+1 to hit on mele attack rolls"),
(17, 9999, "+1 damage on mele attack rolls")
]
def calc_modifiers(stat, modifiers):
return [modifier for minimum, maximum, modifier in modifiers if minimum <= state <= maximum]
def format_modifiers(modifiers):
if modifiers:
return ' '.join('|%s|' % modifier for modifier in modifiers)
else:
return ' ~no modifiers~ 'Context
StackExchange Code Review Q#15913, answer score: 6
Revisions (0)
No revisions yet.