I'm trying to make a dungeon crawler but I need to make the monster move around each turn.
Here's what I tried:
My map is initialized like this:
char map[10][10];
and this is how I spawn the monsters to avoid overwriting:
void fieldinfo::RandomizeMonsterPositions()
{
int Mcounter=0;
amountEnemies=10;
int i=0;
srand(time(0));
do
{
MposX=rand()%9;
MposY=rand()%9;
if(map[MposX][MposY]==enemy)
{
//do nothing
}
else if(map[MposX][MposY]==player)
{
//do nothing
}
else
{
map[MposX][MposY] = enemy;
tempPosX[i]=MposX;
tempPosY[i]=MposY;
i++;
Mcounter++;
}
}while(Mcounter<10);
}
You can see that I tried to "save" each monster's position in the tempPosX/Y
variables to use them here, in my movement logic:
void fieldinfo::RandomizeMonsterMovement()
{
int mouvement;
for(int i; i<amountEnemies; i++)
{
mouvement=rand()%0;
switch(mouvement)
{
case 0:
tempPosX[i]++;
MposX=tempPosX[i];
break;
case 1:
tempPosX[i]--;
MposX=tempPosX[i];
break;
case 2:
tempPosY[i]++;
MposY=tempPosY[i];
break;
case 3:
tempPosY[i]--;
MposY=tempPosY[i];
break;
}
}
}
This doesn't work and I don't know why?
rand()%0
? That's undefined behaviour, and a SIGFPE in most cases I've seen... \$\endgroup\$