Yep, I'm a newbie and totally out of my depth! Playing around with the idea of a dungeon crawl but when I populate my traps into the 2d array, rand duplicates and overwrites its previous locations (for example 99 traps randomly generated into a 10 x 10 does not fill 99 locations with 1 (previously all set to 0) leaving just one 0 remaining, you get my drift)!
Tried numerous ways (within my limited knowledge) but cannot work out how to check if previous local has already been populated with a 1, basically gettting rand to check each go before continueing? Hope this makes sense.
(I don't really understand my rand % 10 bit (borrowed it from another crawl, so don't know how to split it)?
Any help, apart from give up for now, would be most helpful?
Thanks in advance.
Here's my code.

#include <iostream>
using namespace std;

int main() 
{      
const int nNumRows = 10; //down
const int nNumCols = 10; //across
int array[nNumRows][nNumCols] = { 0 }; // will eventually display
int array_check[nNumRows][nNumCols] = { 0 };
int randx = 0, randy =0;			//random values
int x = 0, y = 0;					//x-axis and y-axis position
int n, counter = 0;
int p = 0;

srand((unsigned)time(0));			//set off random counter

// Doesn't work! Rand places 99 x 1 but must overwrite itself on placements?
  for ( p=1 ; p<100 ; p++ )
  {
  randx =rand()% 10;
  randy =rand()% 10;
  array_check[randx][randy]=1;
  if (p == 99)
  break;
//      counter += array_check[nNumRows][nNumCols];
//      array_check[randx][randy]=1;
  }
  cout << p << endl;


//do {
//	randx =rand()% 10;
//	randy =rand()% 10;
//	if (array_check[randx][randy]!=2)
//	array_check[randx][randy]=1;
//	counter++; // adds one to counter
//	}while (counter != 99); // compares till they equal

//for (n=0; n<=98; n++)
//	{
//	randx =rand()% 10;
//	randy =rand()% 10;
//	array_check[randx][randy]=1;
//	}


//for (randx = 0; randx <10; randx++)				
//	for (randy=0; randy<10; randy++)
//		if (array_check[randx][randy] !=1)
//			array_check[randx][randy]=2;
//			else (array_check [randx][randy] =0);
//            array_check[randx][randy]=4;


for (int nRow = 0; nRow < nNumRows; nRow++)
{
for (int nCol = 0; nCol < nNumCols; nCol++)
cout << array_check[nRow][nCol] << " ";
cout << endl;
}

  system("PAUSE");
  return 0;
}

I know the codes ultra messy but I thought it was important to show you my attempts at sorting it.

Thanks, Leppie

Sounds like another instance where a shuffle algo would be better suited perhaps? Don't quote me on it though. I haven't looked at your code in detail at all.

Edited 6 Years Ago by iamthwee: n/a

I have a few code snippets that may or may not be of use. I'm not entirely sure what your goal is:

http://www.daniweb.com/code/snippet217346.html
http://www.daniweb.com/code/snippet217217.html

I don't know what a trap or a dungeon crawl is. I also don't know what 0 or 1 represents in this array. Is this some sort of path/maze where 0 represents a free space or where you've already been or a wall or whatever?

Lines 23 and 24 look meaningless to me. You have commented out code below, so maybe at one time they meant something, but not now.

What should be the array's contents after it reaches line 28 (i.e. after all the randomness is over)? I can't visualize what the end result is supposed to be. If the goal is for to fill in a bunch of random squares with 1, it seems like the code works. But clearly it doesn't or you would not be posting.

I'm feeling helpful so I'll show you one way to do this.

int counter = 1, x,y, zeroX, zeroY;
int map[10][10];
srand((unsigned)time(0));
zeroX = rand() % 10;	// this gets the spot to put the zero
zeroY = rand() % 10;
// place zero
map[zeroX][zeroY] = 0;
while (counter < 100)
{
	x = rand() % 10;
	y = rand() % 10;
	if (x == zeroX && y == zeroY) // check to see if we are at the zero
		continue;
	else
	{
		map[x][y] = 1;  // if not make it a 1
		counter++;
	}
}

Thanks for all your help people, Nathan thanks for your bit but it didn't solve the problem as rand still overwrites itself in your code as well. I've tried to make it easier by not using a 2D array and explain it more. This is what I'm after.

Suppose I create a 1D array (normal array) of 20 elements. I then fill each element with a 0.
I then use rand to randomly select an element and change that elements value to 1.
This repeats until 10 random elements each contain the value 1. That's my goal.

Now I need to know a way to achieve this? Using a loop I can place 10 number 1's into my array but on displaying the array there are never 10 lots of 1. (Duplicity of rand overwrites already placed 1's, leaving me less then 10).

I therefore need to check the value of where rand will place the number 1 before its actual placement to make sure that it is not overwriting a 1 with a 1, (this I can't do because rand is a random placement, so how would I know where to look?)

I suppose (think) I'll have to iterate through the array (while in the loop) adding the array values to the total equals a total of 10 before exiting the loop. (This should work)!

Stuck here though, I know theres a way forward but I'm stumped. Looked at it using "find" but this only locates the first element that's 1. I don't know pointers yet, but do I need to use these and use the elements address in a variable to record what's where to then not overwrite, or is there an easier way? Otherwise I'm going to be stuck with a big "if else's" to lock out element addresses already allocated.

Obviously I don't know enough yet and this is looking to ambitious for myself to try and sort. There's no code I'm afraid (apart from the twenty odd variations of code trying different methods), which would you like? None of them work anyway!

In fact my decision here is to quit this project and come back to it later, but not knowing what method to attack this problem from is driving me nuts. Do I need to use element addresses? find? Is it pointers that I need to know? (Learning pointers now in an attempt to solve it, but is this the right direction or am I complicating the issue?)

This makes me sound so thick!

Leppie.

Start with NathanOliver's example, but change the condition of the if statement on Line 12 to look at the current contents of the cell. If the cell is occupied, continue, otherwise, populate it.

Edited 6 Years Ago by Fbody: n/a

This is looking a bit different from what you initially described.

There are a variety of ways to do this. I think the below way is the easiest

int array[20] = {0};  // initialize array elements to 0
int numOnes = 0; // initialize ones counter to 0

while (numOnes < 10) // check whether we have ten ones yet
{
    int index = rand () % 20; // pick a random index
    if (array[index] != 1) // check array element to see if it's already 1
    {
        // it's not, so make it 1 and increment counter
        array[index] = 1;
        numOnes++;
    }
}

That's the full code for the One-Dimensional. Not much to it. I couldn't think of how to explain it well enough without giving the whole thing, so here you are. :)

Two dimensional will be extremely similar.

Edited 6 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.