Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am creating a tetrisGame and this is the assignment: You have already displaye

ID: 3788968 • Letter: I

Question

I am creating a tetrisGame and this is the assignment:

You have already displayed the Tetris Bucket and started dropping the shapes. During this module, you stop the falling shape at the bottom of the Bucket. If there are shapes already stacked at the bottom of the bucket, then you stop the shape on top of other shapes. At the same time, you drop another shape from the top. Add plenty of narrative comments. Your program must be compilable and executable.

Here is my code so far, I am stuck with the checkBucket() and setShape() functions as I cant figure out how to store the shape in place and also how to transition to a new shape.

What I need is to stop the shape in place and store the placement and then call for another shape

// tetris.cpp : Defines the entry point for the console application.

//

#include <iostream>

#include <Windows.h>

#include <cstdlib>

#include <time.h>

using namespace std;

int shapeTopLeftX = 0;

int shapeTopLeftY = 6;

class TetrisShape {

public:

int shapeTopLeftY = 0;

int shapeTopLeftX = 6;

;

char shapeArray[4][4];

void populateShapeArray(int shapeType) {

switch (shapeType) {

case 2:

shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';

shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = 'X'; shapeArray[3][2] = 'X';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

break;

case 5:

shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = 'X';

shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = ' '; shapeArray[3][1] = 'X';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = 'X';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = 'X';

break;

case 1:

shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = 'X'; shapeArray[3][0] = 'X';

shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = 'X'; shapeArray[3][1] = 'X';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

break;

case 3:

shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = 'X'; shapeArray[3][0] = 'X';

shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

break;

case 4:

shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';

shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = 'X'; shapeArray[3][1] = 'X';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

break;

case 6:

shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = 'X';

shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = ' '; shapeArray[3][1] = 'X';

shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = 'X'; shapeArray[3][2] = 'X';

shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

break;

}

}

};

const int width = 12;

const int height = 25;

char bucket[height][width] = {

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',

'x','x','x','x','x','x','x','x','x','x','x','x',

};//end of class

void setCursorTo(int x, int y) {

HANDLE handle;

COORD position;

handle = GetStdHandle(STD_OUTPUT_HANDLE);

position.X = x;

position.Y = y;

SetConsoleCursorPosition(handle, position);

Sleep(500);

};

void drawBucket() {//function to draw the bucket boundaries

int a = width;

int b = width;

setCursorTo(0, 0);//setting start point of bucket

for (int x = 0; x <= height; x++) {//drawing sides of bucket

for (int y = 0; y < a; y++) {

cout << bucket[x][y];

}

if (b == width) {

cout << bucket[x][b] << endl;

}

}

}

void checkBucket(TetrisShape localTetrisShape) {

int x = height;

int y = width;

if (shapeTopLeftX == height - 3) {

setShape();

if (shapeTopLeftY == width, 0) {

shapeTopLeftY = shapeTopLeftY;

shapeTopLeftX++;

}

}

}

void setShape(TetrisShape localTetrisShape) {

}

void updateBucket(TetrisShape localTetrisShape) {//moving shape along the bucket

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

bucket[shapeTopLeftX + i][shapeTopLeftY + j] = localTetrisShape.shapeArray[i][j];

}

}

}

int main() {

drawBucket();

TetrisShape currentTetrisShape, nextTetrisShape;

TetrisShape shape;

int gameOver = 0;

srand(time(NULL));

int number = rand() % 6 + 1;

shape.populateShapeArray(number);

updateBucket(shape);

drawBucket();

while (gameOver == 0) {

int newShapeTopLeftX = shapeTopLeftX + 1;

shapeTopLeftX = newShapeTopLeftX;

checkBucket(shape);

updateBucket(shape);

drawBucket();

}

}

Explanation / Answer

#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h>


using namespace std;
class TetrisShape
    {
    public:
        int shapeTopLeftX;
        int shapeTopLeftY;
      
    bool active =true;
        TetrisShape()
        {
            shapeTopLeftX = 6;
            shapeTopLeftY = 0;
        }
        char shapeArray[4][4];
        int stoppointY[4];
      
        void populateShapeArray(int shapeType)
        {
            int a=0,b=1,c=2,d=3;
            switch(shapeType)
            {
            case 1:
                shapeArray[a][0] = ' '; shapeArray[b][0] = 'X'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
                shapeArray[a][1] = ' '; shapeArray[b][1] = 'X'; shapeArray[c][1] = ' '; shapeArray[d][1] = ' ';
                shapeArray[a][2] = ' '; shapeArray[b][2] = 'X'; shapeArray[c][2] = 'X'; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = ' '; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';
                stoppointY[a]=0;
                stoppointY[b]=3;
                stoppointY[c]=3;
                stoppointY[d]=0;
                break;
            case 2:
                shapeArray[a][0] = ' '; shapeArray[b][0] = 'X'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
                shapeArray[a][1] = ' '; shapeArray[b][1] = 'X'; shapeArray[c][1] = ' '; shapeArray[d][1] = ' ';
                shapeArray[a][2] = ' '; shapeArray[b][2] = 'X'; shapeArray[c][2] = ' '; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = 'X'; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';
                stoppointY[a]=0;
                stoppointY[b]=4;
                stoppointY[c]=0;
                stoppointY[d]=0;
                break;
            case 3:
                shapeArray[a][0] = ' '; shapeArray[b][0] = 'X'; shapeArray[c][0] = 'X'; shapeArray[d][0] = ' ';
                shapeArray[a][1] = ' '; shapeArray[b][1] = 'X'; shapeArray[c][1] = 'X'; shapeArray[d][1] = ' ';
                shapeArray[a][2] = ' '; shapeArray[b][2] = ' '; shapeArray[c][2] = ' '; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = ' '; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';
                stoppointY[a]=0;
                stoppointY[b]=2;
                stoppointY[c]=2;
                stoppointY[d]=0;
                break;
            case 4:
                shapeArray[a][0] = ' '; shapeArray[b][0] = 'X'; shapeArray[c][0] = 'X'; shapeArray[d][0] = ' ';
                shapeArray[a][1] = 'X'; shapeArray[b][1] = 'X'; shapeArray[c][1] = ' '; shapeArray[d][1] = ' ';
                shapeArray[a][2] = ' '; shapeArray[b][2] = ' '; shapeArray[c][2] = ' '; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = ' '; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';
                stoppointY[a]=2;
                stoppointY[b]=2;
                stoppointY[c]=1;
                stoppointY[d]=0;
                break;
            case 5:
                shapeArray[a][0] = 'X'; shapeArray[b][0] = 'X'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
                shapeArray[a][1] = ' '; shapeArray[b][1] = 'X'; shapeArray[c][1] = 'X'; shapeArray[d][1] = ' ';
                shapeArray[a][2] = ' '; shapeArray[b][2] = ' '; shapeArray[c][2] = ' '; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = ' '; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';

                stoppointY[a]=1;
                stoppointY[b]=2;
                stoppointY[c]=2;
                stoppointY[d]=0;
                break;
            case 6:
                shapeArray[a][0] = ' '; shapeArray[b][0] = 'X'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
                shapeArray[a][1] = ' '; shapeArray[b][1] = 'X'; shapeArray[c][1] = ' '; shapeArray[d][1] = ' ';
                shapeArray[a][2] = 'X'; shapeArray[b][2] = 'X'; shapeArray[c][2] = ' '; shapeArray[d][2] = ' ';
                shapeArray[a][3] = ' '; shapeArray[b][3] = ' '; shapeArray[c][3] = ' '; shapeArray[d][3] = ' ';
                stoppointY[a]=3;
                stoppointY[b]=3;
                stoppointY[c]=0;
                stoppointY[d]=0;
                break;
            }
        }
    };

    const int width = 12;
    const int height = 25;
    int bucketobjectheight[width];
    char bucket [height][width] =
    {
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'},
                                {'x','x','x','x','x','x','x','x','x','x','x','x'}

    };
    void InitializeBucket()
    {
        for(int i =0;i<width;i++)
        {
            bucketobjectheight[i] = 20;

        }

    }


    void setCursorTo(int x, int y)
    {
        HANDLE handle;
        COORD position;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        position.X = x;
        position.Y = y;
        SetConsoleCursorPosition(handle, position);
    }

    void drawBucket()
    {
        setCursorTo(0,0);
            for(int x = 0; x <= height; x++)
            {

                for(int y = 0; y < width; y++)
                {
                    cout<<bucket[x][y];
                }
                cout<<endl;
              

            }
    }
  
    int movedown =0;

    void updateBucket(TetrisShape localTetrisShape)
    {

        movedown++;

        int shapeTopLeftX=6;

        for(int i =0;i<width;i++)
        {
            for (int a = 0;a<4;a++)
            {
              
                if(i>i-4)
                {
                    break;
                }
              
                if(localTetrisShape.stoppointY[a]+movedown >= bucketobjectheight[i+a] && localTetrisShape.active == true)
                {
                    for(int j = 0; j < 4; j++)
                    {
                      
                        bucketobjectheight[shapeTopLeftX+j] -= localTetrisShape.stoppointY[j];
                    }
                  
                    localTetrisShape.active= false;
                }
            }

        }

         
            if(localTetrisShape.active==true)
            {
                    for(int i = 0; i < 4; i++)
                    {
                        for(int j = 0; j < 4; j++)
                        {
                            bucket[movedown+i][shapeTopLeftX+j] = localTetrisShape.shapeArray[i][j];
                        }
                    }
            }
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        TetrisShape shape;
        int gameOver = 0;
        InitializeBucket();

      while(gameOver == 0)
        {
              
            srand(time(NULL));
            int number = rand() % 6 + 1;
            shape.populateShapeArray(1);
            updateBucket(shape);
            drawBucket();
            Sleep(500);

        }
        return 0;
    }