#######################################################
# OBJECT ORIENTED PROGRAMMING:
#######################################################
# main class:
class bag(object):
def __init__(self):
self.things = []
def findThing(self, thing):
for x in self.things:
if x == thing:
return x
else:
pass
def addThing(self, thing):
self.things.append(thing)
def removeThing(self, thing):
if thing in self.things:
self.things.remove(thing)
# sub classes:
class Room(bag):
def __init__(self, name):
self.name = name
super(Room, self).__init__()
self.exits = dict()
def addExit(self, dir, targetRoom):
self.exits[dir] = targetRoom
def getExit(self, dir):
try:
return self.exits[dir]
except:
pass
def getExitDirs(self):
return self.exits.keys()
class Player(bag):
pass
# program class:
class Game:
def __init__(self):
print "**********************************************"
print " Welcome to the haunted house!\n\t\t1.0\nfind Shaggy and Scooby Doo then get out alive!"
print "**********************************************\n"
print "How to play:"
print "go
-> to go in that direction"
print "pickup -> to pick up an object"
print "drop -> to drop an object"
print "unlock -> to unlock an object"
print "quit -> to quit the game\n"
# room 1
room1 = Room("study")
room1.addThing("cat")
room1.addThing("bigSpider")
room1.addThing("key")
# room 2
room2 = Room("hallway")
room2.addThing("skeleton")
room2.addThing("man-eatingPlant")
# room 3
room3 = Room("kitchen")
room3.addThing("posion")
room3.addThing("bats")
room3.addThing("Shaggy")
# room 4
room4 = Room("dungeon")
room4.addThing("chains")
room4.addThing("ScoobyDoo")
# set up game
room1.addExit("north", room2)
room2.addExit("south", room1)
room2.addExit("east", room3)
room2.addExit("west", room4)
room3.addExit("west", room2)
room4.addExit("east", room2)
self.currentRoom = room1
self.rooms = { "study":room1, "hallway":room2, "kitchen":room3 ,"dungeon":room4 }
self.player = Player()
self.gameOver = False
self.ScoobyDooFlag = False
def go(self, dir):
targetRoom = self.currentRoom.getExit(dir)
if targetRoom == None:
print "I cannot go this way!"
else:
self.currentRoom = targetRoom
def pickUp(self, thing):
thing = self.currentRoom.findThing(thing)
if thing == None:
print "I dont see that!"
elif thing == "chains":
print "I can't pick those up! they are attached to Scooby"
elif thing in ["bigSpider", "man-eatingPlant", "posion"]:
print "YOU DIED!"
self.gameOver = True
elif (thing == "Shaggy") and ("ScoobyDoo" in self.player.things):
print "YOU SAVED THEM!"
self.gameOver = True
elif (thing == "ScoobyDoo") and self.ScoobyDooFlag:
if ("Shaggy" in self.player.things):
print "YOU SAVED THEM!"
self.gameOver = True
else:
self.player.addThing(thing)
self.currentRoom.removeThing(thing)
elif (thing == "ScoobyDoo") and not self.ScoobyDooFlag:
print "I can't pick him up. He seems to be locked by the chains..."
else:
self.player.addThing(thing)
self.currentRoom.removeThing(thing)
def drop(self, thing):
thing = self.player.findThing(thing)
if thing == None:
print "I dont have that!"
else:
self.player.removeThing(thing)
self.currentRoom.addThing(thing)
def unlock(self, thing):
thing = self.currentRoom.findThing(thing)
if thing == "chains":
check = self.player.findThing("key")
if check == None:
print "I need a key to do that!"
else:
self.ScoobyDooFlag = True
self.currentRoom.removeThing(thing)
self.currentRoom.addThing("unlocked chains")
else:
print "I can't unlock that!"
def playGame(self):
while not self.gameOver:
print "\nI am in:", self.currentRoom.name
print "I can see:", self.currentRoom.things
print "I am carrying:", self.player.things
print "Exits are:", self.currentRoom.getExitDirs()
cmd = raw_input("\nWhat next? --> ")
if (" " not in cmd): cmd += " "
try:
(verb, noun) = cmd.split(" ")
except:
print "Invalid input"
continue
if (verb == "pickup"):
self.pickUp(noun)
elif (verb == "drop"):
self.drop(noun)
elif (verb == "go"):
self.go(noun)
elif (verb == "unlock"):
self.unlock(noun)
elif (verb == "quit"):
break
else:
print "I don't know how to do that!"
game = Game()
game.playGame()
#######################################################
# NETWORK PROGRAMMING:
#######################################################
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'SERVER: Got connection from:', addr
c.send('SERVER: Thank you for connecting!\n')
c.send('SERVER: ...\n')
c.send('SERVER: ...\n')
c.send('SERVER: okay...\n')
c.send("SERVER: goodbye")
c.close() # Close the connection
##########################################################
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234 # Reserve a port for your service.
print "CLIENT: connecting..."
try:
s.connect((host, port)) # Connect!
except:
print "CLIENT: connection failed!"
print s.recv(1024) # what did the server send back!
s.close # Close the socket when done
print "CLIENT: connection closed"