Menu
CoddyTech

잘못된 이동 처리하기

Coddy Lua 여정의 논리와 흐름 제어 섹션에 포함된 레슨. 54개 중 30번째.

challenge icon

챌린지

쉬움

플레이어가 유효하지 않은 방향을 입력했을 때 피드백을 제공하도록 이동 시스템에 오류 처리를 추가하세요.

이전 챌린지에서 사용한 startingRoom, gardenRoom, player 변수가 제공됩니다.

잘못된 이동 명령을 처리하도록 게임 루프를 수정하세요.

  • 이전 챌린지의 east/west 출구를 north/south 출구로 바꾸세요. startingRoom.exits["north"]를 gardenRoom으로 설정하고, gardenRoom.exits["south"]를 startingRoom으로 설정하세요.
  • 플레이어가 있는 현재 방의 설명을 출력하세요.
  • 줄 바꿈 없이 프롬프트 "> "(보다 큼 기호 뒤에 공백)를 출력하세요.
  • io.read()를 사용해 사용자의 명령을 읽으세요.
  • 명령이 현재 방의 exits 테이블에 있는 키와 일치하는지 확인하세요.
  • 일치하면 player.currentRoom을 해당 출구에 저장된 방으로 업데이트하세요.
  • 명령이 어떤 출구와도 일치하지 않고 "quit"도 아니라면 "You can't go that way."를 출력하세요.
  • 명령이 "quit"이면 루프를 종료하도록 gameRunning을 false로 설정하세요.

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

예상 출력 형식:

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

직접 해보기

-- 방 정의
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 = {}
}

-- TODO: 방들을 북쪽/남쪽 출구로 연결하기

-- 플레이어
local player = {
    currentRoom = startingRoom
}

-- 게임 루프
local gameRunning = true

-- TODO: 여기에 while 루프 작성하기

논리와 흐름 제어의 모든 레슨

직접 연습해 보세요: 온라인 Lua 컴파일러