diff --git a/applications/sudoku/sudokumain.pas b/applications/sudoku/sudokumain.pas index 39212b6ef..b30a22923 100644 --- a/applications/sudoku/sudokumain.pas +++ b/applications/sudoku/sudokumain.pas @@ -149,9 +149,9 @@ procedure TForm1.SGridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); 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 - theValues[ACol + 1, ARow + 1] := ' '; + theValues[ACol + 1, ARow + 1] := 0; end; end; @@ -159,17 +159,15 @@ function TForm1.SolveSudoku: Boolean; var aSudoku: TSudoku; Col, Row: Integer; - Steps: Integer; + Steps, AValue: Integer; begin + theValues := Default(TValues); //initialize all to zero for Col := 0 to 8 do begin for Row := 0 to 8 do begin if Length(SGrid.Cells[Col, Row]) >= 1 then begin - theValues[Col + 1, Row + 1] := SGrid.Cells[Col, Row][1]; - end - else - begin - theValues[Col + 1, Row + 1] := ' '; + if TryStrToInt(SGrid.Cells[Col, Row][1], AValue) then + theValues[Col + 1, Row + 1] := AValue; end; end; end; @@ -192,7 +190,7 @@ begin begin for Row := 0 to 8 do begin - Ch := theValues[Col + 1, Row + 1]; + Ch := IntToStr(theValues[Col + 1, Row + 1])[1]; if Ch = '0' then Ch := #32; SGrid.Cells[Col, Row] := Ch; diff --git a/applications/sudoku/sudokutype.pas b/applications/sudoku/sudokutype.pas index 00417157f..2d9814ae6 100644 --- a/applications/sudoku/sudokutype.pas +++ b/applications/sudoku/sudokutype.pas @@ -32,11 +32,11 @@ uses type Digits = set of 1..9; 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. DigitsPossible: Digits; end; - TValues = Array[1..9,1..9] of char; + TValues = Array[1..9,1..9] of Integer; { TSudoku } @@ -82,13 +82,13 @@ var begin for c := 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].Value := Values[c, r]; - Grid[c, r].DigitsPossible := [StrToInt(Values[c, r])]; + Grid[c, r].DigitsPossible := [(Values[c, r])]; end else begin 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]; end; end; @@ -140,7 +140,7 @@ begin for r := 1 to 9 do begin if Grid[c, r].Locked then Continue; 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; end; end; @@ -154,7 +154,7 @@ begin for i := 1 to 9 do begin if i = r then continue; 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; @@ -166,7 +166,7 @@ begin for i := 1 to 9 do begin if i = c then continue; 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; @@ -179,7 +179,7 @@ begin for j := cmin[r] to cmax[r] do begin if not ((i = c) and (j = r)) then 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;