1

I have a 2D char pointer array which contains a board game with size 7x7. My array will look like this: Board

I have my code to shift elements in one column up 1 position like this:

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

It works properly in the first three rows, but it has issues in the rest. Please help me fix it, I'm kinda stuck on this matter. Anything helps. Thank you.

My output:enter image description here

2
  • for (int i = countElement + 1; i > countElement; i--) : in practice, it simply means i = countElement. Is it your intention ? Commented Nov 28, 2018 at 18:13
  • SO I have the countElement to check how many value do I have in the userChoice column. Then in my next looping, I will assign the arr[i-1][col] = arr[i][col]. It will skip the already accessed element and start with a new one. Commented Nov 28, 2018 at 18:46

1 Answer 1

1

It's hard to figure out your code function precisely (due to some unused variables, duplicated codes, and not using short/single-responsibility functions), anyway it's easy to shift-up a matrix in a required column:

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] = '?';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.