Chapter 9 : Advanced Classes and Dynamic Memory: Game Lobby

example 9.1 page no : 288

In [1]:
class Critter:
    def __init__(self,name=""):
        self.m_Name = name

    def GetName(self):
        return self.m_Name;

class Farm:
    def __init__(self,spaces = 1):
        self.m_Critters = []

    def Add(self,aCritter):
        self.m_Critters.append(aCritter);

    def RollCall(self):
        for i in self.m_Critters:
            print i.GetName() , " here.";

crit = Critter("Poochie");
print "My critter's name is " , crit.GetName()
print "\nCreating critter farm.";
myFarm = Farm(3)
print "Adding three critters to the farm.";
myFarm.Add(Critter("Moe"));
myFarm.Add(Critter("Larry"));
myFarm.Add(Critter("Curly"));
print "Calling Roll. . .";
myFarm.RollCall();
My critter's name is  Poochie

Creating critter farm.
Adding three critters to the farm.
Calling Roll. . .
Moe  here.
Larry  here.
Curly  here.

example 9.2 page no : 293

In [2]:
class Critter:
    def __init__(self,name=""):
        self.m_Name = name

    def GetName(self):
        return self.m_Name;


def Peek(aCritter):
    print aCritter.m_Name

def print_(aCritter):
    print "Critter Object - ",
    print "m_Name: " , aCritter.m_Name,


crit = Critter("Poochie");
print "Calling Peek() to access crit's private data member, m_Name: ";
Peek(crit);
print "Sending crit object to cout with the , operator:";
print_(crit)
Calling Peek() to access crit's private data member, m_Name: 
Poochie
Sending crit object to cout with the , operator:
Critter Object -  m_Name:  Poochie

example 9.3 page no : 297

In [3]:
def intOnHeap():
    pTemp = []
    return pTemp;

def leak1():
    drip1 = []

def leak2():
    drip2 = []


pHeap = 10;
print "*pHeap: " , pHeap
pHeap2 = intOnHeap();
print "pHeap2: " , pHeap2 
print "Freeing memory pointed to by pHeap."; # python automatically deletes the memory.
print "Freeing memory pointed to by pHeap2.";
pHeap = 0;
pHeap2 = 0;
*pHeap:  10
pHeap2:  []
Freeing memory pointed to by pHeap.
Freeing memory pointed to by pHeap2.

example 9.4 page no : 304

In [4]:
class Critter:
    def __init__(self,name=None,age=None):
        if (age!=None and name != None) or (age==None and name==None):
            print "Constructor called";
            self.m_pName = name
            self.m_Age = age
        else:
            print "Copy Constructor called";
            self.m_pName = name.m_pName
            self.m_Age = name.m_Age;

    def GetName(self):
        return self.m_Name;

    def __del__(self):
        print "Destructor called\n";

    def operator(self,c):
        print "Overloaded Assignment Operator called";
        return self
        
    def Greet(self):
        print "I'm " , self.m_pName , " and I'm " , self.m_Age , " years old.";
        print "&m_pName: " , 
        print id(self.m_pName)


def testDestructor():
    toDestroy = Critter("Rover", 3);
    toDestroy.Greet();  

def testCopyConstructor(aCopy):
    aCopy.Greet();

def testAssignmentOp():
    crit1 = Critter("crit1", 7);
    crit2 = Critter("crit2", 9);
    crit1 = crit2;
    crit1.Greet();
    crit2.Greet();
    crit3 = Critter("crit", 11);
    crit3 = crit3;
    crit3.Greet();

testDestructor();
print ''
crit = Critter("Poochie", 5);
crit.Greet();
testCopyConstructor(crit);
crit.Greet();
print ''
testAssignmentOp();
Constructor called
I'm  Rover  and I'm  3  years old.
&m_pName:  30745776
Destructor called


Constructor called
I'm  Poochie  and I'm  5  years old.
&m_pName:  30744672
I'm  Poochie  and I'm  5  years old.
&m_pName:  30744672
I'm  Poochie  and I'm  5  years old.
&m_pName:  30744672

Constructor called
Constructor called
Destructor called

I'm  crit2  and I'm  9  years old.
&m_pName:  30725152
I'm  crit2  and I'm  9  years old.
&m_pName:  30725152
Constructor called
I'm  crit  and I'm  11  years old.
&m_pName:  30744624
Destructor called

Destructor called

example 9.5 and 9.6 page no : 318

In [7]:
class Player:
    def __init__(self,name):
        self.m_Name =name
        self.m_pNext = 0

    def GetName(self):
        return self.m_Name;

    def GetNext(self):
        return self.m_pNext;

    def SetNext(self,next):
        self.m_pNext = next;
        
class Lobby:
    def __init__(self):
        self.m_pHead =0

    def __del__(self):
        self.Clear();

    def AddPlayer(self):
        #create a new player node
        print "Please enter the name of the new player: ",
        name = 'abc' #raw_input()
        pNewPlayer = Player(name);
        #if list is empty, make head of list this new player
        if (self.m_pHead == 0):
            self.m_pHead = pNewPlayer;
        #otherwise find the end of the list and add the player there
        else:
            pIter = self.m_pHead;
            while (pIter.GetNext() != 0):
                pIter = pIter.GetNext();
            pIter.SetNext(pNewPlayer);
    
    def RemovePlayer(self):
        if (self.m_pHead == 0):
            print "The game lobby is empty."
        else:
            pTemp = self.m_pHead;
            self.m_pHead = self.m_pHead.GetNext();

    def Clear(self):
        while (self.m_pHead != 0):
            RemovePlayer();

    def print_(self):
        pIter = self.m_pHead;

        print "Here's who's in the game lobby:";
        if (pIter == 0):
            print "The lobby is empty.\n";
        else:
            while (pIter != 0):
                print pIter.GetName()
                pIter = pIter.GetNext();

myLobby = Lobby()
choice = 1 
choices = [1,2,3,0]
i = 0
while (choice != 0):
    print myLobby.print_();
    print "\nGAME LOBBY";
    print "0 - Exit the program.";
    print "1 - Add a player to the lobby.";
    print "2 - Remove a player from the lobby.";
    print "3 - Clear the lobby.";
    print "\nEnter choice: ",
    choice = choices[i] #int(raw_input())
    i += 1
    if choice ==0:
        print "Good-bye."
    elif choice ==1:        
        myLobby.AddPlayer()
    elif choice==2:            
        myLobby.RemovePlayer(); 
    elif choice==3:            
        myLobby.Clear()
    else:
        print "That was not a valid choice.\n";
 Here's who's in the game lobby:
The lobby is empty.

None

GAME LOBBY
0 - Exit the program.
1 - Add a player to the lobby.
2 - Remove a player from the lobby.
3 - Clear the lobby.

Enter choice:  Please enter the name of the new player:  Here's who's in the game lobby:
abc
None

GAME LOBBY
0 - Exit the program.
1 - Add a player to the lobby.
2 - Remove a player from the lobby.
3 - Clear the lobby.

Enter choice:  Here's who's in the game lobby:
The lobby is empty.

None

GAME LOBBY
0 - Exit the program.
1 - Add a player to the lobby.
2 - Remove a player from the lobby.
3 - Clear the lobby.

Enter choice:  Here's who's in the game lobby:
The lobby is empty.

None

GAME LOBBY
0 - Exit the program.
1 - Add a player to the lobby.
2 - Remove a player from the lobby.
3 - Clear the lobby.

Enter choice:  Good-bye.