사용자 입력 처리하기
Coddy Lua 여정의 로직 및 흐름 제어 (Logic & Flow) 섹션에 포함된 레슨 — 54개 중 28번째.
챌린지
쉬움io.read()를 사용하여 사용자 명령을 읽고 출력하도록 게임 루프(game loop)를 확장하세요.
이전 챌린지에서 작성한 startingRoom, gardenRoom, 그리고 player 변수가 제공됩니다.
다음 조건을 만족하도록 게임 루프를 수정하세요:
player의 현재 방description을 출력합니다.- 줄바꿈 없이 프롬프트
"> "(greater-than 기호 뒤에 공백 하나)를 출력합니다.print()는 끝에 줄바꿈을 추가하므로 대신io.write()를 사용하세요. io.read()를 사용하여 사용자의 명령을 읽습니다."You entered: "뒤에 사용자가 입력한 명령을 출력합니다.- 명령이
"quit"인지 확인하고, 맞다면 루프를 종료하기 위해gameRunning을false로 설정합니다.
사용자가 "quit"를 입력할 때까지 루프가 계속되어야 합니다.
예상 출력 형식:
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: look
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: east
You find yourself in a dusty library filled with ancient books and scrolls.
>
You entered: quit직접 해보기
-- Room definitions
local startingRoom = {
description = "You find yourself in a dusty library filled with ancient books and scrolls."
}
local gardenRoom = {
description = "You are in a beautiful garden with blooming flowers and a fountain."
}
-- Player definition
local player = {
currentRoom = startingRoom
}
-- Create game loop
local gameRunning = true
while gameRunning do
print(player.currentRoom.description)
gameRunning = false
end