Solved

# Two More ?'s

Posted on 2005-04-13
325 Views
Last Modified: 2010-08-05

These questions are an adhancement to the following question:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21386987.html

1.) How do I chnage the Drawgrid some there is no cell selected at all ( no selection or focus for any cells) yet cells still  respond to mouseclicks (onMouseClick, OnMouseDown, OnMouseUp)

2.)

I need to now change the Random number generator.

From the old question: "randomly generated integers should be  in the range of       (Col * row)  div 2"

I need the array to now be filled instead , with sets of identical randomly generated numbers in the same range as described above (from the last question).

From previous question:

"Example:   if grid size = 8 X 8 then the total number of cells = 64      div 2 = 32
The array should be filled with a random number between 1 and 32"

This still holds true, however, there now will only be 16 random numers, cause each number should be in the array (at a random spot) twice.

Example: (each randoim number in array 2 times) = total of 64 numbers (32 sets)

16 01 30 12 09 27 19 11
11 07 03 31 04 22 29 15
19 06 24 26 02 04 17 05
22 10 13 08 20 26 12 23
23 05 16 18 08 13 20 30
27 18 25 06 03 31 02 28
21 15 01 24 15 25 28 29
17 21 10 07 14 09 05 14

sholmes
0
Question by:IElite
26 Comments

Author Comment

I may be wrong, but i believe sholmes procedure (below) is the only thing that should need to be changed to accomdate my changes in question # 2.

procedure TForm1.UpdateBoard;
var
I, J: Integer;
begin
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= Random(((DrawGrid1.ColCount * DrawGrid1.RowCount) div 2) + 1);
end;

Thanks

IELite
0

LVL 15

Expert Comment

You can specify a "range" for the random generator

Random(33) provides you a number between 0 and 33
Trunc(Random(32)) should give you 1-32
0

LVL 15

Expert Comment

1) you will have to call the methods manually as you have nothing to trigger them
0

LVL 15

Expert Comment

Sorry

Trunc(Random(33)) should give you 1-32
0

Author Comment

?????????

IELite
0

Author Comment

I dont believe that gives me a total of 64 numbers (32 sets of integers)

IElite
0

LVL 9

Expert Comment

you want to place the numbers in random places, not random numbers in set places:

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
begin
for N := 1 to 64 do
begin
repeat begin
I := Random(8);
J := Random(8);
end until Board[I,J] = 0;
Board[I,J] := N div 2;
end;
end;

(Note: I didn't run this through delphi)
0

LVL 15

Expert Comment

Change

Random(8);

to

Trunc(Random(9));
0

Author Comment

I want to do both.

A total of 64 numbers (32 random sets) set randomly in the grid - as the picture above suggests.

Thanks!

IElite
0

LVL 9

Expert Comment

IElite, do you want you random sets to be the numbers 1 to 32 or 32 random numbers between 0 and say 1,000,000?
0

LVL 15

Expert Comment

hmm .. might be

Trunc(1+Random(8))

You said
<The array should be filled with a random number between 1 and 32>
I gave you code to generate a random number between 1 and 32 which is what you asked for
0

Author Comment

Taken from above:

"I need to now change the Random number generator."

"From the old question: "randomly generated integers should be  in the range of       (Col * row)  div 2""

"I need the array to now be filled instead , with sets of identical randomly generated numbers in the same range as described above (from the last question)."

"From previous question:"

"Example:   if grid size = 8 X 8 then the total number of cells = 64      div 2 = 32
The array should be filled with a random number between 1 and 32"

"This still holds true, however, there now will only be 16 random numers, cause each number should be in the array (at a random spot) twice."

Sorry it was confusing

I need

A total of 64 numbers (32 random sets) set randomly in the grid - as the picture  suggests.

Example: (each randoim number in array 2 times) = total of 64 numbers (32 sets)

16 01 30 12 09 27 19 11
11 07 03 31 04 22 29 15
19 06 24 26 02 04 17 05
22 10 13 08 20 26 12 23
23 05 16 18 08 13 20 30
27 18 25 06 03 31 02 28
21 15 01 24 15 25 28 29
17 21 10 07 14 09 05 14

IElite
0

LVL 9

Expert Comment

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
sets: array of integer;
begin

// create sets
setlength(sets, DrawGrid1.ColCount * DrawGrid1.RowCount div 2);
for I := 0 to length(sets) -1 do sets[I] := Random(999999);

//clear board
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= 0;

// fill board
for N := 1 to length(sets) * 2 do
begin
repeat begin
I := Random(DrawGrid1.ColCount);
J := Random(DrawGrid1.RowCount);
end until Board[I,J] = 0;
Board[I,J] := sets[N div 2];
end;
end;
0

LVL 9

Expert Comment

// just thought of a few bugs in the algorithm: A set might repeat itself (since its random) and I got the 'div' wrong.

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
sets: array of integer;
begin

// create sets
setlength(sets, DrawGrid1.ColCount * DrawGrid1.RowCount div 2);
sets[0] := Random(9999) + 1;
for I := 1 to length(sets) -1 do sets[I] := sets[I-1] + Random(9999);

//clear board
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= 0;

// fill board
for N := 0 to (length(sets) * 2) - 1 do
begin
repeat begin
I := Random(DrawGrid1.ColCount);
J := Random(DrawGrid1.RowCount);
end until Board[I,J] = 0;
Board[I,J] := sets[N div 2];
end;
end;
0

Author Comment

ezraa,  i dont get your code - especially the - Random(9999) portion

IELite
0

LVL 9

Expert Comment

the 9999 is just some random large number. This time I'll explain a bit better:

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
sets: array of integer;
begin

// create sets
//   say we have an 8 x 8 DrawGrid,
//   create an array of 8x8/2 = 32 integers.
//   each item of the array is a 'set' that will be placed into two random places in the grid.
//   since we want the numbers to be random I set the first number to some random number between 1 and 10,000 Random(9999) + 1;
//   Then i set the rest of the numbers to equal the previous number + Random(9999);
//   (If I didn't use the previous number, the computer might generate the same number twice)
//
setlength(sets, DrawGrid1.ColCount * DrawGrid1.RowCount div 2);
sets[0] := Random(9999) + 1;
for I := 1 to length(sets) -1 do sets[I] := sets[I-1] + Random(9999) + 1;

//clear board
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= 0;

// fill board
//  To fill the board, I first generate 2 random numbers I,J
//  I check if Board[I,J] already received a number if I has, then I generate a new random set.
//  Once I find a position on the board where no number has been placed I put in The N'th set.
//  Since I want each set to appear twice I loop N from 0 to 63 and each time I find an Empty spot
//  on the board and insert the N/2'th set.
//
for N := 0 to (length(sets) * 2) - 1 do
begin
repeat begin
I := Random(DrawGrid1.ColCount);
J := Random(DrawGrid1.RowCount);
end until Board[I,J] = 0;
Board[I,J] := sets[N div 2];
end;
end;

///////////////

If you wanted your sets to be the numbers 1 to 32 placed randomly on the board you can use the first algorithm I suggested:

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
begin
//clear board
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= 0;

// fill board
for N := 0 to 63 do // loop through total number of sqares
begin
repeat begin
I := Random(8); // generate a random location on the board.
J := Random(8);
end until Board[I,J] = 0; // continue generating random locations untill an empty one was found.
Board[I,J] := (N div 2) + 1; // insert the numbers (first and second time: 1, next two times 2, ..... last two times: 32)
end;
end;

I hope this explains this idea enough -Ezra

0

Author Comment

OK, i will have to really ponder over this - your terminology and your renaming of things different from the code that sholmes gave me - has me very confused......

IELite
0

Author Comment

i placed your code in and I got

numbers as large as 9765 in my cells - i think the smallest is 1066

IELite
0

LVL 9

Expert Comment

For smaller numbers change the random(9999) to random(5).
0

Author Comment

OK, i still think there is confusion here....

I may be the blame, becuase I am trying to articulate what i need and it may be coming out wrong.

The grid can change (see above notes and previous question referenced)

Example, if grid is 8 X 8, then there would be a total of 64 cells.
Because I need them in sets, there will be really only 32 numbers generated
so the range would be between 1 - 32 (if it is a 8 x 8 grid - this will change if grid size changes).

That is - there will be 32 sets of numbers filling 64 cells of the grid (randomly).

16 01 30 12 09 27 19 11
11 07 03 31 04 22 29 15
19 06 24 26 02 04 17 05
22 10 13 08 20 26 12 23
23 05 16 18 08 13 20 30
27 18 25 06 03 31 02 28
21 15 01 24 15 25 28 29
17 21 10 07 14 09 05 14

NOw of course, if the Grid was changed to 4 X 4 then there would be a total of 16 cells
Because I need them in sets, there will be really only 8 numbers generated
so the range would be between 1 - 8

2647
6113
5288
3457

Hope this explains this better....

IELite
0

LVL 9

Expert Comment

If you wanted your sets to be the numbers 1 to 32 placed randomly on the board you can use the first algorithm I suggested:

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
size: integer;
begin
size := 8;
//clear board
for I:= 0 to size - 1 do
for J:= 0 to size - 1 do
Board[I, J]:= 0;

// fill board
for N := 0 to (size*size) - 1 do // loop through total number of sqares
begin
repeat begin
I := Random(size); // generate a random location on the board.
J := Random(size);
end until Board[I,J] = 0; // continue generating random locations untill an empty one was found.
Board[I,J] := (N div 2) + 1; // insert the numbers (first and second time: 1, next two times 2, ..... last two times: (size * size) / 2)
end;
end;
0

Author Comment

"If you wanted your sets to be the numbers 1 to 32 placed randomly on the board you can use the first algorithm I suggested"

Maybe, depending on the Grid Size.... If it was 8 X 8 then yes, but if it was 4 X 4 no.

The Routine must accommodate the change in size...

IElite
0

LVL 9

Accepted Solution

change the size := 8; to size := 4; then your set. or size := DrawGrid1.ColCount;
0

Author Comment

Yup, I figured it out.... thats what i needed..... Thanks for hangin in there....

IELite
0

LVL 11

Expert Comment

procedure TForm1.UpdateBoard;
var
N,I, J: Integer;
begin
for I:= 0 to DrawGrid1.ColCount - 1 do
for J:= 0 to DrawGrid1.RowCount - 1 do
Board[I, J]:= 0;
for N := 0 to (DrawGrid1.ColCount*DrawGrid1.RowCount) - 1 do
begin
repeat
begin
I := Random(DrawGrid1.ColCount);
J := Random(DrawGrid1.RowCount);
end
until Board[I,J] = 0;
Board[I,J] := (N div 2) + 1;
end;
end;
0

LVL 12

Expert Comment

I found a smart solution ....
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi databaseâ€¦
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, frâ€¦

#### 728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!