-1

I want to generate the number 2 5 times and the number 1 10 times. I'm trying to spread these across a String Grid in Delphi randomly. I also want to fill the rest of the grid that isn't 1 or 2, with 0's. I have no idea how to even start here.

It would look something like this (P stands for player and there would only be 5 2's and 10 1's): https://gyazo.com/aeef05c3a92ce7847c0f42ad40faa733

ban can
  • 77
  • 6
  • What have you done so far? What is your exact problem? Creating a list of number to be displayed? Shuffling that list? Writing the numbers into the grid? – Wosi Jan 25 '17 at 16:58
  • I've created the list and initialized them (1 array for 2 5 times and 1 array for 1 10 times). I'm just not sure how to write them randomly into the grid. – ban can Jan 25 '17 at 17:05
  • Put all the coordinates of the grid into an array. Shuffle it with Fisher-Yates shuffle. Fill the first 5 shuffled coords with 2, the next 10 with 1, and the rest with 0. – David Heffernan Jan 25 '17 at 17:16
  • I'm trying to assign the 1's to an array, but for some reason I keep getting an error. I'm assigning it to a dynamic array because the user inputs the row count and the column count. The error I keep getting when trying to assign the first 10 numbers is: access violation at address 00409f9A in module 'Project1.exe'. Write of address 00000000 – ban can Jan 25 '17 at 20:08
  • Clearly there's a defect in your code. Try debugging it. That's by far the most efficient way forward. Novice programmers tend to be poor at debugging. Make it your mission to learn how to debug. – David Heffernan Jan 25 '17 at 20:59
  • I've figured it all out don't worry haha. Now I'm struggling with shuffling the array. Any ideas? :) – ban can Jan 25 '17 at 21:18
  • Do a search for Fisher Yates shuffle. I know I've written answers here on the topic. For instance: http://stackoverflow.com/questions/14006205/randomize-stringlist – David Heffernan Jan 25 '17 at 22:18

2 Answers2

3

Given a grid with dimensions m×n, create an array of length m * n. Put five 2's and 10 1's in the array, and fill the remainder with 0's. (We'll assume the product of m and n is at least 15.) Shuffle the array. Copy each element of the shuffled array into successive cells in the grid.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I have to use a dynamic array because the user inputs the column count and the row count. When I try to assign the first 10 numbers, I get this error: access violation at address 00409f9A in module 'Project1.exe'. Write of address 00000000 – ban can Jan 25 '17 at 20:14
  • That doesn't affect this answer at all. Looks like you have some debugging to do. Have you made the array be the size it needs to be? (Call `SetLength`.) – Rob Kennedy Jan 25 '17 at 20:16
  • I know it's not your fault, I'm just asking for advice haha. Yeah i set the length of the dynamic array. i then used 2 for loops with these: N:= treasure[i]; multiarray[i,j] := IntToStr(N); – ban can Jan 25 '17 at 20:22
  • Unless `j` is always 0, you'll eventually reach a point where the `i` used to index the grid goes beyond the bounds of the array. That doesn't lead to an access violation at address 0, though. You've got a null pointer somewhere else. Turn on range checking, turn on overflow checking, and use the debugger. – Rob Kennedy Jan 25 '17 at 20:25
  • i is 0 to 9 and j is 0 to 0. The bizarre thing is, when I change the multiarray[i,j] to StringGrid1.cells[i,j] it fills the row 10 times properly. I will do some debugging now. – ban can Jan 25 '17 at 20:28
  • Found the error. If the grid is below 10x10, it goes out of bounds (by default it was 8x8). Anyway of making the 1's go down to (0,1) after the first 8? – ban can Jan 25 '17 at 20:31
  • Given an index into the linear array, you can compute a corresponding coordinate pair in the grid. For example: `Assert(Length(treasure) = m*n); for i := 0 to Pred(m*n) do grid[i div m, i mod m] := treasure[i];` – Rob Kennedy Jan 25 '17 at 20:47
  • Okay so I basically forgot to do StringGrid1.Rowcount -1, so that's why i was getting some errors. But now everything works. All I need to do now is shuffle the array and I can't find an explanation of how to do that anywhere on the internet. Can you help me? :)) – ban can Jan 25 '17 at 21:36
  • 1
    You can't find *anything* about shuffling an array on the Internet? I don't believe you. There are nearly 3000 questions about it on this site alone. Go try again. Or refer to your textbook. Or ask your instructor. – Rob Kennedy Jan 25 '17 at 22:22
  • @RobKennedy Isn't creating an array of all grid cells and then shuffling it an overkill? Let us say that OP has a grid with 100 by 100 dimensions and he wants to randomly place 500 items on it. Your approach would require OP to create an array with 10K items and then make at least 5K `Random` calls for shuffling the array. Wouldn't it be easier to simply pick each item coordinates randomly by using two random calls (one for X coordinate and one for Y coordinate) before placing them? – SilverWarior Jan 26 '17 at 20:31
0

While the approach represented in Robs answer will do the job I personally think it is way to complicated for it's purpose.

So what would be more simpler approach?

Well your goal is to place these numbers at random positions in grid.

How do you determine position of some object in a grid? You do it by its X (Column) and Y (Row) coordinates.

So how do you get random position in a grid? Simple chose two random values for X and Y coordinates.

As for placing certain numbers of number 1 and number 2 use two simple loops.

SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • 1
    I'm not sure I see how that's simpler. How are you measuring simplicity? (By the way, your approach is wrong, as-is, because it doesn't account for the case where the location you pick has already been filled on a previous iteration. My approach is immune from that case.) – Rob Kennedy Jan 26 '17 at 21:03
  • @RobKennedy That can be solved by a simple check to see if the cell at those random coordinates is empty. – SilverWarior Jan 26 '17 at 21:22
  • That's a check that my approach doesn't need to worry about. How do you measure simplicity? – Rob Kennedy Jan 26 '17 at 21:27
  • I eventually figured it out using Rob's method. Shuffling the grid was a pain, but I found out how to do it after about 3 hours. I was originally going to do your method, but I wasn't sure how to choose random coordinates to place them at. For now i'll stick to Rob's method, but if I get any issues, then I'll switch. Thanks for the answer. – ban can Jan 28 '17 at 14:17
  • @silver What you don't mention is that you need to check that you haven't drawn a coordinate already. So you end up looping. If you need to fill the grid then this can be a little wasteful. It's for sure more complex than a simple shuffle. – David Heffernan Jan 28 '17 at 14:23