RCRPG/Python: Difference between revisions

482 bytes removed ,  13 years ago
no edit summary
(Created page with '{{collection|RCRPG}} This Python 3.1 version of RCRPG uses the standard text interface. All the commands listed on the blog post are implemented, as well…')
 
No edit summary
Line 5:
 
==Code==
<lang python>importdirections re= {'north':(0,-1,0),
from random import random
 
directions = {'north':(0,-1,0),
'east':(1,0,0),
'south':(0,1,0),
Line 26 ⟶ 23:
 
class Room:
passages = []
items = []
 
def __init__(self, items=[]):
self.passages = dict(zip.fromkeys(directions.keys(),[ False]*len(directions.keys())))
self.items = items[:]
 
def describe(self, location):
result = 'You are at '
if (location in roomNames.keys()):
result += roomNames[location]
else:
posStr = ",".join(list(str(v) for v in location))
result += posStr
if (len(self.items)):
result += '\nOn the ground you can see: '+', '.join(self.items)
result += '\nExits are: '
exits = list(filter(lambda[k x:for self.passages[x]k,v in self.passages.keysitems())) if v] or ['None']
ifresult += ', '.join(lenmap(str.capitalize, exits) == 0):
exits = ['None']
result += ', '.join(list(map(lambda x: x.capitalize(), exits)))
return result
 
def take(self, target):
results = []
if (target == 'all'):
results = self.items[:]
del self.items = [:]
print('You now have everything in the room.')
elif (target in self.items):
index = self.items.indexremove(target)
results = self.items[index:index+1target]
self.items = self.items[0:index]+self.items[index+1:]
print('Taken.')
else:
Line 71 ⟶ 62:
 
def opposite_dir(direction):
if (direction == 'north'):
return 'south'
elif (direction == 'south'):
return 'north';
elif (direction == 'west'):
return 'east';
elif (direction == 'east'):
return 'west';
elif (direction == 'up'):
return 'down';
elif (direction == 'down'):
return 'up';
else:
Line 87 ⟶ 78:
 
def make_random_items():
randfrom = int(random()*4) import randrange
if (rand == 0randrange(4):
if rand == 0:
return []
elif (rand == 1):
return ['sledge']
elif (rand == 2):
return ['ladder']
else:
Line 98 ⟶ 90:
 
class World:
def __init__(self):
currentPos = 0,0,0
rooms self.currentPos = {(0,0,0): Room(['sledge'])}
self.rooms = {(0,0,0): Room(['sledge'])}
inv = []
equipped self.inv = ''[]
exitsself.equipped = ['None']
 
def look(self):
Line 107 ⟶ 100:
 
def move(self, direction):
if (direction == 'up' and \
not ('ladder' not in self.rooms[self.currentPos].items)):
print("You'll need a ladder in this room to go up.")
return
if (not (direction not in directions.keys())):
print("That's not a direction.")
return
if (self.rooms[self.currentPos].passages[direction]):
self.currentPos = get_new_coord(self.currentPos, direction)
else:
Line 124 ⟶ 117:
 
def inventory(self):
if (len(self.inv)):
print('Carrying: '+', '.join(self.inv))
else:
print("You aren't carrying anything.")
if (self.equipped != ''):
print('Holding: '+self.equipped)
 
Line 135 ⟶ 128:
 
def drop(self, target):
if (target == 'all'):
self.rooms[self.currentPos].items += .extend(self.inv[:])
del self.inv = [:]
print('Everything dropped.')
elif (target in self.inv):
index = self.inv.indexremove(target)
self.rooms[self.currentPos].items += self.inv[index:index+1]append(target)
self.inv = self.inv[0:index]+self.inv[index+1:]
print('Dropped.')
else:
Line 148 ⟶ 140:
 
def equip(self, itemName):
if (itemName in self.inv):
if (self.equipped != ''):
self.unequip()
index = self.inv.indexremove(itemName)
self.equipped = "".join(self.inv[index:index+1])itemName
self.inv = self.inv[0:index]+self.inv[index+1:]
print('Equipped '+itemName+'.')
else:
print("You aren't caryingcarrying that.")
 
def unequip(self):
if (not self.equipped == ''):
print("You aren't equipped with anything.")
else:
self.inv += [.append(self.equipped])
print('Unequipped '+self.equipped+'.')
self.equipped = ''
Line 170 ⟶ 161:
 
def dig(self, direction):
if (self.equipped != 'sledge'):
print("You don't have a digging tool equipped.")
return
if (not (direction not in directions.keys())):
print("That's not a direction.")
return
if (not self.rooms[self.currentPos].passages[direction]):
self.rooms[self.currentPos].passages[direction] = True
joinRoomPos = get_new_coord(self.currentPos, direction)
if (not (joinRoomPos not in self.rooms)):
self.rooms[joinRoomPos] = Room(make_random_items())
self.rooms[joinRoomPos].passages[opposite_dir(direction)] = True
Line 191 ⟶ 182:
while True:
print('\n'+world.look())
tokens = re.split(r"\s+", input("> ").strip().lower().split()
if (tokens[0] == 'quit'):
break
while (len(tokens)):
if (tokens[0] in aliases.keys() and tokens[0] != aliases[tokens[0]][0]):
tokens[0] = aliases[tokens[0]] + tokens[1:]
continue
Anonymous user