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();
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)
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;
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();
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";