Simple Dungeon Crawl, help with rand() or arrays?

Hi,

I'm trying to build a simple Dungeon Crawl game. And so far I've managed to build the board. Next I wanted to place the treasure in a random square on the board.

This code works, but sometimes it outputs 2 treasures, WHY?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 #include <iostream>

using namespace std;

char board[10][8];


void buildboard();

void initialize();


int main()
{


	initialize();
	buildboard();
	
}

void initialize()
{
	for(int i=0; i<8; i++)
	{
		for(int j=0; j<10; j++)
		{
			board[i][j] = '.';
		}
	}	 
	
	int x, y;
	srand(time(0)); 					
	x = rand() % 8;
	y = rand() % 10;
	board[x][y] = 'T';

}

void buildboard()
{
	system ("cls");
	cout << "\n\tDUNGEON CRAWL \n\n";
	
 	for(int i=0; i<8; i++)
	{
		cout << "\t ";
		for(int j=0; j<10; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}	 
}
You have the wrong dims for your board.

Your board is 10x8:
 
char board[10][8];


Yet you are treating it as 8x10 everywhere else in your code.
cool thanks, spent too long looking at it, and changed all the 10s and 8s too many times.
Topic archived. No new replies allowed.