Originally Posted by
SilverWarior
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.
Bookmarks