gpt4 book ai didi

c++ - 如何移动二维指针数组中的行元素

转载 作者:行者123 更新时间:2023-11-30 04:54:33 29 4
gpt4 key购买 nike

我有一个二维字符指针数组,其中包含一个大小为 7x7 的棋盘游戏。我的数组将如下所示:Board

我有我的代码将一列中的元素向上移动 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;
}
}
}
}

它在前三行中工作正常,但在其余部分中存在问题。请帮我解决这个问题,我有点卡在这件事上了。什么都有帮助。谢谢。

我的输出:enter image description here

最佳答案

很难准确地计算出您的代码功能(由于一些未使用的变量、重复的代码以及未使用短/单一职责功能),无论如何很容易在所需列中向上移动矩阵:

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] = '?';
}

关于c++ - 如何移动二维指针数组中的行元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53525213/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com