Question about displaying content in a 2D array of pointers

I'm kinda stuck and hope this is something obvious. I'm making a simple Stratego game. Right now, I'm working on a function that displays the grid. The grid is a 10 X 10 array of objects defined as NULL.

In a previous function (assuming I only run that function once), I dynamically create a new object and make the grid point to that object. Let's say I make grid[0][0] point to that new object.

BUT.. When I display the grid, at grid[0][0] (where I placed my new object), I get a memory address(in hex form).

The object I made has 2 members:

1
2
3
4
5
6
enum color {red,blue};

struct basicPiece{
color team;
int value;
};


I want to display the object in the grid as "r1" (i.e. color & value) as opposed to getting its memory address. Do you have suggestions on how I can display this right?

Currently, in my displayGrid function, I've got a complicated set of if statements within nested for loops to output my grid but it just isn't working out.
I've got a complicated set of if statements within nested for loops to output my grid but it just isn't working out.


It's probably not working because it's so complicated.

Break this job down into tasks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
string GetDisplayString(const basicPiece* pc)
{
  if(!pc)  // no piece here
    return "__";  // or whatever you want for a blank space
  else
  {
    // otherwise the piece exists.
    stringstream s;  // stringstream for text formatting

    // check the color
    if(pc->team == red)   s << 'r';
    else                  s << 'b';

    // get the number
    s << pc->value;

    // and now you have your name!
    return s.str();
  }
}

//================

void PrintGrid()
{
  for(int y = 0; y < height; ++y)
  {
    for(int x = 0; x < width; ++x)
    {
      cout << GetDisplayString(  your_grid[y][x]  ) << ' ';
    }
    cout << '\n';
  }
}



EDIT: Also... http://www.cplusplus.com/forum/articles/28558/
Last edited on
Wow I never thought of calling a function within the nested loops. Cool. Thanks for that idea! That .. gives me a lot of room. I don't have to try to cram all my into a buncha loops--makes the syntax much easier to read and think through. Way cool.

And.. this string stream.. I've NEVer heard of them. After reading up on it, I realized why it helps me do exactly what I want. Thanks! How did you find out about this? I found it here: http://www.cplusplus.com/reference/iostream/ but that's because I was looking for it. Did you just find it one day or read it in a text you bought?

I initially saw that data type and replaced it with 'string' since I was familiar with that. It worked except for when I wanted to append the integer to the end of the string--kept on getting cool symbols.

my code changed 's' to a string type. Then I tried doing this:
1
2
s+=pc->value;
return s;


I suppose this is true because it was converting pc->value into a string.. reading it as an ASCII value?

That's when I decided there was a reason why you used stringstream. Then I learned about them :D. Yay.

You've been a great help! Thanks!

EDIT: I saw the link you added in your "edit". Thanks for the info.
Last edited on
Wow I never thought of calling a function within the nested loops.


Functions are great for breaking up large tasks into smaller, more managable ones. Figuring out exactly how to do it effectively takes some time though. ;)

How did you find out about this?


I honestly don't remember. It's been so long.

I suppose this is true because it was converting pc->value into a string.. reading it as an ASCII value?


That seems most likely, yes.

Anyway, glad I could help. ^^
Topic archived. No new replies allowed.