2020-01-04 14:39:11 +00:00
|
|
|
unit sudokumain;
|
|
|
|
|
|
|
|
{
|
|
|
|
***************************************************************************
|
|
|
|
* Copyright (C) 2006 Matthijs Willemstein *
|
|
|
|
* *
|
|
|
|
* This source is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This code is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* A copy of the GNU General Public License is available on the World *
|
|
|
|
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
|
|
|
|
* obtain it by writing to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
}
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids,
|
2020-01-04 15:20:09 +00:00
|
|
|
Buttons, StdCtrls, SudokuType;
|
2020-01-04 14:39:11 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
TForm1 = class(TForm)
|
|
|
|
ButtonSolve: TButton;
|
|
|
|
ButtonFill: TButton;
|
|
|
|
StringGrid1: TStringGrid;
|
|
|
|
procedure ButtonFillClick(Sender: TObject);
|
|
|
|
procedure ButtonSolveClick(Sender: TObject);
|
2020-01-04 15:02:45 +00:00
|
|
|
procedure StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
|
2020-01-04 15:12:28 +00:00
|
|
|
{%H-}aState: TGridDrawState);
|
2020-01-04 14:39:11 +00:00
|
|
|
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
|
|
|
|
const Value: string);
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
theValues: TValues;
|
2020-01-04 15:20:09 +00:00
|
|
|
function SolveSudoku: Boolean;
|
|
|
|
procedure ShowSolution;
|
2020-01-04 14:39:11 +00:00
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form1: TForm1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2020-01-04 14:55:32 +00:00
|
|
|
{$R *.lfm }
|
2020-01-04 14:39:11 +00:00
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
procedure TForm1.ButtonFillClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
c, r: Integer;
|
|
|
|
begin
|
|
|
|
for c := 0 to pred(StringGrid1.ColCount) do
|
|
|
|
for r := 0 to pred(StringGrid1.RowCount) do
|
|
|
|
StringGrid1.Cells[c, r] := '';
|
2020-01-04 14:49:55 +00:00
|
|
|
StringGrid1.Options := StringGrid1.Options + [goEditing];
|
2020-01-04 14:39:11 +00:00
|
|
|
StringGrid1.SetFocus;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.ButtonSolveClick(Sender: TObject);
|
|
|
|
begin
|
2020-01-04 14:49:55 +00:00
|
|
|
StringGrid1.Options := StringGrid1.Options - [goEditing];
|
2020-01-04 14:39:11 +00:00
|
|
|
SolveSudoku;
|
2020-01-04 15:20:09 +00:00
|
|
|
ShowSolution;
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
|
|
|
|
2020-01-04 15:02:45 +00:00
|
|
|
|
|
|
|
procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
|
|
|
|
aState: TGridDrawState);
|
2020-01-04 14:39:11 +00:00
|
|
|
var
|
2020-01-04 15:02:45 +00:00
|
|
|
NeedsColor: Boolean;
|
2020-01-04 14:39:11 +00:00
|
|
|
begin
|
2020-01-04 15:02:45 +00:00
|
|
|
NeedsColor := False;
|
|
|
|
if aCol in [0..2, 6..8] then
|
|
|
|
begin
|
|
|
|
if aRow in [0..2, 6..8] then
|
|
|
|
begin
|
|
|
|
NeedsColor := True;
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
2020-01-04 15:02:45 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if aRow in [3..5] then
|
|
|
|
begin
|
|
|
|
NeedsColor := True;
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
|
|
|
end;
|
2020-01-04 15:02:45 +00:00
|
|
|
if NeedsColor then
|
|
|
|
(Sender as TStringGrid).Canvas.Brush.Color := $00EEEEEE;
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.StringGrid1SetEditText(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];
|
|
|
|
end else begin
|
|
|
|
theValues[ACol + 1, ARow + 1] := ' ';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-01-04 15:20:09 +00:00
|
|
|
function TForm1.SolveSudoku: Boolean;
|
2020-01-04 14:39:11 +00:00
|
|
|
var
|
|
|
|
aSudoku: TSudoku;
|
2020-01-04 15:20:09 +00:00
|
|
|
Col, Row: Integer;
|
|
|
|
Steps: Integer;
|
2020-01-04 14:39:11 +00:00
|
|
|
begin
|
2020-01-04 15:20:09 +00:00
|
|
|
for Col := 0 to 8 do begin
|
|
|
|
for Row := 0 to 8 do begin
|
|
|
|
if Length(StringGrid1.Cells[Col, Row]) >= 1 then
|
|
|
|
begin
|
|
|
|
theValues[Col + 1, Row + 1] := StringGrid1.Cells[Col, Row][1];
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
theValues[Col + 1, Row + 1] := ' ';
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
aSudoku := TSudoku.Create;
|
2020-01-04 15:20:09 +00:00
|
|
|
Result := aSudoku.GiveSolution(theValues, Steps);
|
2020-01-04 14:39:11 +00:00
|
|
|
aSudoku.Free;
|
2020-01-04 15:20:09 +00:00
|
|
|
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;
|
2020-01-04 14:39:11 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|