Author Topic: Copying chtype Arrays  (Read 9626 times)

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Copying chtype Arrays
« on: May 08, 2012, 05:46:41 AM »
Aloha again,

Another question, how do I copy a chtype array into another chtype array?  For example, you are at level 15 in the dungeon.  You go down the stairs, and it takes chtype level16 and copies it to chtype currentLvl.  Then it prints currentLvl.

I tried "currentLvl = level16", but that didn't work.  Chtype arrays aren't assignable, I guess.  I also considered the function "cpystr", but that's for char arrays, not chtype arrays.  Is there a special nCurses function, or do you have to just do it one-by-one (copy level16[1][1], currentLvl[1][1] = level16[1][1], etc)?
{O.o}
 |)__)
   ” ”   o RLY?

NON

  • Rogueliker
  • ***
  • Posts: 349
  • Karma: +0/-0
    • View Profile
    • Infra Arcana
    • Email
Re: Copying chtype Arrays
« Reply #1 on: May 08, 2012, 09:41:09 AM »
Why not just write something like this??

Code: [Select]
void loadLevel(/*level number or pointers to arrays*/) {
  for(int y = 0; y < NR_MAP_Y_CELLS; y++) {
    for(int x = 0; x < NR_MAP_X_CELLS; x++) {
       currentLvl[x][y] = newLevel[x][y];
    }
  }
}

But that seems so obvious that I probably missed the point of this question.
Happy is the tomb where no wizard hath lain and happy the town at night whose wizards are all ashes.

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: Copying chtype Arrays
« Reply #2 on: May 08, 2012, 04:32:53 PM »
Why not just write something like this??

Code: [Select]
void loadLevel(/*level number or pointers to arrays*/) {
  for(int y = 0; y < NR_MAP_Y_CELLS; y++) {
    for(int x = 0; x < NR_MAP_X_CELLS; x++) {
       currentLvl[x][y] = newLevel[x][y];
    }
  }
}

But that seems so obvious that I probably missed the point of this question.
I could use that (and that was the point of the question), but I didn't really want to use that.  I wanted more

Code: [Select]
curLvl = newLevel;

or

Code: [Select]
cpychtype(curLvl, newLevel);

It's not that the nested for loops don't work, it's that I'd rather use more concise code (I might have to repeat this over 30 times, once for each level)

And: Wow! Code tags! Never knew we had those things.
{O.o}
 |)__)
   ” ”   o RLY?

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: Copying chtype Arrays
« Reply #3 on: May 08, 2012, 10:24:57 PM »
Another solution is to use the memcpy function:
Code: [Select]
memcpy(newLevel, currentLvl, sizeof(currentLvl))
You can also put your map inside a struct, then you can copy structs with newLevel=oldLevel (but this is inconvenient).

It's not that the nested for loops don't work, it's that I'd rather use more concise code (I might have to repeat this over 30 times, once for each level)

Then create a function which does that (as NON did). This is actually a general rule of good software design. Don't repeat yourself. Create functions instead. If you find yourself repeating something (anywhere in your game), then probably you should use a function, a loop, or another programming construct for it.

Moreover, you probably should not have 30 arrays level1, level2, level3, ..., level15, level16, ... You should have a three-dimensional array instead, level[30][HEIGHT][WIDTH]. Then the ">" simply does { lev++; loadLevel(level[lev], currentLvl); }

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: Copying chtype Arrays
« Reply #4 on: May 09, 2012, 04:49:31 AM »
Then create a function which does that (as NON did). This is actually a general rule of good software design. Don't repeat yourself. Create functions instead. If you find yourself repeating something (anywhere in your game), then probably you should use a function, a loop, or another programming construct for it.
Good advice, thanks :)

Moreover, you probably should not have 30 arrays level1, level2, level3, ..., level15, level16, ... You should have a three-dimensional array instead, level[30][HEIGHT][WIDTH]. Then the ">" simply does { lev++; loadLevel(level[lev], currentLvl); }
Wait, what? You can make 3D arrays?!  That's very cool.  Is this available in C?
{O.o}
 |)__)
   ” ”   o RLY?

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: Copying chtype Arrays
« Reply #5 on: May 09, 2012, 07:19:05 AM »
Yes, you can make as many dimensions as you can...

You can create an array of anything, and a 3D array in C is simply an array of arrays of arrays.

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: Copying chtype Arrays
« Reply #6 on: May 09, 2012, 07:20:33 PM »
Wow. That's great.  Thanks, Z!
{O.o}
 |)__)
   ” ”   o RLY?