Sudoku: make TSudoku internally work with integers, not characters.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7229 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
lazarus-bart
2020-01-04 17:39:25 +00:00
parent e1d784ff8a
commit 7f3b147fc1
2 changed files with 16 additions and 18 deletions

View File

@ -149,9 +149,9 @@ procedure TForm1.SGridSetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: string); const Value: string);
begin begin
if (Length(Value) >= 1) and (Value[1] in ['1'..'9']) then begin if (Length(Value) >= 1) and (Value[1] in ['1'..'9']) then begin
theValues[ACol + 1, ARow + 1] := Value[1]; theValues[ACol + 1, ARow + 1] := StrToInt(Value[1]);
end else begin end else begin
theValues[ACol + 1, ARow + 1] := ' '; theValues[ACol + 1, ARow + 1] := 0;
end; end;
end; end;
@ -159,17 +159,15 @@ function TForm1.SolveSudoku: Boolean;
var var
aSudoku: TSudoku; aSudoku: TSudoku;
Col, Row: Integer; Col, Row: Integer;
Steps: Integer; Steps, AValue: Integer;
begin begin
theValues := Default(TValues); //initialize all to zero
for Col := 0 to 8 do begin for Col := 0 to 8 do begin
for Row := 0 to 8 do begin for Row := 0 to 8 do begin
if Length(SGrid.Cells[Col, Row]) >= 1 then if Length(SGrid.Cells[Col, Row]) >= 1 then
begin begin
theValues[Col + 1, Row + 1] := SGrid.Cells[Col, Row][1]; if TryStrToInt(SGrid.Cells[Col, Row][1], AValue) then
end theValues[Col + 1, Row + 1] := AValue;
else
begin
theValues[Col + 1, Row + 1] := ' ';
end; end;
end; end;
end; end;
@ -192,7 +190,7 @@ begin
begin begin
for Row := 0 to 8 do for Row := 0 to 8 do
begin begin
Ch := theValues[Col + 1, Row + 1]; Ch := IntToStr(theValues[Col + 1, Row + 1])[1];
if Ch = '0' then if Ch = '0' then
Ch := #32; Ch := #32;
SGrid.Cells[Col, Row] := Ch; SGrid.Cells[Col, Row] := Ch;

View File

@ -32,11 +32,11 @@ uses
type type
Digits = set of 1..9; Digits = set of 1..9;
TSquare = record TSquare = record
Value: Char; // The value of this square. Value: Integer; // The value of this square.
Locked: Boolean; // Wether or not the value is known. Locked: Boolean; // Wether or not the value is known.
DigitsPossible: Digits; DigitsPossible: Digits;
end; end;
TValues = Array[1..9,1..9] of char; TValues = Array[1..9,1..9] of Integer;
{ TSudoku } { TSudoku }
@ -82,13 +82,13 @@ var
begin begin
for c := 1 to 9 do begin for c := 1 to 9 do begin
for r := 1 to 9 do begin for r := 1 to 9 do begin
if Values[c, r] in ['1'..'9'] then begin if Values[c, r] in [1..9] then begin
Grid[c, r].Locked := True; Grid[c, r].Locked := True;
Grid[c, r].Value := Values[c, r]; Grid[c, r].Value := Values[c, r];
Grid[c, r].DigitsPossible := [StrToInt(Values[c, r])]; Grid[c, r].DigitsPossible := [(Values[c, r])];
end else begin end else begin
Grid[c, r].Locked := False; Grid[c, r].Locked := False;
Grid[c, r].Value := '0'; Grid[c, r].Value := 0;
Grid[c, r].DigitsPossible := [1, 2, 3, 4, 5, 6, 7, 8, 9]; Grid[c, r].DigitsPossible := [1, 2, 3, 4, 5, 6, 7, 8, 9];
end; end;
end; end;
@ -140,7 +140,7 @@ begin
for r := 1 to 9 do begin for r := 1 to 9 do begin
if Grid[c, r].Locked then Continue; if Grid[c, r].Locked then Continue;
if CountSetMembers(Grid[c, r].DigitsPossible, Value) = 1 then begin if CountSetMembers(Grid[c, r].DigitsPossible, Value) = 1 then begin
Grid[c, r].Value := IntToStr(Value)[1]; Grid[c, r].Value := Value;
Grid[c, r].Locked := True; Grid[c, r].Locked := True;
end; end;
end; end;
@ -154,7 +154,7 @@ begin
for i := 1 to 9 do begin for i := 1 to 9 do begin
if i = r then continue; if i = r then continue;
for d := 1 to 9 do begin for d := 1 to 9 do begin
if StrToInt(Grid[c, i].Value) = d then exclude(Grid[c, r].DigitsPossible, d); if Grid[c, i].Value = d then exclude(Grid[c, r].DigitsPossible, d);
end; end;
end; end;
end; end;
@ -166,7 +166,7 @@ begin
for i := 1 to 9 do begin for i := 1 to 9 do begin
if i = c then continue; if i = c then continue;
for d := 1 to 9 do begin for d := 1 to 9 do begin
if StrToInt(Grid[i, r].Value) = d then exclude(Grid[c, r].DigitsPossible, d); if Grid[i, r].Value = d then exclude(Grid[c, r].DigitsPossible, d);
end; end;
end; end;
end; end;
@ -179,7 +179,7 @@ begin
for j := cmin[r] to cmax[r] do begin for j := cmin[r] to cmax[r] do begin
if not ((i = c) and (j = r)) then begin if not ((i = c) and (j = r)) then begin
for d := 1 to 9 do begin for d := 1 to 9 do begin
if StrToInt(Grid[i, j].Value) = d then exclude(Grid[c, r].DigitsPossible, d); if Grid[i, j].Value = d then exclude(Grid[c, r].DigitsPossible, d);
end; end;
end; end;
end; end;