I'm pretty much a beginner, and am wondering which of my code is optimized better, and why. This is a group project, and I had a disagreement with one of my partners over which code used memory better etc. I am in the opinion that neither codes are more optimized than the other.
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
usingnamespace std;
void monsterspawn (int x, int y, char Map[20][60]) //parameters needed are the player's coordinates and map
{
srand (time(NULL));
int monsters=0;
for (int MapX=0;MapX<60;MapX++) //check for monsters in entire map
{
for (int MapY=0; MapY<20; MapY++)
{
if (Map[MapX][MapY] == 'M')
{
monsters++;
}
}
}
for (monsters; monsters <11; monsters++) //limit of 10 monsters
{
int ranX = (rand() % (-20) + 20); //randomize coordinates from -10 to 10 (10 paces from hero) for onsters
int ranY = (rand() % (-20) + 20);
int MonX = x + ranX;
int MonY = x + ranY;
while ((MonX || MonY)>(-1))//coordiantes cannot be less than 0
{
while((MonX||MonY)<61)//coordinates cannot exceed 2d array
{
while(Map [MonX][MonY] !=' ')//coordinates of monster when he spawns rerunned until it is in an empty space
{
while (((x - MonX)>10))||((y - MonY)>10))// coordinates are within 10 range of character are rejected
{
ranX = (rand() % (-10) + 10); //randomize coordinates from -10 to 10 (10 paces from hero)
ranY = (rand() % (-10) + 10);
MonX = x + ranX;
MonY = x + ranY;
}
}
}
}
Map[MonX][MonY] = 'M';
}
}
This is my my code, which utilizes a bunch of while loops.
Here is the other code (I made this also at his behest)
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
usingnamespace std;
char monsterspawn (int x, int y, char Map[20][60]) //parameters needed are the player's coordinates and map
{
srand (time(NULL));
int monsters=0;
for (int MapX=0;MapX<60;MapX++) //check for monsters in entire map
{
for (int MapY=0; MapY<20; MapY++)
{
if (Map[MapX][MapY] == 'M')
{
monsters++;
}
}
}
for (monsters; monsters <10; monsters++) //limit of 10 monsters
{
int direction = rand() % 2+1
int ranX; //randomize coordinates from -10 to 10 (10 paces from hero) for onsters
int ranY;
int MonX;
int MonY;
do
{
if (direction == 1)
{
ranX = rand()% (-20) to (-10);
ranY = rand()% (-20) to (-10);
}
if (direction == 2)
{
ranX=rand()% (10) to (20);
ranY=rand()% (10) to (20);
}
MonX=x+ranX;
MonY=y+ranY;
}
while ((MonX || MonY)>(-1) && ((MonX||MonY)<61) && (Map [MonX][MonY] !=' ')//coordiantes cannot be less than 0
//or bigger than 60, as well as only occupy empty space
Map[MonX][MonY] = 'M';
}
}
This second code uses a 1 or 2 to determine whether RanX and RanY should be negative. This would then (supposedly, according to my partner) lower the chance of M not being close to the player.
Ignoring the errors, which one is more efficient in terms of using less memory and speed? Honestly, I don't see the difference between the output of the two codes.
Optimization of code that does not do the right thing is futile, but on a whim I say that his is better.
Looping until the Almighty Random Number Generator has given an acceptable answer makes no sense, if you could calculate the valid ranges. You can, except for the empty space check.
Furthermore, X and Y should probably be independent. Both versions discard perfectly good values, if the other is bad.
Honestly, you shouldn't be worrying about speed or memory unless you have tested the code yourself and notice it lagging or consuming tons of memory (like 10+ MB).