This is a part of my code for a Naughts and Crosses (tic-tac-toe) game.
positions = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
# Returns .. 1 = Square already owned, 2 = Blank square, 0 = Enemy square
def check_square(side, square)
if positions[square] == side
state = 1
elsif positions[square] == B
state = 2
else
state = 0
end
return state
end
When I run the program I get the error:
in `check_square': undefined local variable or method `positions' for main:Object (NameError)
However it is literally defined right above it. I have ran the snippet of code in its own .rb and it works fine so I don't see why it doesn't work. I must assume it has to do with the scope of positions but, for me at least (beginner programmer), I don't see why it doesn't work here but does in its own program.
Any help is gladly appreciated.