Menu
CoddyTech

Moving the Player

Part of the Logic & Flow section of Coddy's Lua journey. Lesson 29 of 54.

challenge icon

Challenge

Easy

Implement the movement system that allows the player to navigate between rooms by checking their input against available exits.

You are provided with the startingRoom, gardenRoom, and player variables from the previous challenge.

Modify the game loop to handle movement commands:

  • The starter connects the rooms: east from the library leads to the garden and west leads back
  • Print the description of the player's current room
  • Print the prompt "> " (greater-than sign followed by a space) without a newline
  • Read the user's command using io.read()
  • Check if the command matches a key in the current room's exits table
  • If it matches, update player.currentRoom to the room stored in that exit
  • If the command is "quit", set gameRunning to false to exit the loop

The loop should continue until the user types "quit".

Note: Because io.write("> ") does not add a newline, the next line of output (the room description) appears immediately after the prompt on the same line in the raw output stream. Each loop iteration produces: the room description on its own line, then > with no newline, so the following description continues on that same line.

Expected Output Format (inputs: east, then quit):

You find yourself in a dusty library filled with ancient books and scrolls.
> A hidden garden with overgrown plants and a small fountain.
> 

Try it yourself

-- 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 = {}
}

-- Set exits using east / west
startingRoom.exits = {
  east = gardenRoom
}

gardenRoom.exits = {
  west = startingRoom
}

-- Player
local player = {
  currentRoom = startingRoom
}

-- Game loop
local gameRunning = true

-- TODO: Write the while loop here

All lessons in Logic & Flow

Practice on your own: Online Lua compiler