0

So I have one char 2D pointer dynamic array which contains my elements like this: Picture

Please ignore all the number and *** symbol and just focus on the X and space and '-', '+', '|' symbol.

When the user chooses one lane from 1 to 6, the program will push one element to the top as shown in the picture. If the last element is a blue symbol, I will need to remove it and keep pushing other elements in the array until I can form a straight line to win the game.

For example, if blue symbol '+' is in arr0 then the next time a symbol is inserted in the column, the blue symbol '+' will be removed and array will add another symbol from the bottom.

Here is my code so far for pushing the elements:

int countElement = 6;
char tempElement;
for (int i = 0; i < *sizeOfBoardGame; 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 = 6; i > countElement; i--)
        {
            boardOfGame[i-1][userChoice] = boardOfGame[i][userChoice];
            boardOfGame[6][userChoice] = typeOfUserTile;
        }
    }               
}

This is my output: Picture2

Do you guys also know how to insert a color char to the array like shown in the picture? I tried to use the //boardOfGame[6][userChoice] = printf("\033[1;34m%c\033[0m\n",typeOfUserTile); but it only print a new color symbol to the output instead of assigning it to be the value of my 2D array.

Dat To
  • 45
  • 7
  • For colour have a look here: https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal Your color code appears correct, check that typeOfUserTile is a valid char for the colour code. For the shifting, I think that might be your assignment lol, typically you won't get answers if it appears to be an assignment or lab question. Usually if you want to shift something out of an array like that you just override it, you likely don't need to reallocate or delete on a per item basis. Hope that points the right direction for you. – David Ledger Nov 28 '18 at 04:42
  • Since this is C++, using a `vector >` would make the insertions and removals simple calls to member functions, but if you are stuck with your plain 2D array, look at `memmove`. – David C. Rankin Nov 28 '18 at 04:44
  • Dear David, Sadly i am not allow to use memmove or vector or arrayList or anything like that. For the color part, I have to use an enum to assign the color, and I'm kinda stuck on this too. – Dat To Nov 28 '18 at 04:54
  • Dear David, Can you help me edit this memmove code to work properly, please? This is what I have: memmove (&boardOfGame[i-1][userChoice], &boardOfGame[i][userChoice], 1); boardOfGame[6][userChoice] = typeOfUserTile; This code is good for inserting only one row or two row. If I insert it the third times, all the code will turn weird. For example, - + in the first two row, then the next '+' in next row will become ' + - -' . – Dat To Nov 28 '18 at 07:44

0 Answers0