無効な移動の処理
CoddyのLuaジャーニー「ロジックとプログラムの流れ」セクションの一部。レッスン 30/54。
チャレンジ
簡単プレイヤーが無効な方向を入力したときにフィードバックを表示するよう、移動システムにエラー処理を追加してください。
前のチャレンジで使用したstartingRoom、gardenRoom、playerの各変数が用意されています。
無効な移動コマンドを処理するよう、ゲームループを変更してください。
- 前のチャレンジの東西の出口を北/南の出口に置き換えます。
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オンラインコンパイラ