Danny Yoo wrote:
In the context of the new Master_stats class, it makes more sense for
mfile_read() to return a Master_stats object now rather than a two-tuple
list.
I was able to change the [year, month] to just master and it worked
fine. I was also able to do master = mfile_read() and it worked fine as
well.
To make the code a little safer, explicitely close the file here.
Oh, I forgot to add in the close; I intended to have one put in. I also
noticed I forgot to put a close in for the char_sheet as well, so I
added one in. I assume that it is good programming practise to close
all open files when they are not needed anymore?
Ok, looking at create_char() since it's one of the larger functions.
The value of 's' in create_char() is mixed: at one point, it's an
integer, at other instances, a string. There's a possible flow of
control that doesn't make sense to me...
I wrote the is_yes as follows and it seems to work fine.
###################################################
def is_yes(question):
i = 0
while i == 0:
s = raw_input(question)
if s == 'y' or s == 'yes':
return True
elif s == 'n' or s == 'no':
return False
else:
print '\nplease select y, n, yes, or no\n'
###################################################
I was also able to shrink the code down to:
###################################################
if not is_yes('Is ' + name + ' a sufficient character name?'):
if is_yes('Is ' + alt_name + ' a sufficient character name?'):
name = alt_name
else:
return
###################################################
You will probably notice a new unused class. I am starting to do the
main part of the program and the first thing I need to do is read a very
large text file generated from a web form. What I was thinking about
doing was creating a function that reads the text form and then place
all the values into variables "glued" together in a class. I can then
pass that class back and use the variables when I need them.
--
Your friend,
Scott
Sent to you from a Linux computer using Kubuntu Version 7.04 (Feisty Fawn)
#!/usr/bin/env python
'''This is a program to manage a PBEM EnGarde game using we=custom rules'''
class Character_stats:
"""Character atributes"""
def __init__(self, player, title, name, password, family, sl_inf, mistress_inf, mistress2_inf, appt_inf, appt2_inf, spec_inf, sl, allowance, funds, mid, mistress_name, mistress_sl, club, house, horses, regiment, rank, ma, appointment, strength, con, cur_endurance, rapier, dagger, sabre, cutlass, two_hand):
self.player = player
self.title = title
self.name = name
self.password = password
self.family = family
self.sl_inf = sl_inf
self.mistress_inf = mistress_inf
self.mistress2_inf = mistress2_inf
self.appt_inf = appt_inf
self.appt2_inf = appt2_inf
self.spec_inf = spec_inf
self.sl = sl
self.allowance = allowance
self.funds = funds
self.mid = mid
self.mistress_name = mistress_name
self.mistress_sl = mistress_sl
self.club = club
self.house = house
self.horses = horses
self.regiment = regiment
self.rank = rank
self.ma = ma
self.appointment = appointment
self.strength = strength
self.con = con
self.cur_endurance = cur_endurance
self.rapier = rapier
self.dagger = dagger
self.sabre = sabre
self.cutlass = cutlass
self.two_hand = two_hand
class Master_stats:
"""Master variables used by all characters"""
def __init__(self, year, month):
self.year = year
self.month = month
class Character_temp:
"""Temperary variables used by characters"""
def __init__(self, max_endurance):
self.sp = 0
self.income = 0
self.max_endurance = max_endurance
self.bribe_inf = 0
class turn_sheet:
"""Variables read from the turn sheet"""
def __init__(self, character, password, lending, lend_id, shylock, appt, appt1, appt2, npc_step, npc_step1, npc_step2, join, join_inf1, join_inf2, gen_inf1, gen_inf2, concon, gen_opt1, gen_opt2, gen_opt3, week1, w1toady, w1court_extra, w1bring, w1gamble, w1bets, w1gamble_cut, w1carouse, w1bring_mistress, w1party, w1guest, week2, w2toady, w2court_extra, w2bring, w2gamble, w2bets, w2gamble_cut, w2carouse, w2bring_mistress, w2party, w2guest, week3, w3toady, w3court_extra, w3bring, w3gamble, w3bets, w3gamble_cut, w3carouse, w3bring_mistress, w3party, w3guest, week4, w4toady, w4court_extra, w4bring, w4gamble, w4bets, w4gamble_cut, w4carouse, w4bring_mistress, w4party, w4guest):
self.sp = 0
self.income = 0
self.max_endurance = max_endurance
self.bribe_inf = 0
def is_yes(question):
i = 0
while i == 0:
s = raw_input(question)
if s == 'y' or s == 'yes':
return True
elif s == 'n' or s == 'no':
return False
else:
print '\nplease select y, n, yes, or no\n'
def mfile_read():
'''This will open the master file and load all the variables'''
try:
mfile = open("./Master/mfile", "r")
except:
mfile = open("./Master/mfile", "w")
mfile.close()
print "File could not be opened, so I created one"
return [1631, 8]
master = cPickle.load(mfile)
mfile.close()
return master
def mfile_save(master):
'''This will save the main variables into the master file'''
mfile = open("./Master/mfile", "w")
cPickle.dump(master, mfile)
mfile.close()
return
def month_name(monthNumeral):
"""Returns the month name as a string"""
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
return months[monthNumeral - 1]
def char_sheet(character, master, temp):
"""Create the text based character sheet"""
path = "./character_sheets/" + character.name
path.rstrip()
cfile = open(path, "w")
cfile.write("Engarde! Death with Honour\n--------------------------\n")
cfile.write(character.player)
cfile.write("\n")
if character.title:
s = character.title + " " + character.name
else:
s = character.name
cfile.write(s)
cfile.write("\nPassword: ")
cfile.write(character.password)
s = "\n" + month_name(master.month) + " " + str(master.year)
cfile.write(s)
s = "\n\nSL Influence: " + str(character.sl_inf)
cfile.write(s)
if character.mistress2_inf > 0:
s = "\nMistress Influence: " + str(character.mistress_inf) + "+" + str(character.mistress2_inf)
elif character.mistress_inf > 0:
s = "\nMistress Influence: " + str(character.mistress_inf)
if character.appt2_inf > 0:
s = "\nAppointment Influence: " + str(character.appt_inf) + "+" + str(character.appt2_inf)
elif character.appt_inf > 0:
s = "\nAppointment Influence: " + str(character.appt_inf)
if temp.bribe_inf > 0:
s = "\nBribery Influence: " + str(temp.bribe_inf)
if character.spec_inf > 0:
s = "\nSpecial Influence: " + str(character.spec_inf)
s = "\nSocial Points: " + str(temp.sp)
cfile.write(s)
s = "\nSocial Level: " + str(character.sl)
cfile.write(s)
s = "\n\nAllowance/Income: " + str(temp.income)
cfile.write(s)
s = "\nMonth End Funds: " + str(character.funds)
cfile.write(s)
s = "\n\nMistress Name: " + character.mistress_name
cfile.write(s)
s = "\nMistress SL: " + str(character.mistress_sl)
cfile.write(s)
s = "\n\nClub: " + character.club
cfile.write(s)
s = "\nHouse: " + character.house
cfile.write(s)
s = "\nHorses: " + str(character.horses)
cfile.write(s)
s = "\n\nRegiment: " + character.regiment
cfile.write(s)
s = "\nRank: " + character.rank
cfile.write(s)
s = "\nMilitary Ability: " + str(character.ma)
cfile.write(s)
s = "\nMID: " + str(character.mid)
cfile.write(s)
s = "\nAppointment: " + character.appointment
cfile.write(s)
s = "\n\nStrength: " + str(character.strength)
cfile.write(s)
s = "\nConstitution: " + str(character.con)
cfile.write(s)
s = "\nMax Endurance: " + str(temp.max_endurance)
cfile.write(s)
s = "\nCurrent Endurance: " + str(character.cur_endurance)
cfile.write(s)
s = "\nRapier: " + str(character.rapier)
cfile.write(s)
s = "\nDagger: " + str(character.dagger)
cfile.write(s)
s = "\nSabre: " + str(character.sabre)
cfile.write(s)
s = "\nCutlass: " + str(character.cutlass)
cfile.write(s)
s = "\nTwo-Handed Sword: " + str(character.two_hand)
cfile.write(s)
cfile.close()
return
def char_save(character):
'''This saves the character to the harddrive'''
path = "./characters/" + character.player
cfile = open(path, "w")
cPickle.dump(character, cfile)
cfile.close()
return
def add_player_list(player):
'''This will add the players name to the list of players'''
plist = open("./Master/player_list", "a")
plist.write(player)
plist.write("\n")
plist.close()
return
def create_char():
"""This function creates a new character from a supplied text file"""
# ------------------------------- Read character creation sheet
create_form = open('create', 'r')
create_form.seek(7, 0)
player = create_form.readline()
player = player.rstrip()
create_form.seek(12, 1)
name = create_form.readline()
name = name.rstrip()
create_form.seek(16, 1)
alt_name = create_form.readline()
alt_name = alt_name.rstrip()
create_form.seek(11, 1)
password = create_form.readline()
password = password.strip()
password = password.lower()
create_form.seek(12, 1)
strength1 = create_form.readline()
strength1 = int(strength1)
create_form.seek(12, 1)
strength2 = create_form.readline()
strength2 = int(strength2)
create_form.seek(11, 1)
weakness = create_form.readline()
weakness = int(weakness)
if not is_yes('Is ' + name + ' a sufficient character name?'):
if is_yes('Is ' + alt_name + ' a sufficient character name?'):
name = alt_name
else:
return
# -------------------------- Do Calculations
if strength1 == weakness: # Check for generalized character
strength1 = 0
weakness = 0
if strength2 == weakness:
strength2 = 0
weakness = 0
if strength1 == 2 and strength2 == 2 and weakness == 3:
print ("Can not take funds as a strength and SL as a weakness")
return
family = "Gentleman"
influence = 0 # Give default, normal, values
sl = 5
allowance = 100
funds = 500
ma = 3
strength = 10
con = 10
rapier = 10
dagger = 10
sabre = 10
cutlass = 10
two_hand = 10
if strength1 == 2: # Strength 1
funds = 550
allowance = 110
elif strength1 == 3:
sl = 8
influence = 1
family = "Noble"
elif strength1 == 4:
strength = 13
con = 13
rapier = 13
dagger = 13
sabre = 13
cutlass = 13
two_hand = 13
elif strength1 == 5:
ma = 5
if strength2 == 2: # Strength 2
if strength1 == 2:
funds = 750
allowance = 125
else:
funds = 550
allowance = 110
elif strength2 == 3:
family = "Noble"
if strength1 == 3:
sl = 10
influence = 2
else:
sl = 8
influence = 1
elif strength2 == 4:
if strength1 == 4:
strength = 15
con = 15
rapier = 15
dagger = 15
sabre = 15
cutlass = 15
two_hand = 15
else:
strength = 13
con = 13
rapier = 13
dagger = 13
sabre = 13
cutlass = 13
two_hand = 13
elif strength2 == 5:
if strength1 == 5:
ma = 6
else:
ma = 5
if weakness == 2: #Weakness
funds = 250
allowance = 50
elif weakness == 3:
family = "Commoner"
sl = 1
elif weakness == 4:
strength = 8
con = 9
rapier = 4
dagger = 4
sabre = 4
cutlass = 4
two_hand = 4
elif weakness == 5:
ma = 1
# --------------------------- Assign values to classes
character = Character_stats(player, None, name, password, family, influence, 0, 0, 0, 0, 0, sl, allowance, funds, 0, "None", 0, "None", "None", 0, "None", "None", ma, "None", strength, con, strength * con, rapier, dagger, sabre, cutlass, two_hand)
master = mfile_read() # Load the master file
temp = Character_temp(strength * con)
temp.income = allowance
# --------------------------- Save files
char_sheet(character, master, temp) # Create the character sheet
mfile_save(master) # Save the master file
char_save(character) # Save the character
add_player_list(player) # Add player to list of players
def delete_char():
"""This function will delete a selected character"""
pass
def premonthly(player):
"""Premonthly action"""
import cPickle
ans = 0
while ans != 3:
print 'Engarde Game Master v0.1.0'
print
print '1: Add New Character'
print '2: Delete Character'
print '3: Conduct a Turn'
print '4: Quit'
print
ans = raw_input('Please select from 1-4: ')
if ans == '1':
create_char()
elif ans == '2':
delete_char()
elif ans == '3':
break
elif ans == '4':
exit()
else:
print "\nYou did not select 1, 2, 3, or 4, you selected ", ans, "\n"
plist = open("./Master/player_list", "r")
for l in range(0,5):
print l
plist.seek(0)
for x in plist:
print x
if l == 0:
premonthly(x)
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor