View Single Post
  #1 (permalink)  
Old Oct 27th, 2007, 04:56
tox0tes tox0tes is offline
Reputable Member
Join Date: Jan 2004
Location: California
Age: 19
Posts: 232
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via AIM to tox0tes
Logic Problem involving loops and arrays

Hey everyone,
I am trying to create a sudoku solver in C. I am working on this one function that is supposed to go through each square and compare it to every other square, looking to see if the value in the square matches the value in any other square in its 3x3 grid, row, or column. puzzle[x][y] is the 2dimensional array containing each value that has been submitted. For example, puzzle[4][2] contains the value on column 4 and row 2...

The code i am having a problem with is right here, where I want to test the current square to see whether it conflicts with the value in another square in either its 3x3 grid, row, or column.

Code: Select all
if((x == a && y != b) || (y == b && x != a) || (!((x == a && y != b) || (y == b && x != a)) && ((int)(x / 3) == (int)(a / 3) && (int)(y / 3) == (int)(b / 3))))
The entire code is below.

Code: Select all
int validator (int puzzle[][9])
{
    int x;
    int y;
    int a;
    int b;
    
   for(y=0; y<9; y++)
    {
       for(x=0; x<9; x++)
        {
           if(puzzle[x][y] != '0')
            {
               for(b=0; b<9; b++)
                {
                   for(a=0; a<9; a++)
                    {
                     if((x == a && y != b) || (y == b && x != a) || (!((x == a && y != b) || (y == b && x != a)) && ((int)(x / 3) == (int)(a / 3) && (int)(y / 3) == (int)(b / 3))))
                  {  
                     return 0;
                  }
                      else
                      {
                         return 1;
                      }
                   }
                } 
            }
            else
            {
               return -1;
         }
        }
    }
   return 2;
}
Reply With Quote