I am creating a game in C programming. The game is there is a player and goal inside the map. The win condition is the player hit goal position. However, when every move of the player, there will be a floor collapse randomly in the map. Therefore, the players will be blocked by the char 'X'.
The thing I need to be done is update the map[i][j] then print the map. Therefore, I am trying to change the charmap by the function "XGenerater". And I used the memcpy the char map for updating the map in the pRefreshParams. ANd segfault(core dumded).
I tried to placeObj in the chr** map in other function. The 'X' displayed one in the map, after I move the player. Which means the the 'X' is jumping in the map. And it is randomly generate the 'X'
However, my question is how to change the char** map value?
char** createMap(int pMapSize[])
{
int sizeRow = pMapSize[IDX_MAP_NROW];
int sizeCol = pMapSize[IDX_MAP_NCOL];
char** map = malloc(sizeRow * sizeof(char*));
int i = 0;
for(i = 0; i < sizeRow; i++)
map[i] = malloc(sizeCol * sizeof(char));
return map;
}
void XGenerater(char** map, const int sizeRow, const int sizeCol){
void initRandom();
int row, col;
do
{
row = random(0, sizeRow - 1);
col = random(0, sizeCol - 1);
}while (map[row][col] != ' ');
map[row][col] = 'X';
int moving(void* pRefreshParams[], char cUserInput)
{
int gameStatus = GS_NONE_GOAL;
int aiPlayerCp[ARR_SIZE_OBJ];
char* aiMapCp[2];
/*int validPosition = FALSE;*/
char** map;
int *pMapSize, *pGoal, *pPlayer;
unpackRefreshParams(pRefreshParams, &map, &pMapSize, &pGoal, &pPlayer);
copyObj(aiPlayerCp, pPlayer);
/*memcpy(aiMapCp, map, sizeof(char)*ARR_SIZE_OBJ);*/
copyMap(aiMapCp, map);
performMove(aiPlayerCp, pMapSize, cUserInput);
XGenerater(aiMapCp, pMapSize[IDX_MAP_NROW], pMapSize[IDX_MAP_NCOL]);
copyObj(pPlayer, aiPlayerCp);
/*memcpy(map, aiMapCp, sizeof(char)*ARR_SIZE_OBJ);*/
/*copyMap(map, aiMapCp);*/
if (pPlayer[IDX_COL] == pGoal[IDX_COL] && pPlayer[IDX_ROW] == pGoal[IDX_ROW])
{
refreshMap(pRefreshParams);
gameStatus = GS_PLAYER_GOAL;
}
return gameStatus;
}