Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,110 people online right now. Registration is fast and FREE... Join Now!




Sudoku Box Solver Dead-end

 
Reply to this topicStart new topic

Sudoku Box Solver Dead-end

andystyl
6 Dec, 2007 - 04:16 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 1


My Contributions
Hey,
been working on a Sudoku project for my university and im really stuck on the code for the 3x3 box check, i would be really appreciated if u could show me the way out of this, even the slightest help or clue would be greatly welcomed so i can continue my project:) thank u in advanced and here the code for the button "Done" Click Event

CODE
void __fastcall TMainFrm::DoneClick(TObject *Sender)
{
        bool found=false,number=false;
        int i,j,k;
        //checks whether there an input other then 1-9
        for (i=0;i<StringGrid1->ColCount;i++)
        {
                for(j=0;j<StringGrid1->RowCount;j++)
                {
                                if((StringGrid1->Cells[i][j]<1)||(StringGrid1->Cells[i][j]>9))
                                {
                                        MessageBox(NULL,"Input not between 1-9","Wrong Input",MB_OK);
                                        number=true;
                                        break;
                                }
                }
        }
        //checks wether there's a duplicate number at each row or column
        //number has to be within 1-9
        if(number==false)
        {
                for(k=0;k<StringGrid1->ColCount;k++)
                {
                        for (i=0;i<StringGrid1->ColCount;i++)
                        {
                                for(j=0;j<StringGrid1->RowCount;j++)
                                {
                                        if(StringGrid1->Cells[k][i]==StringGrid1->Cells[j+k+1][i])
                                                found=true;
                                        if(StringGrid1->Cells[j][k]==StringGrid1->Cells[j][i+k+1])
                                                found=true;
                                }
                        }
                }

//this is where the box check is supposed to be

                if(found)
                        MessageBox(NULL,"Theres a duplicate number!!!","WRONG",MB_OK);
                else
                {
                        ShowMessage("SUDOKU solved");
                        int res=MessageBox(NULL,"New Game? (No for exit)","NEW GAME?",MB_YESNOCANCEL);
                        if(res==IDNO)
                                Close();
                        if(res==IDYES)
                        {
                                StartClick(Sender);
                        }

                }
        }


}


This post has been edited by andystyl: 6 Dec, 2007 - 04:21 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Sudoku Box Solver Dead-end
6 Dec, 2007 - 05:23 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
There are three "paths" that intersect any give point on a sudoku board, row, column, and the 3x3 box. Validation involves two checks, one is whether the value is valid at all, which you're already checking. The other check is that the value is the only one of its kind in all three of it's paths.

I found the simplest way for me was to just make a check off matrix for all possibilities. I posted the code in snippets a while back: http://www.dreamincode.net/code/snippet1467.htm

The code I gave may not be the most intuitive, but the problem becomes surprisingly complex. The code for figuring out which box a cell belongs to should be of some help.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month