This has been driving me nuts the past few days, I am supposed to spawn random monsters in a text based game (Win32 App) and have them randomly move one unit (x or y co-ordinate) each time the user moves, however each monster seems to be assigned the exact same x and y co ordinates despite me using the rand()%4 function.
Here is just the relevant code. Am I missing something completely obvious? Any help will be greatly appreciated :)
changing the rand_x and rand_y as well as rand_MOVE to 1 didnt seem to work, I think the problem actually lies in the main() code where I've put rand_x=rand()%11; // Random spawn
rand_y=rand()%11; // co-ordinates
this is what seems to assign ALL monsters the same x and y values
Thanks for the reply Peter, however I just tried this and it didn't seem to work.
The 3 monsters I have created in the main all still have the exact same x and y start co ords and move together rather than being 'random'
That's because you set rand_x and rand_y before the loop. The value of rand_x and rand_y never change inside the loop so you always pass the same values to the Hero and the three Monster constructor calls.
I don't know if you should actually be creating the hero and the monsters inside the loop. I mean, do you really want a new hero and new monsters each iteration of the loop.
You are not updating the monsters that you have created. If you want monsterMOVE to change the position of a monster you should be using a Monster object inside that function instead of changing rand_x and rand_y which has nothing to do with any monster.