### Monsters....aggh

I am trying to write a simple, I use the word loosely lol, dungeon crawl game as described in a post on this forum.

 Dungeon Crawl Requires: variables, data types, and numerical operators basic input/output logic (if statements, switch statements) loops (for, while, do-while) arrays Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters. i.e.

The problem I am having is with:

 ★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)

My monsters should move in a random direction each turn, but sometimes they don't move or they'll move erratically.
Here's the code:
 ``12345678910111213141516171819202122232425262728293031323334`` ``````char movM(char grid[][10]){ int i,j,r=rand()%100; for (i=0;i<10;i++){//find monster for(j=0;j<10;j++){ r=rand()%4+1; if (grid[i][j]=='M'){ switch(r){ case 1: grid[i][j]='.'; grid[i-1][j]='M'; break; case 2: grid[i][j]='.'; grid[i+1][j]='M'; break; case 3: grid[i][j]='.'; grid[i][j+1]='M'; break; case 4: grid[i][j]='.'; grid[i][j-1]='M'; break; default: cout<<"wtf"; } } } }``````

I've tried doing it with pointers instead and I get the same result. Any thoughts?
did you check if grid is not exceeded sometimes? if your monster is at 9,9 and you move it to 9,10 ? also have in mind other collisions (if monster is moving to field with other monster).

its hard to track error without knowing whole program structure

edited:
also see this problem:
if your loops are in 1,1. and your monster is there, moving to 1,2.

when loop continues, and come to 1,2 ,your monster (grid[][] == 'm') will move again (twice or more in the same function) :)
Last edited on
In the other version I wrote, I had bounds in place to keep monsters from running into one another and leaving the grid. I did not, however, think about the same monster being evaluated twice during the loop sequence. I think that might be it. I'll change it and post back my results. Thank you.
The same monster being evaluated more than once was the issue. I fixed it by moving the monsters to their new location on a different, temporary grid while the loop finished going through the rest of the main grid. Then, put the monsters back on the main grid in their new locations from the temp grid. Thank you very much.
Topic archived. No new replies allowed.