Higher Level Language ¶
{{|
from bun import *
from bun import *
#
bobst = Building("공", 7)
bobst.floors(("1","2","3","4","5","6","7"))
bobst.floor(1).buildRooms((("교","","1-2"),
bobst.floor(1).room(2,2).connectRoom(bobst.floor(2).room(2,2),"up") # 결
bobst.floor(2).room(2,2).connectRoom(bobst.floor(1).room(2,2),"down") # 결
bobst = Building("공", 7)
bobst.floors(("1","2","3","4","5","6","7"))
bobst.floor(1).buildRooms((("교","","1-2"),
("1-1","","1-3"),
("","계",""))
# 과 결("","계",""))
bobst.floor(1).room(2,2).connectRoom(bobst.floor(2).room(2,2),"up") # 결
bobst.floor(2).room(2,2).connectRoom(bobst.floor(1).room(2,2),"down") # 결
# 과 결
playground = Building("", 1)
playground.connectBuilding("north", bobst)
bobst.connectBuilding("south", playground)
playground.connectBuilding("north", bobst)
bobst.connectBuilding("south", playground)
# Character
bok = Character("",playground)
bok.move("north", 2)
bok.move("up", 3)
bok.position() # tell current position
bok = Character("",playground)
bok.move("north", 2)
bok.move("up", 3)
bok.position() # tell current position
|}}
Code (with dynamic typing, w/o string parsing ) ¶
~cpp # -*- coding: UTF-8 -*- class Building: def __init__(self, name, numFloors): self.name = name self.numFloors = numFloors self.connectedComponent = {} def floors(self, floorNames): self.floors = [] for name in floorNames : self.floors.append(Floor(name, buildingName)) def __str__(self): return self.name def connectBuilding(self, direction, building): self.connectedComponent[direction] = building def floor(self, roomID): return self.floors[roomID] class Floor: def __init__(self, floorName, buildingName): self.name = floorName self.buildingName = buildingName def buildRooms(self, roomNames): self.roomGrid = [] for roomTup in roomNames: rooms = [] for name in roomTup: rooms.append(Room(name)) self.roomGrid.append(rooms) def __str__(self): return self.name class Room: def __init__(self, roomName, floorName): self.roomName = roomName self.floorName = floorName self.connectedComponent = {} def connectRoom(self, room, direction): self.connectedComponent[direction] = room class Character: def __init__(self, name, currentPosition): self.currentPosition = currentPosition self.name = name self.tellPosition() def __str__(self): return self.name def move(self, direction, step): for i in range(0, step): if self.currentPosition.connectedComponent.has_key(direction) : self.currentPosition = self.currentPosition.connectedComponent[direction] else: return '갈 ' self.tellPosition() def tellPosition(self): print ' %s ' % self.currentPosition