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

During this module, implement one of the very first steps of Tetris, \"Display t

ID: 3766405 • Letter: D

Question

During this module, implement one of the very first steps of Tetris, "Display the Bucket". You need to call the function inside the game loop so that it is always displayed. But, you might need to initialize it once before the game loop. Below you will find some detailed instruction on the bucket implementation.

Bucket will be a 2-D array of char of 25 x 12 dimension.

Fill the left border (column 1), right border (column 12), and bottom border (row 25) of the bucket with any char that you like to create the border with. Remember, the array index is '1' lower than the actual # of columns. This can be your "initializeBucket()" function. Fill the other cells with empty string ' '. You know that, to go over a 2-D array you need two nested for loops. Right? You also need conditional statements.

Display the bucket in a game loop. How do you do that? You use two for loops and use a "cout....". You can name the function "displayBucket()".

For Windows, use the "Windows.h" library. Use the function below to put your cursor, where you want to display something. Remember the top and left most location of the console is (0,0). So, if you want to draw from the top-left most, you call the function this way, "setCursorTo(0, 0);". Then do your 'cout << "etc etc ..."<<endl;' (see below). If you want to display at some other location, call "setCursorTo" for that location and do a cout again. You can do this "setCursorTo" and "cout<<...." combination as many times as you want. To give bucket a bucket shape, you need to use this "setCursorTo" function appropriately.

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 main(){
     setCursorTo(0, 0);
     cout <<"etc etc ..."<<endl;
}

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 main(){
     setCursorTo(0, 0);
     cout <<"etc etc ..."<<endl;
}

Explanation / Answer


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


using namespace std;
class TetrisShape
{
public:
int shapeTopLeftX;
int shapeTopLeftY;
/// active bool is prevent user input and stop object moving
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] = '#'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1] = '#'; 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]=0;
stoppointY[b]=3;
stoppointY[c]=3;
stoppointY[d]=0;
break;
case 2:
shapeArray[a][0] = ' '; shapeArray[b][0] = '#'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1] = '#'; 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]=0;
stoppointY[b]=4;
stoppointY[c]=0;
stoppointY[d]=0;
break;
case 3:
shapeArray[a][0] = ' '; shapeArray[b][0] = '#'; shapeArray[c][0] = '#'; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1] = '#'; 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]=0;
stoppointY[b]=2;
stoppointY[c]=2;
stoppointY[d]=0;
break;
case 4:
shapeArray[a][0] = ' '; shapeArray[b][0] = '#'; shapeArray[c][0] = '#'; shapeArray[d][0] = ' ';
shapeArray[a][1] = '#'; shapeArray[b][1] = '#; 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] = '#'; shapeArray[b][0] = '#'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1] = '#'; 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]=1;
stoppointY[b]=2;
stoppointY[c]=2;
stoppointY[d]=0;
break;
case 6:
shapeArray[a][0] = ' '; shapeArray[b][0] = '#'; shapeArray[c][0] = ' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1] = '#'; 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]=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] =
{
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}

};
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;
///change this to draw bucket nicely

}
}
/// this variable need to declare outside this void
/// else it will keep refresh and reset
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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote