• Advertisement
Sign in to follow this  

c++ placing a character into a matrix

This topic is 862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I've created a 10x10 matrix class, which is 10x10. The 2d world is invisible as its going to act as a co-ordinate system, so rows and columns.
I've created a character and monster class but what the problem i am having is thinking of how i place my created character or monster into a random row and column on the matrix,

// A test function to fill a MatrixM object with random numbers
void Matrix(MatrixM *m, int r, int c) {



int count = 0;
//r = row, c = column
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++) {
//this just fills the 10x10 matrix with random numbers ( i want it to fill with 10 monsters randomly)
m->set(i, j, rand() % 10);
//m->set(i, j, count++);
}
}

/* THIS OBVIOUSLY ATM JUST STATES THEY ARE IN R10 C10
AS ITS NOT ACTUALLY PLACING THEM IN THE MATRIX?*/
Monster* mlist[10];
//give the monsters random co-ords
mlist[0] = new Monster(r, c);
mlist[1] = new Monster(r, c);
mlist[2] = new Monster(r, c);
mlist[3] = new Monster(r, c);
mlist[4] = new Monster(r, c);
mlist[5] = new Monster(r, c);
mlist[6] = new Monster(r, c);
mlist[7] = new Monster(r, c);
mlist[8] = new Monster(r, c);
mlist[9] = new Monster(r, c);


// print out the characters starting co-ordinates
for (int i = 0; i < 10; i++) {
mlist->print();
}


EDIT:

i was thinking something like this
row = m->get(i, 0);
column = m->get(0,j);

but how would i make it randomly pick a row and column Edited by Mr N1nja

Share this post


Link to post
Share on other sites
Advertisement
So the problem is picking 10 random spots in a 10x10 matrix?

#include <random>
#include <iostream>

int main() {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> distribution(0, 99);

  bool occupied[100] = {false};

  for (int i = 0; i < 10; ++i) {
    int monster_location;
    do {
      monster_location = distribution(gen);
    } while (occupied[monster_location]);
    occupied[monster_location] = true;
    std::cout << "row=" << (monster_location / 10) << ' ' << " col=" << (monster_location % 10) << '\n';
  }
}

Share this post


Link to post
Share on other sites

So the problem is picking 10 random spots in a 10x10 matrix?
 

#include <random>
#include <iostream>

int main() {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> distribution(0, 99);

  bool occupied[100] = {false};

  for (int i = 0; i < 10; ++i) {
    int monster_location;
    do {
      monster_location = distribution(gen);
    } while (occupied[monster_location]);
    occupied[monster_location] = true;
    std::cout << "row=" << (monster_location / 10) << ' ' << " col=" << (monster_location % 10) << '\n';
  }
}


Would this set them in my 10x10 matrix and not just give me an x and y co-ord between 0-10?
I have just played a bout with it and it does give me random co-ords thanks a lot of that little bit of help with random include

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement