This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChiknNuggets's profile picture

Question on a method of doing something

Started by ChiknNuggets, 10 February 2013 - 04:09 AM
ChiknNuggets #1
Posted 10 February 2013 - 05:09 AM
So well i do love my sudoku, so im making a program to generate sudokus and eventually turn it into a playable version, anyway ive created a checker to see if it conflicts and it works fine, but im having trouble thinking of a way that if there is a conflict it tries again and again until it gets a value that will fit and if there are no values it will recreate that block again, but i cant exactly think of the code that i would use in there. If someone could suggest a way to do this(or another method if you think its better) and maybe give me an example it would be much appreciated. and here is my code so far http://pastebin.com/swSccwqj
ChiknNuggets #2
Posted 10 February 2013 - 05:27 AM
actually i want to kind of run it like this http://www.codeproject.com/KB/game/SudokuGen/AlgorithmTree.jpg
Orwell #3
Posted 10 February 2013 - 06:51 AM
I believe backtracking is the ideal solution to this.
ChiknNuggets #4
Posted 10 February 2013 - 02:56 PM
yea i was reading about backtracking, this page was very helpful http://www.codeproject.com/Articles/23206/Sudoku-Algorithm-Generates-a-Valid-Sudoku-in-0-018 but the problem is i cant think of exactly how i would really start in LUA, which is kinda the problem
Doyle3694 #5
Posted 10 February 2013 - 03:21 PM
Maybe

for i = 1,9 do
  if checkNumber(i) then -- Change checknumber to the name of your checker function
	 print(i)
	 break
  end
end
	
for checking 1 space sort of say. Then you could do that nine times and if any of them fails try again, maybe even adding a blacklist of numbers used in each space.
ChiknNuggets #6
Posted 10 February 2013 - 03:22 PM
no that wouldnt work because all it would do is go 1 then 2 then 3 and so forth through the puzzle because the first would choose one the second would choose two and so on
Doyle3694 #7
Posted 10 February 2013 - 03:24 PM
Check my edit. How advanced are you in lua? How long can you go about this yourself and how much do you need help with? Don't want to look like a idiot trying to help with things you already knew :P/>
ChiknNuggets #8
Posted 10 February 2013 - 03:34 PM
im not new but im definatly not an expert, like im fairly good at it but i code withweird methods…
Doyle3694 #9
Posted 10 February 2013 - 03:37 PM
Just running through your code, spotted a minor problem;

FilledField = EmptyField
Tables are actually pointers to each other in lua, so FilledField here would fill in to EmptyField as well. There is not a new table called FilledField that looks the same as EmptyField but just rather a variable which is called FilledField, however all changes in it will affect EmptyField
ChiknNuggets #10
Posted 10 February 2013 - 03:37 PM
and to your edit, ive got a function that does random number function that excludes numbers, and i think i just thought of a way to do this, it excludes all numbers in the row and column and the block
but the problem is when i hit a spot that doesnt work how do i make it go back, if that makes sense
oh damn, ahh well that doesnt really change much, cause i didnt actually have a use for the emptyblock yet
Doyle3694 #11
Posted 10 February 2013 - 03:45 PM
Oh, I see what you mean… That is a really good question. How I would do it, not saying it's the most effective way(I usually suck at effiency), would be checking thorugh, and if you get a fail just start over at the last "Confirmed OK" slot. Though, in the end, that would cause a stack overflow, so it's not really practical. You would have to ask a real pro for breaking a loop and calling a function, similar to

function lolrandom()
   return hello()
end

Though I see the uneffiency there. maybe a manual for loop, doing a managed while instead? so that you can back to whatever loop you want?
ChiknNuggets #12
Posted 10 February 2013 - 03:54 PM
Im thinking the best way(cause its the easiest….) is to just have it run in rows, so it creates first row(which never can get an error) then it does the second row, if it runs into an error it restarts that row
Orwell #13
Posted 10 February 2013 - 04:15 PM
Evaluating each square would probably be the easiest and I would really, really use backtracking. It's quite easy that way. You even have a nice C# example there (although I would keep track of occupied squares rather than available squares due to the lack of standard linked lists in Lua). I was looking into writing something up when I realized that this example fills every square in the Sudoku. Is that what you want and evenutally eliminate some squares afterwards, or do you want it to fill up just a number of the fields?
ChiknNuggets #14
Posted 10 February 2013 - 04:27 PM
i just dont understand how i would go back to a spot that makes it work….
ChiknNuggets #15
Posted 10 February 2013 - 07:07 PM
oh yea thats what i want, i want it to fill it out(like a fully sloved sudoku) and then ill start wring the program to dig the holes out(which means i need to make a solver) but at this step i just need to fill out the board, also i have a great pdf on making sudokus http://zhangroup.aporc.org/images/files/Paper_3485.pdf which says you cant make it without having a complete sudoku first because holes need to be dug in certain places to make it still possible.
Doyle3694 #16
Posted 11 February 2013 - 12:22 AM
If your only wish is to go back 1 step in a loop, as I said before, use a managed while loop:

a = 0
while a < 9 do
printinSpace(a,math.random(1,9))
if checknum() then
  a = a+1
else
  clearSpace()
end
end
Ofcourse it's just sample code, replace all functions and code inside with your actual code.
Then you can manage and go back to whatever loop you want. I have no experience with backtracking and it's a long page to read, but since it came from Orwell I can asure you that is a good solution to this problem too. Right now though, I think it's your turn to start coding and having fun :)/> And when you land on a problem, you can come here and we will gladly help you to solve it. I think going along, solving problems as they come is the easiest and fastest way to do this.
ChiknNuggets #17
Posted 11 February 2013 - 01:10 AM
i dont believe that this will actually work, because this basically just trys all the numbers until it reaches 9, thats not really the problem though, the problem is lets say the only number left in a line can be a 1 but there is allready a 1 in that block so what i need to do is go back a few spots until i get to the point that it can change, which is the part im having trouble with. infact In this link if you down to "When to backtrack" http://www.codeproject.com/Articles/23206/Sudoku-Algorithm-Generates-a-Valid-Sudoku-in-0-018 it gives a nice detailed explanation, and i never did much codin in vb.net so i dont understand the code shown below that well
ChiknNuggets #18
Posted 11 February 2013 - 02:12 AM
ok so im pushing my way through this as best as i can, and to prevent a stack overflow, im checking before we choose the number what numbers we can choose and then putting all the incompatible squares numbers in a table called "restricted" that will go into the random number exclusion process, anyway is there a simple script that would allow me to check if it has every number from 1 to 9 in it and if it does, it then calls break or something?
ChiknNuggets #19
Posted 11 February 2013 - 04:44 PM
ok so if anyone is still reading this, ive gotten to the point where it makes the whole grid and wont put in a number if it doesnt work, but instead puts in a 0(which is where i need to backtrack at) anyway i was wondering if anyone was able to understand that page enough to get how to backtrack? also here is the updated code http://pastebin.com/swSccwqj
Orwell #20
Posted 13 February 2013 - 12:18 AM
ChiknNuggets PM'd me asking me if I gave this a try, so I did so today. :P/> Here is what I made: http://pastebin.com/sicaGw5d
I tried making it robust and a bit modular, but still easy to understand. It's also commented like hell. You are free to use it as you like without any restrictions. Especially you, ChiknNuggets. :)/> It basically implements a Sudoku metatable that creates a random valid Sudoku. The only important methods in usage are Sudoku.generate() and Sudoku.print( sudoku ).