More actions
imported>Unknown No edit summary |
(Table transclusion repair v1) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
=== Higher Level Language === | === Higher Level Language === | ||
from bun import * | from bun import * | ||
| Line 25: | Line 24: | ||
bok.move("up", 3) | bok.move("up", 3) | ||
bok.position() # tell current position | bok.position() # tell current position | ||
=== Code (with dynamic typing, w/o string parsing ) === | === Code (with dynamic typing, w/o string parsing ) === | ||
| Line 37: | Line 34: | ||
def floors(self, floorNames): | def floors(self, floorNames): | ||
self.floors = | self.floors = [] | ||
for name in floorNames : | for name in floorNames : | ||
self.floors.append(Floor(name, buildingName)) | self.floors.append(Floor(name, buildingName)) | ||
| Line 45: | Line 42: | ||
def connectBuilding(self, direction, building): | def connectBuilding(self, direction, building): | ||
self.connectedComponent | self.connectedComponent[direction] = building | ||
def floor(self, roomID): | def floor(self, roomID): | ||
return self.floors | return self.floors[roomID] | ||
class Floor: | class Floor: | ||
| Line 56: | Line 53: | ||
def buildRooms(self, roomNames): | def buildRooms(self, roomNames): | ||
self.roomGrid = | self.roomGrid = [] | ||
for roomTup in roomNames: | for roomTup in roomNames: | ||
rooms = | rooms = [] | ||
for name in roomTup: | for name in roomTup: | ||
rooms.append(Room(name)) | rooms.append(Room(name)) | ||
| Line 74: | Line 71: | ||
def connectRoom(self, room, direction): | def connectRoom(self, room, direction): | ||
self.connectedComponent | self.connectedComponent[direction] = room | ||
| Line 90: | Line 87: | ||
for i in range(0, step): | for i in range(0, step): | ||
if self.currentPosition.connectedComponent.has_key(direction) : | if self.currentPosition.connectedComponent.has_key(direction) : | ||
self.currentPosition = self.currentPosition.connectedComponent | self.currentPosition = self.currentPosition.connectedComponent[direction] | ||
else: | else: | ||
return '갈 수 없습니다' | return '갈 수 없습니다' | ||
| Line 97: | Line 94: | ||
def tellPosition(self): | def tellPosition(self): | ||
print '현재 당신의 위치는 %s 입니다' % self.currentPosition | print '현재 당신의 위치는 %s 입니다' % self.currentPosition | ||
Latest revision as of 12:46, 27 March 2026
Higher Level Language
from bun import *
- 빌딩의 건설
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)
- Character
bok = Character("김소현",playground) bok.move("north", 2) bok.move("up", 3) bok.position() # tell current position
Code (with dynamic typing, w/o string parsing )
# -*- 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