Putting stuff on stuff…

After finishing containers the other day, I also decided to create supporters. A supporter is an object on which you can put things—like a table, for example, or a shelf.

Very quickly, I realized that supporters are no different from containers, really. Instead of putting things IN them, you put things ON them. Since everything in a text adventure is abstract, it doesn’t really matter either way. So, when you think about it, supporters are essentially containers without a lid, meaning that they are always open.

In practical terms, this means that the code base is virtually identical. All that changes is the output text when I am listing the contents.

For a container, I will print something like…

The box contains a key and a newspaper.

…whereas a supporter would print…

You see a key and a newspaper on the table.

Minor text differences, as you’ll certainly agree.

The functionality to add or remove items is also identical, so I’ve decided to do a little rewrite. I made a Supporter class and use it as the superclass for my containers.

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

classSupporter(object):

""" This class defines all sorts of supporters where items can be placed on """

def__init__(self,_contents=None,*args,**kwargs):

super().__init__(*args,**kwargs)

self.Contents=_contents

self.EmptyString="There's nothing on it."

self.FullString="You see {1} on the {0}."

self.Silent=True# Empty contents do not spawn a message

defPrintContents(self):

_listString=self.ListContents()

ifnot_listString:

ifFalse==self.Silent:

print(self.EmptyString)

else:

print(self.FullString.format(globals.theNounString,_listString))

returnTrue

defListContents(self):

_counter=0

_outString=""

ifself.Contents:

for_item inself.Contents:

if0!=_counter:

_outString+=" and "

_outString+=_item.GetName(globals.Articles.Undef)

_counter+=1

if_counter:

_outString=_outString.replace(" and ",", ",_counter-2)

return_outString

defHasItem(self,_token):

""" Check for a particular item and return the first match """

ifself.Contents:

for_item inself.Contents:

if_token==_item.Token:

return_item

returnNone

defRemoveItem(self,_token):

""" Remove an item from the contents list """

ifself.Contents:

for_item inself.Contents:

if_token==_item.Token:

self.Contents.remove(_item)

return_item

returnNone

defAddItem(self,_item):

self.Contents.append(_item)

defEvaluate(self):

ifTokens.Look==globals.theVerb andnotglobals.thePrep:

self.Describe()

returnself.PrintContents()

returnFalse

This is what the class looks like, complete with various helper methods to check for items in the supporter’s contents, add items, remove items, etc.

If you recall my previous Container class, you will notice that they look eerily similar. But not for much longer, because my original Container class has become almost entirely redundant.

# Put check in here if the container is closed/locked, etc. before listing contents

returnsuper().ListContents()

Pay attention to line 2, where I am now deriving my Container class from the Supporter class, making sure all of its functionality is coming along with it.

As you can see also, all relevant functionality has been delegated to the Supporter superclass. The only main difference is that I am catching the ListContents() function so I can add some checks that will make sure the container is actually open and not, by any chance, closed or even locked. I haven’t gotten to that yet, but it should be simple enough to add when I need it.

One of the interesting tidbits here that you may want to pay attention to is that I initialize the text string EmptyString and FullString differently in the Container than in the Supporter class. This will automatically match the correct text output to indicate things are supposedly “in” the container as opposed to “on top” of it.

I am using the string format() function here to do a bit of string magic, as it allows me to use any sentence structure and still fill in the correct text subject and the list, wherever it needs to be. The parameters in the function allow me to do that just nicely.

Funny how going deeper down the rabbit hole sometimes changes your whole perspective. I had thought my containers are a clean-cut thing, and yet, here I am after shuffling code around to accommodate the same thing in a different way. It just goes to show you that in programming, there is no one right solution.