我有一个二维char指针数组,其中包含一个7x7大小的棋盘游戏。我的数组如下所示:
我有这样的代码,可以将元素在一列中向上移动1个位置:
void userMove(char **boardOfGame, int *sizeOfBoardGame, char typeOfUserTile, int userChoice)
{
int countElement = 6;
char tempElement;
for (int i = 6; i > 0; i--)
{
if (!isspace(boardOfGame[i][userChoice]))
{
countElement--;
}
}
if (typeOfUserTile == '+' || typeOfUserTile == '-' || typeOfUserTile == '|')
{
if (boardOfGame[userChoice][6] == 'X')
{
cout << "A locked tile is preventing your tile from being added. Try to be more careful next turn." << endl;
}
if (boardOfGame[6][userChoice] == ' ')
{
//boardOfGame[6][userChoice] = printf("\033[1;34m%c\033[0m\n",typeOfUserTile);
boardOfGame[6][userChoice] = typeOfUserTile;
}
else if (boardOfGame[6][userChoice] == '+' || boardOfGame[6][userChoice] == '-' || boardOfGame[6][userChoice] == '|')
{
for (int i = countElement + 1; i > countElement; i--)
{
//memmove (&boardOfGame[i-1][userChoice], &boardOfGame[i][userChoice], 1);
boardOfGame[i-1][userChoice] = boardOfGame[i][userChoice];
if (i < 6 && i > 0)
{
boardOfGame[i][userChoice] = boardOfGame[i + 1][userChoice];
}
if (i == 0)
{
boardOfGame[i][userChoice] = boardOfGame[i+1][userChoice];
}
boardOfGame[6][userChoice] = typeOfUserTile;
}
}
}
}
它在前三行中正常工作,但在其余三行中有问题。请帮我解决它,我对此事有些犹豫。任何帮助。谢谢。
答案 0 :(得分:1)
由于某些未使用的变量,重复的代码以及未使用简短/单一职责的函数,很难精确地确定您的代码功能,无论如何,很容易在所需列中上移矩阵:
const int rowSize = 7;
const int colSize = 7;
char scr[rowSize][colSize];
void shiftUp(int colInd)
{
for (int r = 0; r < rowSize - 1; r++)
{
scr[r][colInd] = scr[r + 1][colInd];
}
scr[rowSize - 1][colInd] = '?';
}