visual studio – How to Update Grid Item Position 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 to 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 moving downwards no matter if the controls movement of the item is left or right.

Here is some code I was experimenting with which creates top – bottom movement but with hardcoded values:

#include <string>
#include <iostream>
#include <thread>

using namespace std;

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 *