Reputation: 13
I am writing a tic tac toe game for my assignment, and I was stuck with the nested if statement. Attached part of my code below:
if name[1] == "AI" then
print("test string")
playerId = 1
if level == 1 or level == 2 then
print("AI lvl 1/2")
easy()
elseif level == 3 then
print("AI lvl 3")
hard()
end
elseif name[1] == "player" then
print("test string2")
Runtime:addEventListener("touch", fill)
end
while this executed, test string was printed on console, and playerId = 1, but inside 2nd if statement was completely ignored. Not even print on console. Could someone help me solve it please? Or tell me what is wrong with my code. Thanks
Upvotes: 1
Views: 237
Reputation:
Add something like print(level, type(level))
to see what level
is. Remember, lua can convert strings like '123'
to numbers and vice versa when you want to do some arithmetics or string concatenations, but you can't compare strings with numbers like that: 123 == '123'
will return false
.
So, if level
is a string, either replace 1
, 2
and 3
in your code with '1'
, '2'
and '3'
, or convert level
to a number: level = tonumber(level)
Upvotes: 3