2012-03-30 7 views
2

저는 어린 시절 게임을 해킹했습니다. 그냥 재미로 보았습니다. 문제가 생겼습니다. 내가 코드를 게시하고 설명하기 위해 최선을 다할 것입니다 :내 목록이 변경되지 않는 이유는 무엇입니까?

def parseCmd(string): 
    cmd = string.split(' ') 
    if cmd[0] == 'help': 
     showHelp() 
    elif cmd[0] == 'add': 
     addServer() 
    elif cmd[0] == 'bag': 
     viewInventory(inventory) 
    elif len(cmd) == 1 and cmd[0] == 'look': 
     describeRoom() 
    elif len(cmd) == 1 and cmd[0] == 'take': 
     print 'What do you want me to take?' 
    elif cmd[0] == 'take': 
     pickUp(cmd[1], items) 
    elif cmd[0] == 'exit': 
     sys.exit(0) 
    else: 
     print 'I don\'t know how to ' + cmd[0] 

def describeRoom(): 
    print locations[player_location] 

def pickUp(item, item_list): 
    if item in item_list[player_location]: 
     item_list[player_location].remove(item) 
     inventory.append(item) 
     print 'You took the ' + item   
    else: 
     print 'I can\'t find any ' + item 

inventory = ['id card', 'money', 'keys'] 
player_location = 'cookieroom' 
items = {'cookieroom': ['crowbar', 'hammer']} 
locations = {'cookieroom': 'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: %s' % items[player_location], 
       'LFA': 'The infamous LFA, where dreams of office supplies become reality. there is a big guy sleeping in his chair next to a fire extinguisher.\n\nSOUTH: Cookieroom, WEST: WC'} 

if __name__ == "__main__": 
    while 1: 
     t = raw_input('-> ') 
     parseCmd(t) 

을 그래서, 당신은 내가 항목에서 항목의 목록을 원하는 참조하면 특정 공간에서 사용할 수있는 항목을 선택할 때 변경 사전을 할 수있다. 나는 아이템을 집어 올릴 수 있고 그것이 나의 인벤토리에 추가된다. 그러나 내가 'look'명령을 내리면, 그것의 원래 상태에있는 아이템의리스트를 보여준다.

저는 1½ 일 동안 인터넷 검색 및 stackoverflowing을 해왔으며이 문제를 해결할 것으로 보이는 항목을 찾을 수 없습니다.

뭔가 명확하지 않은 경우 나에게 묻기 만하면 답변을 드리겠습니다.

답변

4

describeRoom 함수가 방의 설명을 선택하는 곳인 locations 사전은 프로그램이 시작될 때 한 번 초기화됩니다. 그때 플레이어의 위치는 cookieroom이고 거기에있는 객체는 crowbarhammer입니다. 그래서, 문자열은 나중에 items 사전의 내용을 변경할 경우에도이 문자열은 변경되지 않습니다 그래서

'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: ["crowbar", "hammer"]' 

같이 생성됩니다.

locations 사전에는 객실 설명의 변경되지 않는 부분 만 포함되어야합니다. 사용자가 방의 설명을 요청할 때마다 변경 부분 (예 : 방의 항목 목록 등)을 다시 계산해야합니다.

+0

좋아요! Noufal 감사합니다. 나는 충분히 명성이 없기 때문에 나는 이것을 upvote 할 수 없다. –

관련 문제