BobJohn
BobJohn

Reputation: 65

Random values in an instance of a class, that are not saved

I am new to python and want to write a simple text adventure game. The player enters a tavern and interacts with the guests. The game takes place in a fantasy setting, where there are multiple races. I want to randomly generate each guest and then interact with them in the tavern. Here is my simplified code:

import random
class guest:
    def __init__(self,race,name,mood):
        self.race = race
        self.name = name
        self.mood = mood

def humannames():
    human_names_choice = ('Mark','Joe','Bill')
    return random.choice(human_names_choice)

def orcnames():
    orc_names_choice = ('Ug','Orok','Ushnar')
    return random.choice(orc_names_choice)

def humanmoods():
    human_moods_choice = ('entertained','disgusted','pleased')
    return random.choice(human_moods_choice)

def orcmoods():
    orc_moods_choice = ('drunk','pissed off','angry')
    return random.choice(orc_moods_choice)

guest_human = guest('human',humannames(),humanmoods())
guest_orc = guest('orc',orcnames(),orcmoods())

allguests = [guest_human,guest_orc]
guest1 = random.choice(allguests)

print('You meet a ' + guest1.race + ' named ' + guest1.name + '. He seems ' + guest1.mood) 

So far so good. With this system i can add many more races,names etc. later. My problem is the following:

It is logical, that guest1.name and guest.race never change. If i print the last statement 5 times in a row, i will get the same results, because the values are set at the start. This is good for me, because those things should never change. With guest1.mood however it is more complicated. A tavern guest doesnt always have the same mood, it changes over time.

Question: How can i keep guest1.name and guest.race static but get a new random mood everytime the player meets the same guest again later?

Thanks in advance!

Upvotes: 1

Views: 49

Answers (1)

Jack Taylor
Jack Taylor

Reputation: 6217

You can add a change_mood method to your guest class. This means that you can change the mood whenever you want.

class guest:
    def __init__(self,race,name,mood):
        self.race = race
        self.name = name
        self.mood = mood

    def change_mood(self):
        self.mood = random.choice(('entertained','disgusted','pleased'))

Then you can change the mood like this.

print('You meet a ' + guest1.race + ' named ' + guest1.name + '. He seems ' + guest1.mood) 
guest1.change_mood()
print('You meet a ' + guest1.race + ' named ' + guest1.name + '. He seems ' + guest1.mood)

This will always change the mood to a human mood, which is not ideal for orcs. To make this easier for different races, you could make two subclasses of guest: one for humans, and one for orcs. I would organise it like this:

class Guest:
    def __init__(self):
        self.name = random.choice(self.names)
        self.mood = random.choice(self.moods)

    def change_mood(self):
        self.mood = random.choice(self.moods)

class Human(Guest):
    race = 'human'
    names = ('Mark', 'Joe', 'Bill')
    moods = ('entertained', 'disgusted', 'pleased')

class Orc(Guest):
    race = 'orc'
    names = ('Ug', 'Orok', 'Ushnar')
    moods = ('drunk',' pissed off', 'angry')

You can then use the classes like this:

guest1 = Orc()
print('You meet a ' + guest1.race + ' named ' + guest1.name + '. He seems ' + guest1.mood)

Edit: another way would be to store all of your race data in one big data structure, and then specify the race as an argument to the Guest class.

class Guest:
    races = {
        'human': {
            'names': ('Mark', 'Joe', 'Bill'),
            'moods': ('entertained', 'disgusted', 'pleased'),
        },
        'orc': {
            'names': ('Ug', 'Orok', 'Ushnar'),
            'moods': ('drunk',' pissed off', 'angry'),
        },
    }

    def __init__(self, race):
        self.race = race
        self.name = random.choice(self.races[race]['names'])
        self.mood = random.choice(self.races[race]['moods'])

    def change_mood(self):
        self.mood = random.choice(self.races[self.race]['moods'])

Also, instead of storing the race data in the class, you could store it in a separate JSON file or something, and then load the data from the file in the __init__ method.

Upvotes: 2

Related Questions