Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a C++ Game!

Status
Not open for further replies.

Lokiworm

Programmer
Apr 4, 2003
4
0
0
US
**I am working on a game that is a maze type thing. I am trying to get the lines i draw in the grid to be randomly generated. But I think that there is one little thing I am doing wrong because I dont know enough about randomly generated numbers and characters with the puesdo thing.**

Here is my code:

Code:
int main()
{

	
	char maze1[23][35];
	char fil=219;
	int r=0;
	int c=0;
	
	filmazes(maze1, fil);
	display_themaze1(maze1);
	drawmaze1(maze1);
	
	return 0;
}

void drawmaze1(char maze1[23][35])

{
	
	srand((signed)time(0));
		
for(int loopcount=0; loopcount!=10; loopcount++)
	{
		srand(rand());
		int random = (rand() % 20+1);
		int rowrandom = (rand() % 22+1);
		int colrandom = (rand() % 34+1);
		
		int r=0;
		while(r<=random)
		{
			maze1[rowrandom+r][colrandom]=' ';
			r++;			
		}
	}
	
for(int loopcount=0; loopcount!=10; loopcount++)
	{
		srand(rand());
		int random = (rand() % 20+1);
		int rowrandom = (rand() % 22+1);
		int colrandom = (rand() % 34+1);
		
		int c=0;
		while(c<=random)
		{
			maze1[rowrandom][colrandom-c]=' ';
			c++;
		}
	}
}

void display_themaze1(char maze1[23][35])
{
	for(int r=0;r<=22;r++)
	{
		for(int c=0;c<=34;c++)
		{
			cout << maze1[r][c];
		}
		cout <<endl;
	}
}

void filmazes(char maze1[23][35], char fil)
{
	for(int r=0;r<=22;r++)
	{
		for(int c=0;c<=34;c++)
		{
			maze1[r][c]=fil;		
		}
		
	}
}

**Everytime I run it, it just outputs a full gray block which is that it is supposed to do for the void fillmaze and then displays the maze which is the void displaymaze function. Then when it gets to the void drawmaze1 function it doesnt do anything and usely gives me a &quot;Winsioux c++ gui debug has caused an error in KERNEL32.DLL&quot; error.

**Any ideas what Im doing wrong?? PLEASE reply**
 
The syntax for a randomly generated number is:

int high, low, ranNum;
high = 10;
low = 0;

ranNum = rand() % (high-low+1) + low;



~~rar.
 
don't do srand(rand())
because you will always be seeding the generator with the same random number.

srand(time()) is better
 
He does seed it with time, initially. Then he seeds it with a new random number each time through the loop, presumably in an attempt to make it &quot;more&quot; random.
 
I can't see any point, though, ever in srand(rand()), because it always leaves the random number seed in a totally defined state relative to what it was at the start. i.e. it doesn't introduce any randomness.
 
Theoretically, that's true.

I suppose seeding it each time might help in cases where the pseudo-random number generator is less random than one would like (e.g. 1, 1, 1, 3, 2, 4, 6, 5, 27...).
 
All the info is good but it still doesnt help my problem!!

When the program goes into the For loop it generates 3 sets of random numbers, but for some reason the randomly generated numbers are not going into the double arrays found in the While loops.

Either I am putting them in wrong or something???

Any ideas?
 
At the moment you are populating the
Code:
maze1
array correctly, but not refreshing the screen with its contents. Another call to
Code:
drawmaze1
after
Code:
display_themaze1(maze1)
should do the trick.

I get the errors right at the end of execution as well, but I don't know why sorry. I think it's something to do with the iostream but I'm not sure what.

Hope this helps a bit
Rob
 
Latest update on code:

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

void display_themaze1(char maze1[23][35]);
void filmazes(char maze1[23][35], char fil);
void drawmaze1(char maze1[23][35]);
const float len[12]={5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

int main()
{

char maze1[23][35];
char fil=219;
cout<<endl;
filmazes(maze1, fil);
drawmaze1(maze1);
display_themaze1(maze1);

return 0;
}

void drawmaze1(char maze1[23][35])

{
int random;
int rowrandom;
int colrandom;
int r=0;
int c=0;
int loopcount=0;

for(loopcount=0; loopcount<=10; loopcount++)
{
srand(clock());
random = (rand() % 9);
rowrandom = (rand() % 23);
colrandom = (rand() % 35);

if(colrandom==34)
colrandom = colrandom - 1;
if(colrandom<34)
colrandom = colrandom + 1;
if(rowrandom==22)
rowrandom = rowrandom - 1;
if(rowrandom<22)
rowrandom = rowrandom + 1;

while(r<=len[random])
{
maze1[rowrandom+r][colrandom]=' ';
r++;
}
}

for(loopcount=0; loopcount<=10; loopcount++)
{
srand(clock());
random = (rand() % 9);
rowrandom = (rand() % 23);
colrandom = (rand() % 35);

if(colrandom==34)
colrandom = colrandom - 1;
if(colrandom<34)
colrandom = colrandom + 1;
if(rowrandom==22)
rowrandom = rowrandom - 1;
if(rowrandom<22)
rowrandom = rowrandom + 1;

while(c<=len[random])
{
maze1[rowrandom][colrandom-c]=' ';
c++;
}
}
}

void display_themaze1(char maze1[23][35])
{
for(int r=0;r<=22;r++)
{
for(int c=0;c<=34;c++)
{
cout << maze1[r][c];
}
cout <<endl;
}

}

void filmazes(char maze1[23][35], char fil)
{
for(int r=0;r<=22;r++)
{
for(int c=0;c<=34;c++)
{
maze1[r][c]=fil;
}

}
cout << endl;
}

****With all of your help and some other help from friends I managed to get this code. I still have some problems:
1) i need the rows and columns to not touch the last row or column on the end
2) for some odd reason my loopcount is not working

Any ideas again??
Thanks for all you help robblyth, chipperMDW, lionelhill, apatterno, and digitalpacman!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top