CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Threaded View

  1. #18
    Join Date
    Apr 2009
    Posts
    21

    Re: Sudoku solving algorithm

    I found that, after I fixed the syntax error (blame Game Maker for that one, they allow you to do something like "if(x=2)" in their scripting), the function almost works correctly. To be more specific, the horizontal and vertical checks work fine, but not the square check. I know this because I did 3 things:
    1. I did the sudoku by hand to find what numbers go where.
    2. I used 'dispothermatrix()' to look at smatrix, and I did by hand what I want the program to do (I myself looked at rows, columns, and boxes, to find 0s that were standing alone) and I found that all of these were the correct numbers as checked by the sudoku I had completed.
    3. Within 'if (correctnum==true)' I added something that would tell me the coordinates and the numbers of the spots that passed the tests, and which test it passed (horizontal, vertical, or square). Some of these matched my hand-solved sudoku, some didn't. I found that the ones that did match were the ones that passed either the horizontal or the vertical tests, and the ones that didn't match were the ones that passed the square test.

    Thus, my problem should be in the square test. However, as far as checking every spot in the square, the programming is exactly the same as the one to set smatrix, which works fine. Also, as far as the test, it is the same as the horizontal and vertical tests.

    The code to set the squares in smatrix (working)
    Code:
    int cx=x-((x-1)%3),
    	cy=y-((y-1)%3);
    
    for(int e=0;e<3;e++)
    	for(int i=0;i<3;i++)
    		smatrix[cx+e][cy+i][currentnum]=1;
    The code to check horizontal (working)
    Code:
    if (correctnum == false)
    {
    	correctnum = true;
    	lastpassed="horizontal";
    
    	for(int n=1;n<10;n++)
    		if(x!=n)
    			if(smatrix[n][y][z]==0)
    				correctnum=false;
    The code to check squares (not working)
    Code:
    int cx=x-((x-1)%3),
    	cy=y-((y-1)%3);
    if (correctnum == false)
    {
    	correctnum = true;
    	lastpassed="square";
    
    	for(int e=0;e<3;e++)
    		for(int i=0;i<3;i++)
    
    			if(cx+e != x && cy+i != y)
    				if(smatrix[cx+e][cy+i][z]==0)
    					correctnum=false;
    For example, compile and run the attachment. Note the first 4 lines after the initially displayed sudoku.
    1 at (8,3) passed: square
    1 at (9,3) passed: square
    cheat!!!! (8,3) = (9,3)
    cheat!!!! (9,3) = (8,3)
    This should not have passed the third test for the square, but it did.
    Can someone tell me why this is not working, while the others are?

    Thanks
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured