Menu
CoddyTech

טיפול בקלט המשתמש

חלק מהיחידה לוגיקה וזרימת התוכנית במסלול ה-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

נסו בעצמכם

-- חדרי המשחק
local startingRoom = {
    description = "You find yourself in a dusty library filled with ancient books and scrolls."
}

local gardenRoom = {
    description = "You step into a beautiful garden with blooming flowers and a gentle breeze."
}

-- שחקן
local player = {
    currentRoom = startingRoom
}

-- לולאת המשחק
local gameRunning = true

-- TODO: כתבו כאן את לולאת ה-while

כל השיעורים ביחידה לוגיקה וזרימת התוכנית

תרגלו בעצמכם: קומפיילר Lua אונליין