Danny Yoo wrote:
I will work at your suggestions and will get back to you if I have any problems.

Good!  Keep the folks on Python-Tutor up to date with your progress.
Here is the code changes I made based on your suggestions:

I put all the values together into a class

#######################################
class Character_stats:
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
#############################

There where some values I did not want saved to the character file. A couple where values that are for all characters, so I put them into their own class.

###############################
class Master_stats:
    def __init__(self, year, month):
        self.year = year
        self.month = month
###############################
There are only two values now, but that will most likely increase as I get further into the program.


Lastly, I put the temporary values that I did not want saved on the sheet into their own class.

#################################
class Character_temp:
    def __init__(self, max_endurance):
        self.sp = 0
        self.income = 0
        self.max_endurance = max_endurance
        self.bribe_inf = 0
##################################

As you mentioned, my character save file is only a few lines long now :)
##################################
def char_save(character):
    '''This saves the character to the harddrive'''
    path = "./characters/" + character.player
    cfile = open(path, "w")
    cPickle.dump(character, cfile)
    return
##################################

With the months, I used both your suggestions. I used the list and then put it into a function.

###################################
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]
###################################

and I was able to call then function nicely by using:

####################################
s = "\n" + month_name(master.month) + " " + str(master.year)
cfile.write(s)

--
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:
    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:
    def __init__(self, year, month):
        self.year = year
        self.month = month
    

class Character_temp:
    def __init__(self, max_endurance):
        self.sp = 0
        self.income = 0
        self.max_endurance = max_endurance
        self.bribe_inf = 0

def mfile_read():
    '''This will open the master file and load all the variables'''
    i = 0
    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.year, master.month]

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)

def char_save(character):
    '''This saves the character to the harddrive'''
    path = "./characters/" + character.player
    cfile = open(path, "w")
    cPickle.dump(character, cfile)
    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)
    s = 0

    while s < 1 or s > 3:
        print 'Is', player, 'the player you wanted?'
        s = raw_input()
        if s == 'y' or s == 'yes':
            break
        elif s == 'n' or s == 'no':
            return
        else:
            print '\nplease select y, n, yes, or no\n'
            
    while s < 1 or s > 3:
        print '\nIs', name, 'a sufficient character name?'
        s = raw_input()
        if s == "y" or s == "yes":
            break
        elif s == "n" or s == "no":
            
            while s < 1 or s > 3:
                print '\nIs the alternate name', alt_name,
                print 'a sufficient character name?'
                s = raw_input()
                if s == "y" or s == "yes":
                    name = alt_name
                    break
                elif s == "n" or s == "no":
                    return
                else:
                    print '\nplease select y, n, yes, or no\n'
            
            break
            
        else:
            print '\nplease select y, n, yes, or no\n'
            
    # -------------------------- 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)
    
    mfile = mfile_read()       # Load the master file
    year = mfile[0]
    month = mfile[1]
    
    master = Master_stats(year, month)
    
    temp = Character_temp(strength * con)
    temp.income = allowance
    
    # --------------------------- Save files
    char_sheet(character, master, temp)
    
    mfile_save(master)   # Save the master file
    
    char_save(character)
    
    add_player_list(player)

def delete_char():
    """This function will delete a selected character"""
    pass

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(1,6):
    print l
    
    
    plist.seek(0)
    for x in plist:
        print x
    




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to