visual studio – How to Animate item going from top to bottom in 2D Array in C++


I’ve been developing a CLI Tetris game in C++ and I’ve been trying to figure out how to make the item [] in my 2d grid be animated and move from the top to the bottom of the grid. I want to do it in a way where I don’t need to hardcode location values and it will continue animating downwards no matter moving the item left or right. Here is my current working code.

string grid[4][3] = {
{".", ".", "."},
{".", ".", "."},
{".", ".", "."},
{".", ".", "."}
};

void display_grid(string _grid[4][3]);

int main()
{
    string key;
    
    int row = 0;
    int column = 1;
    
    
    grid[row][column] = "[]";

    display_grid(grid);

    while (true) {

        key = _getch();

        if (key == "s") {
            if (row + 1 < 4) {
                swap(grid[row][column], grid[row + 1][column]);
                row++;
            }
            if (row == 3) {
                row = 0;
                column = 1;
                grid[row][column] = "[]";
            }
        }
        if (key == "d") {

            if (row == 3) {
                grid[row][column];
            }

            else if (column + 1 < 3) { // stops character from going outside grid
                swap(grid[row][column], grid[row][column + 1]);
                column++;
            }
        }
        if (key == "a") {
            if (row == 3) {
                grid[row][column];
            }

            else if (column - 1 >= 0) {
                swap(grid[row][column], grid[row][column - 1]);
                column--;
            }
        }
        int ro = 0;
        int col = 0;

        if (grid[ro + 3][col + 0] == "[]" and grid[ro + 3][col + 1] == "[]" and grid[ro + 3][col + 2] == "[]") {

            grid[ro + 3][col + 0] = "-";
            grid[ro + 3][col + 1] = "-";
            grid[ro + 3][col + 2] = "-";
        }

        if (key == "q") {
            return 0;
        }

        system("CLS");
        display_grid(grid);
        
    }

};


void display_grid(string _grid[4][3]) {

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 3; column++) {
            //grid[0][1] = "[]";
            cout << "\t" << grid[row][column];
        }
        cout << "\n" << "\n";
    }
};

Here is some code I was experimenting with which creates a top – bottom animation but with hardcoded value:

string newthing[4][3];

void A(int x, int y) {

    system("CLS");
    for (int c = 0; c < 4; c++) {
        for (int r = 0; r < 3; r++) {
            newthing[c][r] = "o";
        }
    }
    newthing[x][y] = "[]";

    for (int c = 0; c < 4; c++) {
        for (int r = 0; r < 3; r++) {
            cout << "\t" << newthing[c][r];
        }
        cout << "\n" << "\n";
    }
}


void moveToLocation(int x, int y)
{
    A(x, y);
}



int main()
{
    moveToLocation(0, 1);
    Sleep(1000);
    moveToLocation(1, 1);
    Sleep(1000);
    moveToLocation(2, 1);
    Sleep(1000);
    moveToLocation(3, 1);
    Sleep(10000);
}

Leave a Reply

Your email address will not be published. Required fields are marked *