Grayson Cunha Grayson Cunha - 3 months ago 11
Ruby Question

Ruby syntax error I am newbie in this programming language

I have one problem in my code in the ruby language. This code was in c sharp and i am converting the code of C# for Ruby. I am studying Ruby through of tutorialspoint but my code has produced some errors. Anyone can help me?

The error are:
KnightsTour.rb:57 syntax error, unexpected keyword_else, expecting keyword_end
KnightsTour.rb:65 syntax error, unexpected end-of-input, expecting keyword_end

class KnightsTour

def initialize(numero)
@num = numero
@numSqr = num*num
@table = Array.new(num){Array.new(num)}
@dx = [2 , 1 , -1, -2, -2, -1, 1 , 2 ]
@dy = [1 , 2 , 2 , 1 , -1, -2, -2, -1]
end

def isAcceptable(x, y)
result = (x >= 0 && x <= num - 1)
result = result && (y >= 0 && y <= num - 1)
result = result && (table[x, y] == 0)
return result
end

def tryMove(i, x, y)
done = ( i > numSqr ) #Verifica a qtd de movimentos
k = 0;
while !done && k < 8 do
u = x + dx.at(k)
v = y + dy.at(k)
end

if isAcceptable u,v
table[u , v] = i
# ==> tenta outro movimento
done = tryMove i + 1 , u , v
if !done
#sem sucesso . Descarta movimento
table[u , v] = 0;
end
end

k = k + 1 #passa ao próximo movimento possível

return done
end


def showTour (x , y)
$i = 0
$j = 0

table [x][y] = 1
done = tryMove 2 , x , y
if done do
for i in 0...num
for j in j...num
puts (table [i , j] + " ")
#$j += 1
end
puts("\n")
#$i += 1
end
else
puts ("Não há passeio possível")
end
end

end

cavalo = KnightsTour.new("7")
cavalo.showTour 0,0

Answer

Remove keyword do after if:

It should be

if done
    for i in 0...num
        for j in j...num 
            puts (table [i , j] + " ")
            #$j += 1
        end
        puts("\n")
        #$i += 1
    end
else
    puts ("Não há passeio possível")
end