Alright, so I'm trying to make a text rpg for proof of concept purposes, and I've hit a wall.

My problem, which may be deeper than I hope, lies within class instance referencing. I have a chest class where it fills the chest with a random amount of items. This works so far. Then there is the option to print out the contents of the chest. This works, kind of. It returns exactly what I told it to, unfortunately. Whenever I manually print the contents of the chest, it gives me memory locations for each instantiation. I have *no* idea how to fix this. If you need more of my code, please ask.

class Chest():
    def __init__(self):
        self.contents = []
        self.maxnum = random.randrange(1,6) + 3
        self.currnum = 0
        self.itemtype = 0
        itemindex = 0
        self.fillChest()

    def fillChest(self):
        self.currnum = 0
        while self.currnum < self.maxnum:
            self.itemtype = random.randrange(1,4)
            if self.itemtype == 1:
                    self.contents.append(HealthPotion())
                    self.currnum += 1

            elif self.itemtype == 2:
                    self.contents.append(Armor(True))
                    self.currnum += 1

            elif self.itemtype == 3:
                    self.contents.append(Sword(True))
                    self.currnum += 1

            else:
                print "Error"
                break

    def printChest(self):
        x = self.contents.count(HealthPotion())
        y = self.contents.count(Armor(True))
        z = self.contents.count(Sword(True))
        print "Chest contains:"
        print "="*20
        if x > 0:
            print str(x) + " health potions"
        if y > 0:
            print str(y) + " set(s) of " + Armor.material
        if z > 0:
            print str(z) + Sword.material + " sword(s)"
        print "="*20

^chest code

from playerclass02 import Player
import random



class Item(object):
    def __init__(self):
        self.name = "Null"
        self.weight= 1
        self.value = 10


    def use(self):
        print "Null use action."

class HealthPotion(Item):
    def __init__(self):
        Item.__init__(self)
        self.name = "Health Potion"
        self.weight = 5
        self.value = 15
        self.healamt = 10
        self.description = "Heals 10 points of health"

    def use(self, player):
        if (player.health < player.maxhealth) and (player.health > 0):
            print "%s is using %s. Description of object: %s" % (player.name, self.name, self.description)
            player.health += self.healamt
        elif player.health >= player.maxhealth:
            print "%s is already at max health!" % (player.name)
        else:
            print "%s is dead, and cannot use the %s" % (player.name, self.name)


class Armor(Item):
    def __init__(self, randinst):
        Item.__init__(self)
        matmod = 1
        self.name = "Armor"
        self.weight = 20
        self.value = 50
        self.defense = matmod * 5
        self.material = "$default$"
        self.description = "A sturdy suit of %s armor that protects for %s points of damage" % (self.material, self.defense)
        self.worn = False
        self.randinst = False

        if self.randinst == True:
                randarm()

    def use(self, player):
        if self.worn == False:
            self.worn == True
            player.armorval += self.defense
            print "%s dons a suit of % armor. Description: %s" % (player.name, self.type)
        else:
            self.worn == False
            player.armorval -= self.defense

    def randarm(self):
            random.randint(1,3) == a
            if a == 1:
                self.material = "iron"
                matmod = 2
            elif a == 2:
                self.material = "steel"
                matmod = 4
            elif a == 3:
                self.material = "obsidian"
                matmod = 6

class Sword(Item):
    def __init__(self, randinst):
        Item.__init__(self)
        matmod = 1
        self.material = "$default$"
        self.randinst = False
        self.worn = False
        self.attack = matmod * 5

        if self.randinst == True:
            randsword()

    def randsword(self):

            random.randint(1,3) == a
            if a == 1:
                self.material = "iron"
                matmod = 2
            elif a == 2:
                self.material = "steel"
                matmod = 4
            elif a == 3:
                self.material = "obsidian"
                matmod = 6

^item code

PS. I don't know if the entire thing works, I've only got so far testing it before I ran into my little problem. If you have ANY questions please ask.

Recommended Answers

All 4 Replies

Can you put the code you use for writing the content of the chest (i suppose : chest.printChest()) and the datas that are written ?
BTW, for this kind of thing, you should have a look at __repr__ function.

In last code you are also putting value to matmod which is never used.

Can you put the code you use for writing the content of the chest (i suppose : chest.printChest()) and the datas that are written ?

I'm not quite sure what you mean. chest.fillChest() writes the contents of the chest (i think), and chest.printChest() outputs the contents (i think). Also, thanks for the tip about __repr__, I'll check it out.

In last code you are also putting value to matmod which is never used.

I am unsure of what you mean here, too. My understanding of python is probably not as deep as yours, but I believe that the armor and sword classes randomize the type of material when they're instantiated with the parameter for being random set to true. Please correct me if I'm wrong but I think with how I set it up, it is instantiated as random with Armor/Sword(True).

, it gives me memory locations for each instantiation

That is true as that is what you are storing in the list. First, this statement,
random.randint(1,3) == a
should either yield a True or False (as it is an assert that one side of equation equals the other), or an error if "a" is not declared. I don't think that is what you want. The problem with the code is that there is too much code that has not been tested. Test each function as you go along. So, to print the number of each class type, use something similar to this to get you started on how classes work, but this will only give you an idea of how it is to work as your code requires some cleaning up first.

def printChest(self):
    totals_dict = {"Health Potion":0, "Armor":0, "Sword":0}
    for class_instance in self.contents:
        if class_instance.name in totals_dict:
             totals_dict[class_instance.name] += 1
        else:
            print "Error -->", class_instance.name

    print totals_dict
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.