Menu
Coddy logo textTech

Processando a Entrada do Usuário

Parte da seção Logic & Flow do Journey de Lua da Coddy — lição 28 de 54.

challenge icon

Desafio

Fácil

Estenda o loop do jogo para ler e exibir comandos do usuário usando io.read().

Você recebeu as variáveis startingRoom, gardenRoom e player do desafio anterior.

Modifique o loop do jogo para:

  • Imprimir a descrição da sala atual do jogador
  • Imprimir o prompt "> " (sinal de maior que seguido por um espaço) sem uma nova linha
  • Ler o comando do usuário usando io.read()
  • Imprimir "You entered: " seguido pelo comando que o usuário digitou
  • Verificar se o comando é "quit" - se for, defina gameRunning como false para sair do loop

O loop deve continuar até que o usuário digite "quit".

Formato de Saída Esperado:

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

Experimente você mesmo

-- 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

Todas as lições de Logic & Flow