Results 1 to 10 of 28

Thread: Sudoku solver program, do it like a human!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by audioinstinct View Post
    I just posted another reply and this time, it did warn me about moderation. It seems like I can post freely but if I include a picture in the post, a moderator has to approve it.
    Hmm that is strange. I will however forward this information to site administrator.

  2. #2
    Quote Originally Posted by SilverWarior View Post
    Hmm that is strange. I will however forward this information to site administrator.
    Thanks for that.

    Anyway, right now I'm thinking about a recursive approach to build the chain itself, a function would call itself for each candidate from 1-9 and build a chain of strong links which would return an array at the end which I could then easily check for possibilities. Well, it definitely sounds more simple than it is , but I think this will be most efficient way because with recursion I can ignore factors such as how many instances of the candidate exists and what's the relation of every single one of them to all the others which would just further complicate and add completely unnecessary loops into the procedure. Aaaand I need to step up my recursion capabilities anyway because I've never really written anything recursive harder than a factorial, lol.

  3. #3
    Ugh, this is a mess. I'm seriously considering that I'm rewriting the entire code in Object Pascal right now. I think I'm going to get used to that faster than I get around all the BS I encounter without it and I'm going to need it anyway when I want to create an interface for it. It will be million times easier to do the interface if I have to draw the properties of already existing objects, than to create new ones based on the tremendous amount of data, it would take eons to debug that.

  4. #4
    Quote Originally Posted by audioinstinct View Post
    It will be million times easier to do the interface if I have to draw the properties of already existing objects, than to create new ones based on the tremendous amount of data, it would take eons to debug that.
    I think that it will be easier to debug that is unless you don't create to big a mess with objects in the first place (yes you can do that).

    Anywhay if you will need any help with classes (class inheritence, method inheritance, property inheritance, getter and setter methods of properties, some data reusability with external classes and data forwarding) do let me know and I'll help as best as I can.

  5. #5
    Quote Originally Posted by SilverWarior View Post
    I think that it will be easier to debug that is unless you don't create to big a mess with objects in the first place (yes you can do that).

    Anywhay if you will need any help with classes (class inheritence, method inheritance, property inheritance, getter and setter methods of properties, some data reusability with external classes and data forwarding) do let me know and I'll help as best as I can.
    Well I've started to rewrite it, and now it fails at line 2. Weirdest error I've ever seen. Error code is 216 it says "program received signal sigsegv segmentation fault". I researched this and it seems to mean that my program is trying to refer to a memory address that it is not allowed to, but I have absolutely 0 idea why that is. Here's the code:

    Code:
    {$mode objfpc}
    
    {$m+}
    
    Program SudokuSolverObject;
    
    uses crt;
    
    
    type
    
     Cell=Class
    
      private
    
       Procedure Eliminate(cand:smallint);
    
       Procedure ReaarangeCandidates;
    
       Function BoxCalc(x,y:smallint):smallint;
    
    
    
      public
    
       x,y,box:smallint;
    
       boxnullx,boxnully:smallint;
    
       candidates: array[1..9] of smallint;
    
       num_of_candidates: smallint;
    
       solved:boolean;
    
       constructor create(a,b:smallint);
    
       procedure ClearRow(cand:smallint);
    
       procedure ClearColumn(cand:smallint);
    
       procedure ClearBox(cand:smallint);
    
       procedure Solve(cand:smallint);
    
     End;
    
    
    
    VAR
    
    Grid:array[1..9,1..9] of Cell;
    
    Completed:boolean;
    
    
    Constructor Cell.create(a,b:smallint);
    
    var
    
    i:smallint;
    
    Begin
    
     x:=a;
     y:=b;
    
     for i:=1 to 9 do
    
     begin
    
      candidates[i]:=i;
    
     end;
    
     box:=BoxCalc(a,b); //This function is defined before this in the code, I didn't include it because it's not related to the error at all
    
     num_of_candidates:=9;
    
     solved:=false;
    
    
     if box=1 then
    
     begin
    
      boxnullx:=1;
      boxnully:=1;
    
     end
     else
    
     if box=2 then
    
     begin
    
      boxnullx:=1;
      boxnully:=4;
    
     end
     else
    
     if box=3 then
    
     begin
    
      boxnullx:=1;
      boxnully:=7;
    
     end
     else
    
     if box=4 then
    
     begin
    
      boxnullx:=4;
      boxnully:=1;
    
     end
     else
    
     if box=5 then
    
     begin
    
      boxnullx:=4;
      boxnully:=4;
    
     end
     else
    
     if box=6 then
    
     begin
    
      boxnullx:=4;
      boxnully:=7;
    
     end
     else
    
     if box=7 then
    
     begin
    
      boxnullx:=7;
      boxnully:=1;
    
     end
     else
    
     if box=8 then
    
     begin
    
      boxnullx:=7;
      boxnully:=4;
    
     end
     else
    
     begin
    
      boxnullx:=7;
      boxnully:=7;
    
     end;
    
    End;
    
    Procedure Cell.Solve(cand:smallint);
    
    var
    
    i:smallint;
    
    Begin
    
     for i:=1 to 9 do
    
     begin
    
      candidates[i]:=0; 
    
    {This is the line that gives me the error, but all the other changes do it too, I've tried to switch the orders, any change
    
    to any variable of the classes after they were created throws me this stupid error. I've tried adding "self." before the assignments too, it didn't help.}
    
     end;
    
     num_of_candidates:=1;
     solved:=true;  
    
     candidates[1]:=cand;
    
    End;
    
    // I have all the other functions and procedures for the class defined as well, but they're not related to this problem.
    
    Procedure ReadGrid;
    
    var
    
    i,j:smallint;
    
    inp:string;
    
    act:smallint;
    
    err:smallint;
    
    Begin
    
     for i:=1 to 9 do
    
     begin
    
      Writeln('Please give me the clues of row # ',i, ' (0 means no clue)');
    
      Readln(inp);
    
      for j:=1 to 9 do
    
      begin
    
       val(inp[j],act,err);
    
       Grid[i,j].create(i,j); //This works just fine, there is no error yet
    
       if act<>0 then //"act" has a valid value, I double-checked it in debug mode, it works fine.
    
       begin
    
        Grid[i,j].Solve(act); //This is the point where the program enters the procedure which gives the error
    
       end;
    
      end;
    
     end;
    
    End;
    
    BEGIN
    
    Writeln('Welcome to Sudoku solver!');
    
    Readgrid;
    
    Readln;
    
    End.
    Edit: I've just tried to add a watch to certain points of the Grid before I get the error. It seems that the trouble is deeper than I thought. I get to the point where the class is created, and then it's all weird. If I set the watch like this: Grid[i,j].candidates then it says it is not a structure or union type, whatever the hell that means, and if I set the watch like this: Grid[i][j].candidates then, it tells me this "Can not access memory at 0xe". Wtf is going on here? P.S: I'm administrator on this computer, mine is the only user anyway, and it might be relevant, I'm using Windows 7 as OP system.

    Edit2: I've tried running this ridiculously easy program and it failed with the same BS so the problem is even deeper than I thought. Either I screw up the class definitions or it has to do something with the compiler...

    Code:
    {$mode objfpc}
    
    {$m+}
    
    Program wtf;
    
    
    type
    
    testclass=class
    
     private
    
      datx:integer;
    
     public
    
      dat1:integer;
    
      dat2:integer;
    
      constructor create(d1,d2,dx:integer);
    
     end;
    
    
    Constructor testclass.create(d1,d2,dx:integer);
    
    Begin
    
     datx:=dx;
     dat1:=d1;
     dat2:=d2;
    
    End;
    
    Var
    
    what:testclass;
    
    i,j,k:integer;
    
    Procedure Whatthef(a,b,c:integer);
    
    Begin
    
     what.create(a,b,c);
    
    End;
    
    BEGIN
    
    Writeln('Give me the first variable');
    
    Readln(i);
    
    Writeln('Give me the second');
    
    Readln(j);
    
    Writeln('Give me the third');
    
    Readln(k);
    
    Whatthef(i,j,k);
    
    Writeln('The first value was: ', what.dat1);
    
    Writeln('The second value was: ',what.dat2);
    
    Writeln('The third value was private');
    
    Readln;
    
    End.
    Last edited by audioinstinct; 04-01-2015 at 07:23 PM. Reason: addition

  6. #6
    The problem lies in the way you are creating your classes.
    Whenever you are creating classes you use next sytax:

    Code:
    //ClassRefereneVar is variable trhrough which you will be accessing your class
    //MyClass is the name of the class you are creating
    ClassReferenceVar := MyClass.Create;
    So in your case you need to call:

    Code:
    Grid[i,j] := Cell.create(i,j); //Right approach
    instead of:

    Code:
    Grid[i,j].create(i,j //Wrong approach
    EDIT: Pepole usually add T prefix to class names (TCell insted of just Cell) so that they can be quicly distinguished between normal variables in code.
    Last edited by SilverWarior; 04-01-2015 at 10:25 PM.

  7. #7
    Haha omg epic fail. Object Pascal 101, well thanks, I hate stupid mistakes like this, because I never guess the problem is something so simple. It runs fine now. I will continue to rewrite the original code with objects tomorrow, and see how it turns out. It's pretty weird though, pascal is always so strict with the syntax, and it allowed me to start the program like that. Basically this means the class it creates points to nothing, so it's no wonder it couldn't access the memory address, oh well, I guess I will get used to it, just takes some time.

Tags for this Thread

Bookmarks

Posting Permissions

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