C Program 2D arrays with structs and pointers

C Program 2D arrays with structs and pointers



#include
#include

#define NUM_ROWS 20
#define NUM_COLS 20

#define WALL 0
#define FLOOR 1
#define PASS 1
#define NOT_PASS 0

typedef struct playerChar {
int HP;
} PlayerChar;

//General struct with pointers to every type of 'creature' that can be shown on map
typedef struct charPointers {
PlayerChar * pPointer;
} CharPointers;

int main (void) {
initscr();
noecho();
curs_set(0);
resize_term (NUM_ROWS, NUM_COLS);

int i, j;
int ** mapArray = (int **) malloc (NUM_ROWS * sizeof (int *));
int ** passableArray = (int **) malloc (NUM_ROWS * sizeof (int *));
int ** charLoc = (int **) malloc (NUM_ROWS * sizeof (int *));


for (i = 0; i < NUM_ROWS; i++) {
mapArray [i] = (int *) malloc (NUM_COLS * sizeof (int));
passableArray [i] = (int *) malloc (NUM_COLS * sizeof (int));
charLoc [i] = (int *) malloc (NUM_COLS * sizeof (CharPointers));
for (j = 0; j < NUM_COLS; j++) {
if (i == 0) {
mapArray [i][j] = WALL;
passableArray [i][j] = NOT_PASS;
}
else if (i == (NUM_ROWS - 1)) {
mapArray [i][j] = WALL;
passableArray [i][j] = NOT_PASS;
}
else if (j == 0) {
mapArray [i][j] = WALL;
passableArray [i][j] = NOT_PASS;
}
else if (j == (NUM_COLS - 1)) {
mapArray [i][j] = WALL;
passableArray [i][j] = NOT_PASS;
}
else {
mapArray [i][j] = FLOOR;
passableArray [i][j] = PASS;
}
charLoc[i][j].CharPointers.pPointer = NULL;
}
}


No comments:

Post a Comment