Menu
Coddy logo textTech

출구 표시하기

Coddy Lua 여정의 Logic & Flow 섹션에 포함된 레슨 — 54개 중 31번째.

challenge icon

챌린지

쉬움

방 설명이 표시된 후 플레이어에게 사용 가능한 모든 출구를 보여줌으로써 게임의 사용자 경험을 향상시키세요.

이전 챌린지에서 사용했던 startingRoom, gardenRoom, 그리고 player 변수가 제공됩니다.

사용 가능한 출구를 표시하도록 게임 루프를 수정하세요:

  • 플레이어의 현재 방에 대한 설명을 출력합니다.
  • 새 줄에 "Exits:"를 출력합니다.
  • pairs()를 사용하여 현재 방의 exits 테이블을 반복합니다.
  • 각 출구에 대해 " - " (공백 두 개, 대시, 공백 한 개)와 방향 이름을 이어서 출력합니다.
  • 줄 바꿈 없이 프롬프트 "> " (보다 큼 기호와 공백 한 개)를 출력합니다.
  • io.read()를 사용하여 사용자의 명령을 읽습니다.
  • 명령이 현재 방의 exits 테이블에 있는 키와 일치하는지 확인합니다.
  • 일치하는 경우, player.currentRoom을 해당 출구에 저장된 방으로 업데이트합니다.
  • 명령이 어떤 출구와도 일치하지 않고 "quit"도 아닌 경우, "You can't go that way."를 출력합니다.
  • 명령이 "quit"인 경우, gameRunningfalse로 설정하여 루프를 종료합니다.

루프는 사용자가 "quit"을 입력할 때까지 계속되어야 합니다.

예상 출력 형식:

You find yourself in a dusty library filled with ancient books and scrolls.
Exits:
  - east
> 
A hidden garden with overgrown plants and a small fountain.
Exits:
  - west
> 
You can't go that way.
A hidden garden with overgrown plants and a small fountain.
Exits:
  - west
> 
You find yourself in a dusty library filled with ancient books and scrolls.
Exits:
  - east
> 

직접 해보기

-- Room definitions
local startingRoom = {
    description = "You find yourself in a dusty library filled with ancient books and scrolls.",
    exits = {}
}

local gardenRoom = {
    description = "A hidden garden with overgrown plants and a small fountain.",
    exits = {}
}

-- Connect rooms
startingRoom.exits["north"] = gardenRoom
gardenRoom.exits["south"] = startingRoom

-- Player
local player = {
    currentRoom = startingRoom
}

-- Game loop
local gameRunning = true

while gameRunning do
    print(player.currentRoom.description)
    io.write("> ")
    local command = io.read()
    
    if command == "quit" then
        gameRunning = false
    elseif player.currentRoom.exits[command] then
        player.currentRoom = player.currentRoom.exits[command]
    else
        print("You can't go that way.")
    end
end

Logic & Flow의 모든 레슨