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;
}
}
}