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

معالجة مدخلات المستخدم

جزء من قسم المنطق والتحكم في التدفق في رحلة Lua على Coddy. الدرس 28 من 54.

challenge icon

التحدي

سهل

وسّع حلقة اللعبة لقراءة أوامر المستخدم وعرضها باستخدام io.read().

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

عدّل حلقة اللعبة من أجل:

  • طباعة وصف الغرفة الحالية للاعب
  • طباعة موجّه الأوامر "> " (علامة أكبر من متبوعة بمسافة) من دون سطر جديد: استخدم io.write() بدلًا من 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

جميع دروس المنطق والتحكم في التدفق

تدرّب بنفسك: مترجم Lua عبر الإنترنت