How to handle my TStringgrid in order to achieve my goal(s)?

Hi Experts!


I have a TStringgrid which I fill with predfined characters coming from some textfile. I also have the ability to save the contents to a textfile. The TStringgrid is 20 cols and 20 rows and each cell contains but one character ranging from A to Z. The textfiles are 400 bytes of size hence, they hold the 400 characters of my TStringgrid.

Now the hard part: I have as much as 18 different textfiles each filled with 400 characters. Now in all of those textfiles some words are hidden, (ref. wordsearching), those words are 5 characters long and are hidden twice in each file. The locations differ per file.

Example: in file oefen1.txt the word SPOKS is hidden twice. When this particular file is loaded, my stringgrid should monitor the cells in which the characters are places which form the word SPOKS. The first instance of this word covers the following cells of the TStringgrid: 4,3 - 5,3 - 6,3 - 7,3 - 8,3  (the first number being the column the second being the row > this instance is horizontally read from left to right)

Now I managed to grab hold of the cells by means of popping up a message in case one of the cells is clicked > here's my code:

procedure TForm1.SG1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
if (Acol =  4) and (Arow = 3) then
ShowMessage('Found');
if (Acol =  5) and (Arow = 3) then
ShowMessage('Found');
if (Acol =  6) and (Arow = 3) then
ShowMessage('Found!');
if (Acol =  7) and (Arow = 3) then
ShowMessage('Found');
if (Acol =  8) and (Arow = 3) then
ShowMessage('Found!');
end;

To monitor the second instance of that word as well would mean another 5 of those if (Acol = ..) and (Arow = ..) then ShowMessage('..') thingies.

Loading another textfile, would mean searching another word which covers different cells ofcourse. Given the 18 different textfiles, I'm now looking for some mechanism to let the name of the textfile loaded, determine which 10 cells to monitor.

Loading oefen1.txt would then automatically have the TStringgrid monitor the cells mentioned above, while loading oefen2.txt would then have the Tstringgrid monitor 10 other cells (which are covered by some other word)

How do I achieve that?
Should I store some info in the textfile which then functions like some ini file and determines which cells to monitor or should the loading set some variable or point to some procedure????????


Regards Peter

Ps the person who manages to make this thingie work gets the points. Provide working samples and it would be great when you comment what it is you actually do.
PeterdeBAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mokuleConnect With a Mentor Commented:
here is sample file
1
2
1
3
1
4
1
5
1
6
4
12
5
12
6
12
7
12
8
12
-------------------------
And the program
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
    letters: array[0..9] of TPoint;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// reading a file with cell coordinates
procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
  F: TextFile;
  str: string;
begin
  AssignFile(F,'test.txt');        // sample coordinate file as above
  Reset(F);
  for i := 0 to 9 do
    begin
    readln(F,str);
    letters[i].X := StrToIntDef(str,-1);
    readln(F,str);
    letters[i].Y := StrToIntDef(str,-1);
    end;
  CloseFile(F);
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  i: integer;
begin
  for i := 0 to 9 do
    if (letters[i].X = ACol) and (letters[i].Y = ARow) then
      ShowMessage('Found!');
end;

end.


0
 
mokuleCommented:
You should have saved 10 pair integer values for each file corresponding to location of letters of hidden words.
it may be saved in a simple text file.
Each line containing an integer.
0
 
PeterdeBAuthor Commented:
I hereby grant you the points Mokule!!

Got it working now > I moved your code a bit in order to have my app load the puzzle + the coordinates. This way I can load another puzzle at run time with corresponding coordinates, which was my goal.

This now seems to work perfect:

// procedure which fills the Stringgrid and loads the coordinates of cells to monitor
// LBWZ and LBCELLS are both Listboxes. LBWZ contains the puzzle files and LBCELLS contains the coordinate files

procedure TForm1.btnLoadPuzzleClick(Sender: TObject);
 var
  f : file;
  arr : array of char;
  n : cardinal;
  ig: integer;
  fCells: TextFile;
  str: string;
begin
AssignFile(f, LBWZ.filename);
FileMode := fmOpenRead;
reset(f, 1);
SetLength(arr, filesize(f));
BlockRead(f, arr[0], length(arr));
CloseFile(f);
for n := 0 to high(arr) do SG1.Cells[n mod 20, n div 20] := arr[n];
AssignFile(fCells,LBCELLS.FileName);  
  Reset(fCells);
  for ig := 0 to 9 do
    begin
    readln(fCells,str);
    letters[ig].X := StrToIntDef(str,-1);
    readln(fCells,str);
    letters[ig].Y := StrToIntDef(str,-1);
    end;
  CloseFile(fCells);
end;


// your unmodified procedure which messages: Hoooray!! when the predefined cells are clicked

procedure TForm1.SG1SelectCell(Sender: TObject; ACol, ARow: Integer;var CanSelect: Boolean);
  var i: integer;
begin
  for i := 0 to 9 do
    if (letters[i].X = ACol) and (letters[i].Y = ARow) then
      ShowMessage('Hoooray!');
end;


Regards,

Peter :)

Ps I'm a very happy man now! :)
0
 
mokuleCommented:
So I'm happy also.
I've known You must change my code to Your demands.
But I've believed You can do it, and I was right.:)
0
 
PeterdeBAuthor Commented:
Well thank you Mokule, this really means a lot to me to get a compliment from you.....speechless....

Regards from Happy Peter :)
0
All Courses

From novice to tech pro — start learning today.