Menu
Coddy logo textTech

ユーザー入力の処理

CoddyのLuaジャーニー「ロジックと制御フロー」セクションの一部 — レッスン 28/54。

challenge icon

チャレンジ

簡単

io.read() を使用してユーザーのコマンドを読み取り、表示するようにゲームループを拡張します。

前のチャレンジから引き継いだ startingRoomgardenRoom、および player 変数が用意されています。

ゲームループを以下のように修正してください:

  • プレイヤーの現在の部屋の description を出力する
  • プロンプト "> " (改行なしの不等号(大なり)とそれに続くスペース) を出力する — io.write() は末尾に改行を追加せずテキストを出力するため、print() の代わりに io.write() を使用します
  • io.read() を使用してユーザーのコマンドを読み取る
  • "You entered: " に続けてユーザーが入力したコマンドを出力する
  • コマンドが "quit" であるかを確認し、そうである場合は gameRunningfalse に設定してループを終了する

ユーザーが "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

ロジックと制御フローのすべてのレッスン