I'm using geany to run this. The problem is, the console is returning that I'm trying to index a nil value but I don't know if it is in line 63, 80, or even in another line. I'm really a beginner in programming so I don't know and understand many things if you could keep it simple I would be grateful.
Here's the error message:
lua: TTC.Lua:65: attempt to index a nil vallue (field'?')
stack traceback:
TTC.Lua:65: in function 'preenche_tabuleiro'
TTC.Lua:82: in main chunk
[C]:in ?
Here's the code:
function cria_tabuleiro()
return {{_, _, _}, {_, _, _}, {_, _, _}}
end
function pula_linha(_)
for __ = 0, _ do print() end
end
function abertura_do_jogo()
pula_linha(7)
print("\t-_-_-_-_-_-_-_-_-_-_-_-_-")
print("\t- TIC-TAC-TOE -")
print("\t-_-_-_-_-_-_-_-_-_-_-_-_-")
end
function checa_OS()
os.getenv("HOME")
if home == nil then return "|", "--", " " end
return "↓", "→ ", "."
end
function recebe_nomes()
jogs = {}
for _ = 1,2 do
msg = ("Digite o nome do jogador numero %s: ")
io.write(msg:format(_))
table.insert(jogs, io.read())
end
return jogs
end
function imprimir_tabuleiro(T, SB, SD)
abertura_do_jogo()
pula_linha(2)
print(string.format("\t\t A B C\n\t\t %s %s %s" , SB, SB, SB))
for _ = 1,3 do
io.write(string.format("\t\t %s%s", _, SD))
print(table.concat(T[_], " "))
end
pula_linha(5)
end
function ler_jogada(JOGADORES, X)
jogada = {}
checa_jog = function(jog)
coluna = string.byte(jog:upper()) - 64
linha = tonumber(jog:sub(2))
if coluna >= 1 and coluna <= 3 and linha >= 1 and linha <= 3 then
return coluna, linha
else
print("Sua jogada foi invalida, tente novamente")
ler_jogada(jogadores, X)
end
end
pula_linha(2)
io.write(string.format("%s, digite sua jogada (EX:B3, A2, ETC...) : ", JOGADORES[X]))
table.insert(jogada, io.read())
col, lin = checa_jog(jogada[1])
end
function preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS, jogadores, _, COL, LIN)
if tabuleiro[LIN][COL] == POS_VAZIA then
tabuleiro[LIN][COL] = PECAS
else
msg = "%s, voce tentou uma posicao ja preenchida. jogue novamente."
print(msg:format(jogadores[_]))
preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS, jogadores, _, ler_jogada(jogadores, _))
return tabuleiro
end
end
abertura_do_jogo()
SETA_BAIXO, SETA_DIREITA, POS_VAZIA = checa_OS()
tabuleiro = cria_tabuleiro(POS_VAZIA)
jogadores = recebe_nomes()
PECAS = {"X", "O"}
imprimir_tabuleiro(tabuleiro, SETA_BAIXO, SETA_DIREITA)
for _ in pairs(jogadores) do
preenche_tabuleiro(tabuleiro, POS_VAZIA, PECAS[_], jogadores, _, ler_jogada(jogadores, _))
end