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.ColCoun

end;

Thanks

IELite

Solved

Posted on 2005-04-13

These questions are an adhancement to the following question:

http://www.experts-exchang

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

26 Comments

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.ColCoun

end;

Thanks

IELite

Random(33) provides you a number between 0 and 33

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

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)

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

Thanks!

IElite

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

"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

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;

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;

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

IELite

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

IELite

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

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;

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

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*DrawGr

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;

Title | # Comments | Views | Activity |
---|---|---|---|

Produce a sound in Delphi | 11 | 94 | |

Internet Explorer View Settings Question | 15 | 92 | |

FMX enumerated colours | 2 | 53 | |

Process Javascript errors with Delphi TWebBrowser | 1 | 84 |

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

Connect with top rated Experts

**22** Experts available now in Live!