Pointer to a 2D array; to put "x"

How could I put a random x, inside the 2d array?

thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>

using namespace std;

int main()
{

char* GameMap [10][10] =
{"|","-","-","-","-","-","-","-","-","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|"," "," "," ","     ","","","","","|",
 "|","-","-","-","-","-","-","-","-","|",};


for(int x=0; x < 10; x++){
    cout << endl;
    for( int y = 0; y < 10; y++){
    cout << " ";
    cout << GameMap[x][y] ;


    }


cout << endl;


}
}
char* GameMap[rand()%10+1][rand() %10+1] = 'x';
placing the 'x' is a piece of cake:
1
2
3
int a=5;
int b=5;
GameMap[a][b] = "x";

now all you need is just some way make the value of a and b random.
for random numbers I use this:
1
2
3
4
5
6
7
8
9
#include <cstdlib>
#include <ctime>

int random(int minimum_number,int maximum_number,unsigned milliseconds=50){
	clock_t start = clock();
	while(clock()-start < (CLOCKS_PER_SEC/1000.0)*milliseconds)
		rand();
	return minimum_number+(rand()%(maximum_number+1-minimum_number));
}

and use: srand(time(NULL)); in the first line of main
Last edited on
I think his problem is that placing x like that would mess up the alignment of the map. like where ever 'x' is placed it would move the end '|' one place further.
why would it do that?
the 'X' replaces the ' ' not catenate to it.
this bit that i put together seems to work just fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int random(int minimum_number,int maximum_number,unsigned milliseconds=50){
	clock_t start = clock();
	while(clock()-start < (CLOCKS_PER_SEC/1000.0)*milliseconds)
		rand();
	return minimum_number+(rand()%(maximum_number+1-minimum_number));
}

int main() {
	char GameMap[10][10] = {
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
		{' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
	};
	srand(time(NULL));
	GameMap[random(0,9,100)][random(0,9,100)] = 'X';
	
	cout << "+----------+" << endl;
	for(int x=0; x < 10; x++){
		cout << '|';
		for( int y = 0; y < 10; y++)
			cout << GameMap[x][y];
		cout << '|' << endl;
	}
	cout << "+----------+" << endl;
}
Last edited on
I actually didn't see your post... so i was replying to bobby tables. Since the code op posted has elements with nothing in them "" replacing that with a 'x' would do what I said
Last edited on
lol, I didn't even see Little Bobby Tables post, it's so small. not that small is a bad thing, if you can give them all the info they need in a post like that why not.
Last edited on
Topic archived. No new replies allowed.