@Alertoso

Показывается на 1 комбинацию меньше(расстановка ферзей), что делать?

И снова мои ферзи, докручивал оформление, ну и файл заодно, в итоге вышли свои проблемы:
1) как можно закрасить доску адекватнее?
2) один из главных вопросов: у меня показывается с 1 комбинации по 9 только, счётчик, когда показывается комбинация показывает значение, на одно больше, например, у меня 1 комбинация, а счётчик уже на 2 перескочил, подскажите, пожалуйста, как можно пофиксить данный момент?
Привожу весь листинг программы, где данный аспект необходимо пофиксить, на данный момент:
Unit Unit1;

Interface

Uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, Buttons, StdCtrls, Spin, Menus, jpeg;

Const Maxlen = 15;
{При размере доски 15 x 15 будет 2279184 различных комбинаций расстановки ферзей, для 14 - 365596}

Type
ta=array[1..Maxlen] of 0..Maxlen;

  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    SpinEdit2: TSpinEdit;
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    procedure Draw(var a:ta; co: cardinal);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BackTracking(k:integer;a:ta);
    procedure SpeedButton2Click(Sender: TObject);
    procedure DeleteThisFileContent(const FileToClear: string);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

Var
  Form1: TForm1;
  Input: TextFile;

implementation

uses Unit2;

var
  a:ta;
  t:cardinal;
  exitclick:boolean;
  NumOfPlacements:cardinal;
  curlen:integer;
  showtime:cardinal;

{$R *.dfm}

procedure TForm1.DeleteThisFileContent(const FileToClear: string);
Begin
  Assignfile(Input, 'D:\Работы по ОАиП\Курсовая работа\Placements.txt');
  Rewrite(Input);
  Closefile(Input);
End;

procedure TForm1.Draw(var a:ta; co: cardinal);
var I, J:integer;
  HelpString: string;
  img1, img2: TPicture;
Begin
  Assignfile(Input, 'D:\Работы по ОАиП\Курсовая работа\Placements.txt');
  Append(Input);
  writeln(Input,'Placement № ',co);
  if Curlen-1 div 10 = 1 then
  Begin
    while Length(HelpString) < ((curlen-1)*3+curlen-2+curlen mod 10) do
      HelpString := '-' + HelpString;
    writeln(Input,HelpString)
  End
  else
  Begin
    while Length(HelpString) < ((curlen-1)*3+curlen-2) do
      HelpString := '-' + HelpString;
    writeln(Input,HelpString)
  End;
  for i:=1 to Curlen-1 do
  begin
    Write(Input,char(ord(96+a[i])),'-',curlen-i);
    if I <> curlen-1 then write(Input,',');
  end;
  writeln(Input);
  writeln(Input,HelpString);
  CloseFile(Input);
  img1:= TPicture.Create;
  img1.LoadFromFile('D:\Работы по ОАиП\Курсовая работа\Ферзь 1.bmp');
  img2:= TPicture.Create;
  img2.LoadFromFile('D:\Работы по ОАиП\Курсовая работа\Ферзь 2.bmp');
  for I:=1 to Curlen-1 do with StringGrid1 do
  Begin
    for j:=1 to Curlen-1 do with StringGrid1 do
    Begin
      if (j+i) mod 2 = 1 then
      Begin
        StringGrid1.Canvas.Brush.Color:=$478bd1;
        StringGrid1.Canvas.Rectangle(CellRect(i,j));
      End
      else
      Begin
        StringGrid1.Canvas.Brush.Color:=$9ecfff;
        StringGrid1.Canvas.Rectangle(CellRect(i,j));
      End;
    End;
  End;
  StringGrid1.Canvas.Brush.Color:=clYellow;
  for I:=1 to Curlen-1 do with StringGrid1 do
    if (I+A[I]) mod 2 = 0 then
      StringGrid1.Canvas.StretchDraw(StringGrid1.CellRect(i,a[i]), img1.Graphic)
    else
      StringGrid1.Canvas.StretchDraw(StringGrid1.CellRect(i,a[i]), img2.Graphic);
End;

procedure TForm1.BackTracking(k:integer;a:ta);
var I, J:integer;
  Po:boolean;
  B:ta;
Begin
  if not exitclick then
  Begin
    if k<curlen-1 then
    Begin
      for i:=1 to curlen-1 do b[i]:=a[i];
      for i:=1 to curlen-1 do
      Begin
        po:=true;
        for j:=1 to k do
        Begin
          if a[j]=i then po:=false;
          if abs(a[j]-i)=abs(j-k-1) then po:=false
        End;
        if po then
        Begin
          b[k+1]:=i;
          BackTracking(k+1,b)
        End
      End
    End
    else
    Begin
      Inc(NumOfPlacements);
      Form1.Caption:=IntToStr(NumOfPlacements);
      Form1.Draw(a,NumOfPlacements);
      t:=GetTickCount;
      repeat
        Application.ProcessMessages;
      until (GetTickCount-t)>showtime;
      StringGrid1.Repaint
    End
  End
End;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var i,j: integer;
Rect: TRect;
Begin
  assignfile(Input, 'Placements.txt');
  rewrite(Input);
  closefile(Input);
  NumOfPlacements:=0;
  exitclick:=false;
  showtime:=SpinEdit2.Value;
  curlen:=SpinEdit1.Value+1;
  StringGrid1.ColCount:=curlen;
  StringGrid1.RowCount:=curlen;
  StringGrid1.DefaultColWidth:=trunc((StringGrid1.Width-2*curlen)/curlen);
  StringGrid1.DefaultRowHeight:=trunc((StringGrid1.Height-2*curlen)/curlen);
  i:=1;
  J:=Curlen-1;
  while (I <= Curlen) do with StringGrid1 do
  begin
    Font.Size:=20+trunc(StringGrid1.DefaultRowHeight/curlen)-1;
    Font.Style:=[fsBold];
    Font.Name:='Times New Roman';
    StringGrid1.Canvas.Brush.Color:=clRed;
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Cells[i, 0] := char(ord(96+i));
    StringGrid1.Cells[0, I] := IntToStr(J);
    Inc(I);
    Dec(J);
  end;
  BackTracking(0,a)
End;

Procedure TForm1.FormCreate(Sender: TObject);
Begin
  Curlen:=8;
  Showtime:=300
End;

procedure TForm1.SpeedButton2Click(Sender: TObject);
Begin
  exitclick:=true;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Hide;
  Form2.Show;
end;

End.
  • Вопрос задан
  • 66 просмотров
Решения вопроса 1
HemulGM
@HemulGM Куратор тега Delphi
Delphi Developer, сис. админ
1. Рисовать нужно было не на StringGrid, а на DrawGrid, у которого есть событие OnDrawCell. Оно вызывается на каждую ячейку (данные сетки храни в массиве, например двумерный массив из record). И не нужно будет делать страшный цикл с использование костыля Application.ProccessMessages
2. Переменная текущей комбинации - 1
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы