need help in lua

  1. you’re right. have the wrong delimiter “;” used.
  2. if p = minVal and random(10) <5 // take it… or not.
  3. minVal = p “;” minD   = m
  4. end
  5. if p < minVal 
  6. minVal = p “;” minD   = m
  7. end  

the correction is here:

minVal = p

minD = m

end

if p < minVal

minVal = p

minD = m

I do not want to get involved with polemik this time.

It only costs time for you and me too.

thank you for looking ring.

it works realy perfect.

I agree with Dave here.  You came in with a question not related to game development in any way.  This would normally be an instant, “Can’t help!”   This isn’t a general help forums.  It is for game developers working with Corona.

However, I’m a sucker for question involving algorithms so I gave it a look.  That said, I hate it when people ask questions and post code that is impossible to read.  I always suggest, ‘provide a full project I can download and run’.  I also ask that the OP provide clear and legible code.  It irks me to have to put in more time examining the code that the OP did asking the question.

Again, Dave is right.  Help us to help you and you’ll be much better off.  I’m waiting to see a copy of this code in the language you programming language copied it from.  That way we can compare the ‘original’ to the ‘converted Lua’ version.

I’d also love to know what ‘does not run properly’ means.

hello roaminggamer

Agreed. I wonder how I was going to react in such situarion in her place. I want to say thank you very much for being patient with me.

I am glad that you are interested in my solution. the algorithm is relatively short (compared to others), clearly understandable and functions really well. we can say perfect because, as we both know, the warnsdorff’s rule is not always successful. It often happens that the choice of the best suitable field leads to the dead end.

the part with the random choice distinguishes the solution. we do not always take the first optimal field (in case of equality) but throw a “coin”. this makes the knight’s path more flexible.

Since I’m still looking for the solution, I need several versions side by side. this is not good with corona (or I do not know how). in the zb-editor I can compare that better. there it does not need the “main” part. therefore no project.

I posted the ring code. for the negligence with the delimiter I’m sorry. 

The phrase “not working properly” should mean better: does not deliver the expected results.

Please do not forget that I have to adapt every sentence I write in English more often in the translator. Sometimes things are different than expected. 

have nice weekend.

hi aficionados

I can not believe it yet … but: IT’S ON !

am still testing and clean up.

but it does and it does as good as the RING.

perfect.   

have nice weekend.

@neskuk,

Hi.  Welcome to the forums. 

Are you interested in using Corona or just learning Lua?   

If the latter, please be aware, while this sub-forum is for Lua questions it is intended for members of the Corona game and app dev community. 

So, if you’re just learning Lua you may want to find a Lua specific community or a general programming resource.

Whatever the case, there are many Lua resources on the web, but among the best is the PIL: https://www.lua.org/pil/

Also, you can find implementations of many algorithms over at Rosseta Code: https://rosettacode.org/

For example Knights Tour (you misspelled it):

  • Thanks for the answer.
  • the example in rosetacode ( https://rosettacode.org/wiki/Knight%27s_tour#Lua ), the only one found in lua) is unfortunately recursive. it works acceptable only from start field: 1/1 - not a good example unfortunately.
  • yes, I want to learn lua - lua with corona! 
  • in lua fourth edition is unfortunately nothing usable for my problem with knigtht’s tour.

I’m still not clear what the exact problem is.

How is your code failing?

i’ve no interest in debugging that for you, but i’ll offer this:  one of the more common mistakes converting code from other languages to lua is that numeric array indices are 1-based (not 0-based) by default

while your “for” loops look like they’ve been revised, you must also think about how that change to indexing might affect other calculations, like a modulo or divide on a linear index to extract row/col, or the converse of that, or any other use that implies a 0-basis.

hi roaming gamer

my lua code is strange. it does not work as it should. i have tested it in several other languages and it works perfectly. only in lua not.

I’m pretty irritated. To make sure that I do not fall into the 1-base trap, I rewrote it in ring (1-base) and here it runs perfectly.

it has to hang on some property of lua. I do not know lua yet. I’m learning it now.

look at the code in the iterpreter. then you will see it. I suspect the error in the random part or in the formation of the priority queue. I just do not know how to locate.

hi davebollinger

it does not work as it should. i have tested it in several other languages and it works perfectly. only in lua not.

I’m pretty irritated. To make sure that I do not fall into the 1-base trap, I rewrote it in ring (1-base) and here it runs perfectly.

it has to hang on some property of lua (float / integer / maybe the loops?). I do not know lua yet. I’m learning it now.

I suspect the error in the random part or in the formation of the priority queue.  I just do not know how to locate.

look at the code in the iterpreter. then you will see what i mean. 

Debugging a knights tour code is not trivial. I know what I’m talking about.

:frowning:

the problem is tricky, but the code itself isn’t (or needn’t be)

what’s “hard” is trying to puzzle out someone else’s translation of it for bugs that even the author can’t see.

(fe, i too once wrote a knight’s tour, in lua 3 circa 2000, but it doesn’t look like this one - except perhaps the “pm” table :D)

I don’t see anything wrong with the Lua code itself.  I’d clean that up with better indentation and then re-check your algorithm.

2018 me is hating all those 1-2 character variable names, while remembering 2012 me wouldn’t have even bothered commenting them…

hi dave

no idea what you are talking about.

if you mean that example in rosetta (https://rosettacode.org/wiki/Knight%27s_tour#Lua), the only lua example for kt in lua that you can find on the web, that’s is it not. one could call it unusable. 

hi roaminggamer

look at the code in the interpreter. you will see what I mean. the pgm is totally illogical … unpredictable. the same code in ring or python (for example) works perfectly. but not in lua. Why?

thanks Nick Sherman

but a statement on the matter would be more useful.

I ran the code (with a fixed start and random starts) and no errors popped out.

Yes, the random start version failed, but the were no syntax errors, crashes, or hangs. 

Beyond that, I’m not going to debug the actual algorithm. 

Sorry, but it’s been 20+ years since I had to solve Knight’s Tour in school and this is really not a Corona issue which is where I prefer to put my help time.

Here is the original and a cleaned/modified copy of the code for anyone who has time to help:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/knightsTour.zip

To run the orignal, do this in main.lua:

require "orig"

To Run the modified do this:

local cleaned = require "cleaned" cleaned.run()

or this to run a specific start:

local cleaned = require "cleaned" cleaned.run( { kx = 2, ky = 3 } )

or this to run them all and not print result:

local cleaned = require "cleaned" for kx = 1, 8 do for ky = 1, 8 do cleaned.run( { kx = kx, ky = ky, quiet = true } ) end end

post the code that works in some other language, then maybe, MAYBE, someone might then take an interest in helping translate it

because that’s likely the only source of your problem, but i doubt anyone will figure it out as is

this translated code apparently has an error, though at least one person (roaminggamer) ran it without error, and you refuse to tell us exactly what the error actually IS (instead you just keep saying “same code runs in other languages” - which can’t be true, this MUST have been translated)

plus there’s lots of “mystery stuff” in there, like passing two values to randomseed, or “math.random()<5” which will always evaluate true, or indexing by 10’s when you only have 8 col/row, plus the few comments present are in german (not my native), obscure variable names, etc – maybe it works, maybe it doesn’t, but it’s just too weird for most of us casual helpers to spend enough time on to figure it out for you.