New to Sudoku, but wrote something that might be of intrest

Programs which generate, solve, and analyze Sudoku puzzles

New to Sudoku, but wrote something that might be of intrest

Postby OgLeM » Fri Sep 23, 2005 4:06 pm

Greetings,

While burning a dvd I got bored and decided to read the paper, while reading I found the Sudoku problem, begin the first time I had encounter Sudoku I got the urge to solve it.

Now I must admit my background is in computers so I did it the way I usally do, write a program and by the time I had finished the 4th dvd I had a working program.

Now I understand this may be not the way others solve the problem or they may disagree with my methods. I figured Id post it for others to play with.

main.cpp

Code: Select all
#include "Sudoku.h"

#include <stdio.h>

int main()
{
   c_Sudoku Sudoku;

   Sudoku.Dump();
   Sudoku.AddKnowledge(2,0,5);
   Sudoku.Dump();
   Sudoku.AddKnowledge(3,0,9);
   Sudoku.Dump();
   Sudoku.AddKnowledge(5,0,6);
   Sudoku.Dump();
   Sudoku.AddKnowledge(7,0,7);
   Sudoku.Dump();

   Sudoku.AddKnowledge(0,1,4);
   Sudoku.Dump();
   Sudoku.AddKnowledge(1,1,3);
   Sudoku.Dump();
   Sudoku.AddKnowledge(5,1,7);
   Sudoku.Dump();
   Sudoku.AddKnowledge(6,1,2);
   Sudoku.Dump();
   Sudoku.AddKnowledge(7,1,9);
   Sudoku.Dump();

   Sudoku.AddKnowledge(1,2,6);
   Sudoku.Dump();
   Sudoku.AddKnowledge(4,2,1);
   Sudoku.Dump();
   Sudoku.AddKnowledge(8,2,8);
   Sudoku.Dump();

   Sudoku.AddKnowledge(0,3,8);
   Sudoku.Dump();
   Sudoku.AddKnowledge(1,3,2);
   Sudoku.Dump();
   Sudoku.AddKnowledge(3,3,1);
   Sudoku.Dump();
   Sudoku.AddKnowledge(5,3,4);
   Sudoku.Dump();
   Sudoku.AddKnowledge(8,3,7);
   Sudoku.Dump();

   Sudoku.AddKnowledge(2,4,6);
   Sudoku.Dump();
   Sudoku.AddKnowledge(6,4,9);
   Sudoku.Dump();

   Sudoku.AddKnowledge(0,5,1);
   Sudoku.Dump();
   Sudoku.AddKnowledge(3,5,7);
   Sudoku.Dump();
   Sudoku.AddKnowledge(5,5,5);
   Sudoku.Dump();
   Sudoku.AddKnowledge(7,5,3);
   Sudoku.Dump();
   Sudoku.AddKnowledge(8,5,2);
   Sudoku.Dump();

   Sudoku.AddKnowledge(0,6,6);
   Sudoku.Dump();
   Sudoku.AddKnowledge(4,6,5);
   Sudoku.Dump();
   Sudoku.AddKnowledge(7,6,8);
   Sudoku.Dump();

   Sudoku.AddKnowledge(1,7,5);
   Sudoku.Dump();
   Sudoku.AddKnowledge(2,7,7);
   Sudoku.Dump();
   Sudoku.AddKnowledge(3,7,8);
   Sudoku.Dump();
   Sudoku.AddKnowledge(7,7,4);
   Sudoku.Dump();
   Sudoku.AddKnowledge(8,7,9);
   Sudoku.Dump();

   Sudoku.AddKnowledge(1,8,4);
   Sudoku.Dump();
   Sudoku.AddKnowledge(3,8,6);
   Sudoku.Dump();
   Sudoku.AddKnowledge(5,8,3);
   Sudoku.Dump();
   Sudoku.AddKnowledge(6,8,1);
   Sudoku.Dump();
//*/
   char Quit =1;
   while(Quit)
   {
      Sudoku.Check();
      Sudoku.Dump();
     
      scanf("%d",&Quit);
   }
         

   return 1;
}


Sudoku.cpp

Code: Select all
#include "Sudoku.h"

#include <stdio.h>

c_Sudoku::c_Sudoku()
{
   int i,j,k;
   for(i=0;i<9;i++)
      for(j=0;j<9;j++)
         for(k=0;k<9;k++)
            Board[i][j][k] = k+'1';
}


void c_Sudoku::AddKnowledge(char PosX,char PosY,char Item)
{
   UpdateSector(PosX,PosY,Item);
   UpdateRow(PosX,PosY,Item);
   UpdateCol(PosX,PosY,Item);
   SetCell(PosX,PosY,Item);
}

void c_Sudoku::Check()
{
   for(int i=0;i<9;i++)
      for(int j=0;j<9;j++)
      {
         int Count = 0;
         char item = 0;
         for(int k=0;k<9;k++)
            if(Board[i][j][k] != ' ')
            {
               Count++;
               item = k + 1;
            }

         if(Count == 1)
         {
            AddKnowledge(i,j,item);
            Dump();
         }
      }
}

void c_Sudoku::Dump()
{

   int i,j,k;
   for(i=0;i<9;i++)
   {
      for(j=0;j<9;j++)
      {
         if(j!=0)
            printf("\t|");
         else
            printf("|");
         for(k=0;k<9;k++)
            if(Board[j][i][k] != ' ')
               printf("%c",Board[j][i][k]);
      }
      printf("|\n");
   }
   printf("\n");
}

void c_Sudoku::SetCell(char PosX,char PosY,char Item)
{
   for(int i=0;i<9;i++)
      if(i != Item-1)
         Board[PosX][PosY][i] = ' ';
      else
         Board[PosX][PosY][i] = '0'+Item;

}

void c_Sudoku::UpdateSector(char PosX,char PosY,char Item)
{
   int i,j;
   PosX = (PosX) / 3;
   PosY = (PosY) / 3;

   for(i=PosX*3;i<(PosX*3)+3;i++)
      for(j=PosY*3;j<(PosY*3)+3;j++)
         Board[i][j][Item-1] = ' ';

}

void c_Sudoku::UpdateRow(char PosX,char PosY,char Item)
{
   for(int i=0;i<9;i++)
      Board[i][PosY][Item-1] = ' ';
}

void c_Sudoku::UpdateCol(char PosX,char PosY,char Item)
{
   for(int i=0;i<9;i++)
      Board[PosX][i][Item-1] = ' ';
}


Sudoku.h

Code: Select all
#ifndef __INC_FILE_Sudoku__
#define __INC_FILE_Sudoku__

class c_Sudoku
{
   public:
      c_Sudoku();

      void AddKnowledge(char PosX,char PosY,char Item);

      void Check();

      void Dump();
   protected:
     
      void SetCell(char PosX,char PosY,char Item);
      void UpdateSector(char PosX,char PosY,char Item);
      void UpdateRow(char PosX,char PosY,char Item);
      void UpdateCol(char PosX,char PosY,char Item);

      char Board[9][9][9];


};

#endif


Anyway have fun people,

Peter Beardsley[/quote]
OgLeM
 
Posts: 1
Joined: 23 September 2005

Postby stuartn » Fri Sep 23, 2005 6:25 pm

yes yes - splendid. And the logic?... couldn't quite see it.

stuartn
stuartn
 
Posts: 211
Joined: 18 June 2005

Postby ovaltrade » Sun Sep 25, 2005 2:37 pm

It seams to me this logic copes with a single long chain strategy and would fail when there are more than 1 chain. I don't see any recursivity.

Try to solve this with your method:

xxx x7x 94x
x7x x9x xx5
3xx xx5 x7x

x87 4xx 1xx
463 xxx xxx
xxx xx7 x8x

8xx 7xx xxx
7xx xxx x28
x5x 268 xxx

There is a unique solution.
ovaltrade
 
Posts: 19
Joined: 03 September 2005


Return to Software