I have made the treasure hunt game using 2D array. where '#' are the walls '*' is the border of the game.

i want to place '@' as tresure at random postion between the spaces of 2D Array of size
[20][20].
i know i have to use
#include<time.h>

time_t t;
time(&t);
srand(t);
int r = rand() % 18; r is the position for y position of 2D array.

but i have to place '@' at more than one place and at different position.
and other thing '@' char cant be place on the borders.

treasure on each space line could be 3,2 or 1 but not 0.

Thanks in advance

Recommended Answers

All 4 Replies

>>treasure on each space line could be 3,2 or 1 but not 0.
I don't understand that, but for the rest..

You can call rand() as many time as you want, so that solves the problem of having multiple treasures.

Also, to avoid walls and borders and other treasures, just make a loop that generates a new treasure position, checks if the spot is already occupied by a wall or border or treasure. If not, then set that position to a treasure. If yes, it is already occupied, then go back to the start of the loop, generate a new position for the treasure and repeat. Repeat this loop until you have placed all the treasure you wanted to place.

>>treasure on each space line could be 3,2 or 1 but not 0.

regarding the above, i mean that at each free space, there would only be 3 treasure at different position or 2 or 1 there must be some treasure.

the treasure hunt game is not automatic, this is user input treasure hunt game. All other conditions are being made, the only thing rest is random treasures on The 2D array.

Oh so you mean in 1 location, there can be up to 3 treasures. Can you show me how
are are setting up your 2d array. The size and whatnot.

i have defined my array only having fixed walls and border position

char array1[20][20] = {{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
		{'*','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#',' ','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#','#','#',' ','#','#','#','#',' ','#','#','#','#','#','#',' ','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#',' ','#','#','#',' ','#','#','#','#','#',' ','#','#',' ','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#','#',' ','#','#','#','#','#','#','#','#',' ','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#',' ','#',' ','#','#','#','#','#','#',' ','#','#','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#','#','#','#',' ','#','#',' ','#',' ','#','#',' ','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#','#',' ','#','#','#','#','#','#','#','#',' ','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
		{'*','#','#','#',' ','#','#','#',' ','#','#','#',' ','#','#','#','#','#','#','*'},
		{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}};


	array[18][1] = 'H';                ////////////Home Position 

array[2][18] = 'F';				   ////////////Final Position

here * are fixed borders and # are fixed walls.
the spaces which are defined as ' ' in 2D array between the walls are free space to place the treasure.
in each space way there could be 1 treasure , or 2 treasure , or 3 treasure . every time treasure are placed at random position.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.