You've already forked lazarus-ccr
Sudoku: give better message if sudoku isn't completely solved.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7224 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -28,7 +28,7 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids,
|
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids,
|
||||||
Buttons, StdCtrls, sudokutype;
|
Buttons, StdCtrls, SudokuType;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
@ -47,7 +47,8 @@ type
|
|||||||
private
|
private
|
||||||
{ private declarations }
|
{ private declarations }
|
||||||
theValues: TValues;
|
theValues: TValues;
|
||||||
procedure SolveSudoku;
|
function SolveSudoku: Boolean;
|
||||||
|
procedure ShowSolution;
|
||||||
public
|
public
|
||||||
{ public declarations }
|
{ public declarations }
|
||||||
end;
|
end;
|
||||||
@ -73,19 +74,10 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TForm1.ButtonSolveClick(Sender: TObject);
|
procedure TForm1.ButtonSolveClick(Sender: TObject);
|
||||||
var
|
|
||||||
c, r: Integer;
|
|
||||||
begin
|
begin
|
||||||
StringGrid1.Options := StringGrid1.Options - [goEditing];
|
StringGrid1.Options := StringGrid1.Options - [goEditing];
|
||||||
SolveSudoku;
|
SolveSudoku;
|
||||||
StringGrid1.Clean;
|
ShowSolution;
|
||||||
for c := 1 to 9 do begin
|
|
||||||
for r := 1 to 9 do begin
|
|
||||||
StringGrid1.Cells[c - 1, r - 1] := theValues[c, r];
|
|
||||||
if StringGrid1.Cells[c - 1, r - 1] = '0' then
|
|
||||||
StringGrid1.Cells[c - 1, r - 1] := ' ';
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -123,25 +115,48 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TForm1.SolveSudoku;
|
function TForm1.SolveSudoku: Boolean;
|
||||||
var
|
var
|
||||||
aSudoku: TSudoku;
|
aSudoku: TSudoku;
|
||||||
c, r: Integer;
|
Col, Row: Integer;
|
||||||
Stappen: Integer;
|
Steps: Integer;
|
||||||
begin
|
begin
|
||||||
for c := 0 to 8 do begin
|
for Col := 0 to 8 do begin
|
||||||
for r := 0 to 8 do begin
|
for Row := 0 to 8 do begin
|
||||||
if Length(StringGrid1.Cells[c, r]) >= 1 then begin
|
if Length(StringGrid1.Cells[Col, Row]) >= 1 then
|
||||||
theValues[c + 1, r + 1] := StringGrid1.Cells[c, r][1];
|
begin
|
||||||
end else begin
|
theValues[Col + 1, Row + 1] := StringGrid1.Cells[Col, Row][1];
|
||||||
theValues[c + 1, r + 1] := ' ';
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
theValues[Col + 1, Row + 1] := ' ';
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
aSudoku := TSudoku.Create;
|
aSudoku := TSudoku.Create;
|
||||||
Stappen := aSudoku.GiveSolution(theValues);
|
Result := aSudoku.GiveSolution(theValues, Steps);
|
||||||
aSudoku.Free;
|
aSudoku.Free;
|
||||||
ShowMessage('Sudoku solved in ' + IntToStr(Stappen) + ' steps.');
|
if Result then
|
||||||
|
ShowMessage(Format('Sudoku solved in %d steps.', [Steps]))
|
||||||
|
else
|
||||||
|
ShowMessage(Format('Unable to completely solve sudoku (tried %d steps).',[Steps]));
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.ShowSolution;
|
||||||
|
var
|
||||||
|
Col, Row: Integer;
|
||||||
|
Ch: Char;
|
||||||
|
begin
|
||||||
|
for Col := 0 to 8 do
|
||||||
|
begin
|
||||||
|
for Row := 0 to 8 do
|
||||||
|
begin
|
||||||
|
Ch := theValues[Col + 1, Row + 1];
|
||||||
|
if Ch = '0' then
|
||||||
|
Ch := #32;
|
||||||
|
StringGrid1.Cells[Col, Row] := Ch;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
unit sudokutype;
|
unit SudokuType;
|
||||||
|
|
||||||
{
|
{
|
||||||
***************************************************************************
|
***************************************************************************
|
||||||
@ -41,7 +41,7 @@ type
|
|||||||
{ TSudoku }
|
{ TSudoku }
|
||||||
|
|
||||||
TSudoku = class(TObject)
|
TSudoku = class(TObject)
|
||||||
function GiveSolution(var Values: TValues): Integer;
|
function GiveSolution(var Values: TValues; out ASteps: Integer): Boolean;
|
||||||
private
|
private
|
||||||
Grid : Array[1..9, 1..9] of TSquare;
|
Grid : Array[1..9, 1..9] of TSquare;
|
||||||
Steps: Integer;
|
Steps: Integer;
|
||||||
@ -51,7 +51,7 @@ type
|
|||||||
procedure CheckBlock(c, r: Integer);
|
procedure CheckBlock(c, r: Integer);
|
||||||
procedure CheckDigits(d: Integer);
|
procedure CheckDigits(d: Integer);
|
||||||
procedure Fill(Values: TValues);
|
procedure Fill(Values: TValues);
|
||||||
procedure Solve;
|
function Solve: Boolean;
|
||||||
function Solved: Boolean;
|
function Solved: Boolean;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSudoku.Solve;
|
function TSudoku.Solve: Boolean;
|
||||||
var
|
var
|
||||||
c, r: Integer;
|
c, r: Integer;
|
||||||
begin
|
begin
|
||||||
@ -113,21 +113,22 @@ begin
|
|||||||
end;
|
end;
|
||||||
for c := 1 to 9 do CheckDigits(c);
|
for c := 1 to 9 do CheckDigits(c);
|
||||||
CalculateValues;
|
CalculateValues;
|
||||||
until Solved or (Steps > 50);
|
Result := Solved;
|
||||||
|
until Result or (Steps > 50);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TSudoku.GiveSolution(var Values: TValues): Integer;
|
function TSudoku.GiveSolution(var Values: TValues; out ASteps: Integer): Boolean;
|
||||||
var
|
var
|
||||||
c, r: Integer;
|
c, r: Integer;
|
||||||
begin
|
begin
|
||||||
Fill(Values);
|
Fill(Values);
|
||||||
Solve;
|
Result := Solve;
|
||||||
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
|
||||||
Values[c, r] := Grid[c, r].Value;
|
Values[c, r] := Grid[c, r].Value;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
Result := Steps;
|
ASteps := Steps;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSudoku.CalculateValues;
|
procedure TSudoku.CalculateValues;
|
||||||
|
Reference in New Issue
Block a user