Rewrite a simple game without using the sentence "goto"


In a part of the book I am reading I need to rewrite a "Labyrinth" game that uses goto sentences to another that does not use them.

The labyrinth is very simple, it only has 4 rooms and it starts from room # 1:

| 1 | 2 |
| 3 | 4 |

This is the original version of the book using goto :

goto room1 -- initial room
::room1:: do
  local move =
  if move == "south" then goto room3
  elseif move == "east" then goto room2
    print("invalid move")
    goto room1 -- stay in the same room

::room2:: do
  local move =
  if move == "south" then goto room4
  elseif move == "west" then goto room1
    print("invalid move")
    goto room2

::room3:: do
  local move =
  if move == "north" then goto room1
  elseif move == "east" then goto room4
    print("invalid move")
    goto room3

::room4:: do
  print("Congratulations, you won!")

And this is my attempt to rewrite it:

local rooms = {}
rooms[1] = {
rooms[2] = {
rooms[3] = {
currentRoom = 1
  local move =
  local room = rooms[currentRoom][move]
  if room == nil then
    room = currentRoom
    print("Invalid move")
  elseif room == 4 then
    print("Congratulations, you won!")
  currentRoom = room or currentRoom
until currentRoom == 4 

In this case I found it more appropriate to use repeat instead of while although in this specific case I think both work the same.

Is my solution the most optimal or could I change something in my code to improve it?

asked by César 22.03.2016 в 17:59

1 answer


I think the code is very logical but there are specific Lua things that you have to take into account for your question.

For this case use the loop while

local curRoom, move, room = 1

while true do
  io.write("Estás en la habitación "..curRoom.." Diga a donde ir: ")
  move =
  room = rooms[curRoom][move]
  if room == 4 then
    print("En hora buena... ¡Ganaste!"); break -- salimos del bucle while
  curRoom = room or curRoom

Note that I have declared the local variables outside the loop, this is a good practice in Lua to improve the code performance and there is no need to assign them a value when they are declared at the beginning.

With respect to conditional if room = nil then.. I deleted it because as I explained in this post Lua considers as false a null or false variable -values redundancy- this can be confusing because one could even believe in the first instance that an empty table is null which is not true for Lua

print(nil or 2)                             -- se imprime: 2
print(1 or 2)                               -- se imprime: 1
local miVar; print(miVar or "otro valor")   -- se imprime: otro valor
print({} or "tb")                           -- se imprime: table: 0x....

This can also be similar to C # with its operator?: leaving something like the link example

local function cal(x) print(x~=0 and math.sin(x)/x or 1) end
cal(0); cal(0.8)
-- se imprime: 1    0.8966951136244035

In that sense curRoom = room or curRoom will remain curRoom = curRoom yes room is nil only when room has some value, the previous expression will behave as if it were curRoom = room

answered by 06.06.2018 в 04:44