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.
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.
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’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.
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.
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.
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?
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.
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.