Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

عرض المخارج

جزء من قسم Logic & Flow في رحلة Lua على Coddy — الدرس 31 من 54.

challenge icon

التحدي

سهل

قم بتحسين تجربة المستخدم في اللعبة من خلال عرض جميع المخارج المتاحة للاعب بعد إظهار وصف الغرفة.

لقد تم تزويدك بمتغيرات startingRoom و gardenRoom و player من التحدي السابق.

قم بتعديل حلقة اللعبة (game loop) لعرض المخارج المتاحة:

  • اطبع وصف الغرفة الحالية للاعب
  • اطبع "Exits:" في سطر جديد
  • استخدم pairs() للتكرار عبر جدول exits الخاص بالغرفة الحالية
  • لكل مخرج، اطبع " - " (مسافتان، شرطة، ومسافة) متبوعة باسم الاتجاه
  • اطبع رسالة الحث "> " (علامة أكبر من متبوعة بمسافة) بدون سطر جديد
  • اقرأ أمر المستخدم باستخدام 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.
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