Declaring a random amount of objects in c++ -


so have decent amount of experience in other languages, new c++. trying create simple text based rpg there is player , random number of enemies. problem when try move around in never run enemy, i'm either having problem creating actual enemy objects, or code checking if player @ same location enemy isn't working, or maybe it's else. i'd appreciate, help. thanks.

btw sorry formatting got kind of messed up.

#include <iostream> #include <random> #include <string>  using namespace std;  #define 1 #define right  class character { public:     int health, armour, speed, x, y, attackpower;     character()     {         health = 100;         armour = 100;         speed = 1;     }      bool istouching(character character)     {         if (x == character.x && y == character.y)         {             return true;         }         else         {             return false;         }     }     void move(string dir)     {         if (dir == "up")         {             y += speed;         }         else if (dir == "down")         {             y -= speed;         }         else if (dir == "right")         {             x += speed;         }         else if (dir == "left")         {             x -= speed;         }     }      void takedamage(int damage)     {         if (armour > damage)         {             armour -= damage;         }         else         {             if (armour > 0)             {                 int remainingdamage = damage - armour;                 armour = 0;                 health -= remainingdamage;             }         }         health -= damage;     }      void attack(character* character)     {         character->takedamage(attackpower);     }  };  class player: public character { public:     player()     {         health = 100;         armour = 100;         speed = 1;         x = 10;         y = 10;         attackpower = 50;      } };   class enemy : public character { public:     enemy()     {         health = 100;         armour = 0;         speed = 1;         x = rand() % 100;         y = rand() % 100;         attackpower = 20;      } };  int main() {      character player;       enemy *enemies;     int numenemies = rand() % 30 + 20;     enemies = new enemy[numenemies];      while (true)     {         string input;         cout << "enter command: ";         cin >> input;         if (input == "exit" || input == "quit")         {             break;         }         if (input == "move")         {             string direction;             cout << "pick direction: ";             cin >> direction;             player.move(direction);              (int = 0; < sizeof(enemies) / sizeof(int); i++)             {                 if (player.istouching(enemies[i]))                 {                     cout << "you ran enemy!" << endl;                     cout << "what do?: ";                     string interactioninput;                     cin >> interactioninput;                     if (interactioninput == "attack")                     {                         player.attack(&enemies[i]);                         enemies[i].attack(&player);                         cout << "enemy @ " << enemies[i].armour << " armour , " << enemies[i].health << " health" << endl;                         cout << "you @ " << player.armour << " armour , " << player.health << " health" << endl;                     }                 }             }          }        }      system("pause");     return 0; } 

sizeof(enemies) equal 4 or 8, depending on size of virtual memory address space.

sizeof(int) equal 2 or 4, depending on compiler definition (based on underlying hw).

so sizeof(enemies) / sizeof(int) somewhere between 1 , 4.

having said that, can use numenemies instead.


if allocated enemies array statically (enemy enemies[...]), use:

sizeof(enemies)/sizeof(enemy) sizeof(enemies)/sizeof(*enemies) sizeof(enemies)/sizeof(enemies[0]) // or other index 

but since allocating dynamically, treated pointer (with size of 4 or 8 bytes).


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -