Here is my struct:
struct Checker
{
short row;
short col;
unsigned short number;
short color;
};
Now, I have to also make another stru开发者_JAVA技巧ct to represent a checkers board:
struct Board
{
Checker checkers[2][13]; // Zeroth entry of 13 is not used.
Checker *grid[8][8]; // each entry holds Null or an address
// of an element in the checkers array
};
If it matters, checkers[0-1] represents what side, [1-12] are unique numbers for each piece.
Anyways, I have a an 8x8 grid, that either points to NULL, or a checker piece. What i want to do is to be able to access that checker not by its unique ID (checkers[0][5] for instance), but instead by its position on the grid.
How can I access the data members in this fashion? I'l try to summarize:
Declared a Board gameBoard. Set up the grid so that I have a checkers piece at [0][0]
. This would be checkers[0][1]
. So instead of doing cout <<checkers[0][1].number
, I'd like to be able to print the checker's number without knowing its ID, and instead knowing that that specific checker is at [0][0]
Thanks.
cout << grid[0][0]->number;
If the grid is a 2-dimensional array of pointers to Checker structs, then grid[0][0] is a pointer to the Checker at that location (0, 0). The ->
syntax dereferences the pointer and then accesses the number
field.
If I am misunderstanding your question or my response fails, please let me know and I'll happily delete. It's late. :)
精彩评论