I am creating a tetrisGame and this is the assignment PLEASE
I am creating a tetrisGame and this is the assignment:
PLEASE NO EXTERNAL FUNCTIONS
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,
In the above photo shows the end of a single block falling from the top. The block is supposed to stop at the bottom and start a new, this is normal tetris.
CODE
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
using namespace std;
class TetrisShape
{
public:
int shapeTopLeftX;//Top left coord of shape
int shapeTopLeftY;//top left coord of shape
bool active = true;//bool for checking if shape can move
TetrisShape()
{
shapeTopLeftX = 6;
shapeTopLeftY = 0;
}
char shapeArray[4][4];//2 dimensional array for shape
int stoppointY[4];//stop point array
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;//width of play field
const int height = 25;//height of play field
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()//drawing bucket
{
for (int i = 0; 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()//drawing bucket
{
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)//update bucket for adding shapes
{
movedown++;
int shapeTopLeftX = 6;
for (int i = 0; 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 main()
{
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;
}
x xxxx xxxx xxxx xxxx xxxx xxxSolution
#include <iostream>
#include <windows.h>
using namespace std;
class TetrisShape
{
public:
int shapeTopLeftX;//Top left coord of shape
int shapeTopLeftY;//top left coord of shape
bool active = true;//bool for checking if shape can move
TetrisShape()
{
shapeTopLeftX = 6;
shapeTopLeftY = 0;
}
char shapeArray[4][4];//2 dimensional array for shape
int stoppointY[4];//stop point array
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;//width of play field
const int height = 25;//height of play field
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()//drawing bucket
{
for (int i = 0; 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()//drawing bucket
{
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)//update bucket for adding shapes
{
movedown++;
int shapeTopLeftX = 6;
for (int i = 0; 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 main()
{
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;
}












