C program 2d array question

lets say i have a 2d array of character named board like this:

1 2 3
4 5 6
7 8 9

and there is a player of character p that is going to be put into the board

p 2 3
4 5 6
7 8 9

and he moves to a new position, but the value at where p was should stay at (board[0][0] = 1)like this:
1 2 3
4 5 p
7 8 9

how can we fix the movement of character p so that the elements of board is preserved(without swapping the elements of where p is going to)?

here is a standard swapping function of which its incorrectly swapping value to a new position:

``````void swapPositions(char board[ROWS][COLS], int x1, int y1, int x2, int y2)

{
char temp = board[x1][y1]; //store the value at (x1, y1) in the temp

board[x1][y1] = board[x2][y2]; //store the value at (x2, y2) to position (x1, y1)

board[x2][y2] = temp; //store the value of temp to position (x2, y2)
}
``````
``````void print_board(char board[ROWS][COLS]) //print the board to screen

{

int i, j;

for(i = 0; i < ROWS; i++){

for(j = 0; j < COLS; j++){

printf("%c\t", board[i][j]);

}

printf("\n");

}

}

void mark_board(char board[ROWS][COLS],int x, int y, char player)

{

for (int i = 0; i < ROWS; i++)

{

for (int j = 0; j < COLS; j++)

{

board[x][y] = player;

}

}

}
``````

Hello and welcome to the forums @boardsurfer94494

A good question I think is, why don’t you want to do the swapping? It seems like an effective method of doing what you want.

My first thought, what if instead of changing the board, which you don’t wanna do, you override the `printf()` in the `print_board()` function? As it is going through the `for` loops, it could be checking, before printing, to see if it is on the portion of the board were the player is located, and then print the `'p'` instead of the number at that spot in the array. Though I think this may make the program slightly slower since it has to run through the condition every time it loops as opposed to your current process of swapping out the characters.

Just as an idea:

``````//                                              Extra arguments
//                                                 /        \
void print_board(char board[ROWS][COLS], int player_x, int player_y) {

int i, j;

for(i = 0; i < ROWS; i++){

for(j = 0; j < COLS; j++){
/*
include an if here to check if the location is
equal to board[player_x][player_y]
*/
}
}
}
``````