You have already displayed the Tetris bucket in your previous project assignment
ID: 3829530 • Letter: Y
Question
You have already displayed the Tetris bucket in your previous project assignment. In this assignment, you will need to display and drop the shape. Add plenty of narrative comments. Your program must be compilable and executable.
If you need guidance, you will find some detailed instruction (below) to assist you. This is not the only approach that will work but it will present one possibility. The sample below uses Classes (a little Object oriented programming) but, if you prefer a different approach, you do not have to use classes in your project.
To represent the Tetris shapes, you can create a Class called "TetrisShape". In the class, declare a 2-D array of chars that is 4x4 in size (you could also use 4x2 but that would require different algorithm). Call the array "shapeArray". For convenience, declare it as public (but, it should not be your practice when you work in the software industry). You might need more dimensions for the array if you want to statically assign (hard code) all the shape rotations (3-d) for all the shapes (4-d). In this example, the rotation of the shapes is done by value swapping in the 4x4 array.
Now, populate or initialize the "shapeArray". There are three (3) options for doing this:
Option 1: Add a function in the class (call it "populateShapeArray(int shapeType)"), that will take an integer which will denote the type of shape.
Option 2: Use a non-default constructor. In this case, when you create an object, you will take an integer as the constructor argument, which will denote the type of shape, as shown below.
TetrisShape currentShape = new TetrisShape(shapeType);// shapeType is an int
Option 3: Use a setter "setShape(int shapeType)" function.
Regardless of which option you chose above, you would use a switch statement to assign the 4x4 array values for the specific shape. For example, for 'L', it can be something like the following:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
bucket[i+shapeTopLeftX][j+shapeTopLeftY] = localTetrisShape.shapeArray[i][j];
You are actually imposing the shape values on to the bucket.
Always keep record of the top left corner location (x,y dimensions) of the shape that is falling. This record will indicate where to draw the shape in the bucket after the shape moves. Initially, the location should be, (6, 0), which is the top middle of the bucket. Store the coordinates in the "TetrisShape" class as shapeTopLeftX and shapeTopLeftY. Note, x changes horizontally, and y changes vertically.
In this step you will generate one shape. To accomplish this, in the main() function, before the while loop (game loop), do the following:
Generate a random number from 0 to 6.
Use one of the options presented in step 2 above to create a TetrisShape object.
Create a global function called "updatebucket(TetrisShape localTetrisShape)" to populate the bucket with values of the "shapeArray". Here, you will use two nested for loops to go over the shapeArray of 4x4. You need to provide the TetrisShapeobject (created in step '4b' above) to the function as an argument in order to access its shapeArray. Inside the loop, you will do this.
Call the "updatebucket(TetrisShape localTetrisShape)" function from main() using the object created in step 4b.
Display the bucket.
Inside the while loop (game loop), as the shape falls, the value of y will increase. In order to accomplish this you will have "newShapeTopLeftY = shapeTopLeftY + 1;" so that the shape falls one cell below. Notice that you are storing the new y value in another variable because you need both the old and the new values.
Clear (assign spaces to) the current position values of the shape in the bucket using the "old" shapeTopLeftX and shapeTopLeftY. This will ensure that you do not leave any trail of the shape.
Call the "updatebucket(TetrisShape localTetrisShape)" function with the "new" shapeTopLeftX and shapeTopLeftY values. This will ensure the shape will be displayed in the new location (newShapeTopLeftX, newShapeTopLeftY).
Display the bucket. The shape is displayed in the new location of the bucket.
Store the new shapeTopLeftX and shapeTopLeftY values to the old shapeTopLeftX and shapeTopLeftY values to get ready for the next loop.
Sleep for a while to slow down the game.
Remember, these are guidelines and are only intended to help you, if you need assistance. If you already know what you need to do, then do it your way.
TetrisShape currentShape = new TetrisShape(shapeType);// shapeType is an int
Explanation / Answer
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std; #define GAME_INTERVAL 2
#define GO_DOWN 2
#define GO_LEFT 4
#define GO_RIGHT 6
#define GO_ROTATE 5 class TetrisShape
{
public: char shapeArray[4][4];
int shapeTopLeftX = 6;
int shapeTopLeftY = 0;
void populateShapeArray(int shape);
void rotate();
template <size_t rows, size_t cols>
void setShape(char(&shape)[rows][cols]);
TetrisShape(int shape) { populateShapeArray(shape); };
TetrisShape() {};
}; void TetrisShape::rotate()
{
char _shapeArray[4][4]; _shapeArray[0][0] = shapeArray[0][3]; _shapeArray[1][0] = shapeArray[0][2]; _shapeArray[2][0] =
shapeArray[0][1]; _shapeArray[3][0] = shapeArray[0][0];
_shapeArray[0][1] = shapeArray[1][3]; _shapeArray[1][1] = shapeArray[1][2]; _shapeArray[2][1] =
shapeArray[1][1]; _shapeArray[3][1] = shapeArray[1][0];
_shapeArray[0][2] = shapeArray[2][3]; _shapeArray[1][2] = shapeArray[2][2]; _shapeArray[2][2] =
shapeArray[2][1]; _shapeArray[3][2] = shapeArray[2][0];
_shapeArray[0][3] = shapeArray[3][3]; _shapeArray[1][3] = shapeArray[3][2]; _shapeArray[2][3] =
shapeArray[3][1]; _shapeArray[3][3] = shapeArray[3][0]; for (int _s = 0; _s < 4; _s++)
{
for (int _a = 0; _a < 4; _a++)
{
shapeArray[_s][_a] = _shapeArray[_s][_a];
} } } void TetrisShape::populateShapeArray(int shape)
{
switch (shape)
{
case 1:
shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = 'X'; shapeArray[3][3] = '
';
break;
case 2:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 3:
shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = '
';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = 'X'; shapeArray[3][3] = ' ';
break;
case 4: shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = '
';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 5:
shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; 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] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = 'X'; shapeArray[3][3] = '
';
break;
case 6:
shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = ' '; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = '
';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = 'X'; shapeArray[3][3] = '
';
break;
}
} int currentShape = -1; // this is the shape that is currently in play.
int x[4] = { -1, -1, -1, -1 };
int y[4] = { -1, -1, -1, -1 }; // the x, y location for each of the shapes.
bool isDropping = false; // gameTick globals.
int currentTick = 0; template <size_t rows, size_t cols>
void generateBucket(char(&bucket)[rows][cols]); // create the bucket
void generateShapeStream(); // shapes, in drop and in queue, constantly
void dropShape(); // drop the shape
bool moveShape(int direction); // GO_DOWN (falling)
template <size_t rows, size_t cols>
bool gameTick(char(&bucket)[rows][cols], char(&perm_bucket)[rows][cols]); // handles game events
template <size_t rows, size_t cols>
void landShape(char(&bucket)[rows][cols]); // shape hitting the bottom
template <size_t rows, size_t cols>
void drawBucket(char(&bucket)[rows][cols]); // display bucket
int getUserInput(); // gets the key pressed from the user. template <size_t rows, size_t cols>
bool canEnter(int direction, char(&bucket)[rows][cols]); // is space available for the block
void setCursorTo(int x, int y); // set cursor to appropriate position
int previousX = 6, previousY = 0;
int shapes[256]; TetrisShape activeShape; int main()
{
// set 2 buckets, one permanent and one for display
char bucket[12][25];
char _bucket[12][25];
int shapes[256] = {};
int shapeIndex = 0;
bool gameOver = false; generateBucket(bucket);
generateBucket(_bucket);
generateShapeStream();
drawBucket(bucket); while (!gameOver)
{
gameOver = gameTick(bucket, _bucket);
Sleep(500);
currentTick++;
}
setCursorTo(25, 6);
cout << "GAME OVER" << endl;
system("pause");
} void setCursorTo(int x, int y)
{
HANDLE handle;
COORD position;
handle = GetStdHandle(STD_OUTPUT_HANDLE);
position.X = x;
position.Y = y;
SetConsoleCursorPosition(handle, position);
} /* generateBucket */
template <size_t rows, size_t cols>
void generateBucket(char(&bucket)[rows][cols]) {
for (int w = 0; w < 12; w++)
{
for (int z = 0; z < 25; z++)
{
if (((w == 0) || (w == 11)) && (z == 0))
{
bucket[w][z] = '.';
}
else if (((w % 12 == 0) || (w % 12 == 11)) && ((z > 0) && (z < 24)))
{
bucket[w][z] = '|';
}
else if (((w == 0) || (w == 11)) && (z == 24))
{
bucket[w][z] = '+';
}
else if (z == 24)
{
bucket[w][z] = '-';
}
else
{
bucket[w][z] = ' ';
}
}
}
} /* generateShapeStream - generates the shapes preparing to fall */
void generateShapeStream()
{
// Initialize the random number generator
srand(time(NULL)); for (int p = 0; p < 256; p++)
{
shapes[p] = rand() % 6 + 1;
} } /* drawBucket -displays bucket and shapes to fall */
template <size_t rows, size_t cols>
void drawBucket(char(&bucket)[rows][cols])
{
setCursorTo(0, 0);
for (int m = 0; m < 25; m++)
{
for (int k = 0; k < 12; k++)
{
cout << bucket[k][m];
}
cout << endl;
}
} /* gameTick - everything happening in game, and game over procedures*/ template <size_t rows, size_t cols>
bool gameTick(char(&bucket)[rows][cols], char(&perm_bucket)[rows][cols])
{
drawBucket(bucket);
if (!isDropping)
{
currentShape++;
activeShape = TetrisShape(shapes[currentShape]); if (!canEnter(GO_DOWN, perm_bucket))
{
return true;
}
else
{
isDropping = true;
updateBucket(bucket, false);
}
}
else
{
if (currentTick % GAME_INTERVAL == 1)
{
// we are on a drop interval.
if (canEnter(GO_DOWN, perm_bucket))
{ updateBucket(bucket, moveShape(GO_DOWN)); }
else
{
landShape(perm_bucket); }
}
}
int direction = getUserInput();
if (canEnter(direction, perm_bucket))
{ updateBucket(bucket, moveShape(direction));
} if (!canEnter(GO_DOWN, perm_bucket))
{
landShape(perm_bucket); } return false; } /* moveShape - handles the shape dropping down. */
bool moveShape(int direction)
{ previousX = activeShape.shapeTopLeftX;
previousY = activeShape.shapeTopLeftY;
switch (direction)
{
case GO_DOWN:
activeShape.shapeTopLeftY++;
return false;
break;
case GO_RIGHT:
activeShape.shapeTopLeftX++;
return false;
break;
case GO_LEFT:
activeShape.shapeTopLeftX--;
return false;
break;
case GO_ROTATE:
activeShape.rotate();
return true;
break; }
} /* updateBucket - place new shape into bucket, and removing old shape*/
template <size_t rows, size_t cols>
void updateBucket(char(&bucket)[rows][cols], bool isRotation)
{
for (int _l = 0; _l < 4; _l++) {
for (int _g = 0; _g < 4; _g++)
{
if (!isRotation)
{
if ((activeShape.shapeArray[_l][_g] != ' ') && (bucket[_l + previousX][_g +
previousY] != '|') && (bucket[_l + previousX][_g + previousY] != '-'))
{
bucket[_l + previousX][_g + previousY] = ' ';
}
}
else {
if ((bucket[_l + previousX][_g + previousY] != '|') && (bucket[_l +
previousX][_g + previousY] != '-'))
{
bucket[_l + previousX][_g + previousY] = ' ';
}
}
}
}
for (int _l = 0; _l < 4; _l++)
{
for (int _g = 0; _g < 4; _g++)
{
if (activeShape.shapeArray[_l][_g] != ' ')
{
bucket[_l + activeShape.shapeTopLeftX][_g +
activeShape.shapeTopLeftY] = activeShape.shapeArray[_l][_g];
} }
} } /* landShape - Sets the shape in place once it hits the bottom of the bucket. Moves the shape to the
permanent bucket (_bucket)*/
template <size_t rows, size_t cols>
void landShape(char(&bucket)[rows][cols])
{
updateBucket(bucket, false);
previousX = 6; previousY = 0; isDropping = false; } /* getUserInput - Reads the user input from the player*/
int getUserInput() {
setCursorTo(35, 9);
if ((GetKeyState(VK_DOWN) != 0) && (GetKeyState(VK_DOWN) != 1)) { return GO_DOWN; }
if ((GetKeyState(VK_RIGHT) != 0) && (GetKeyState(VK_RIGHT) != 1)) { return GO_RIGHT; }
if ((GetKeyState(VK_LEFT) != 0) && (GetKeyState(VK_LEFT) != 1)) { return GO_LEFT; }
if ((GetKeyState(VK_UP) != 0) && (GetKeyState(VK_UP) != 1)) { return GO_ROTATE; } return 0;
} /* canRotate - can we rotate? if we are adjacent to another shape NO! */
template <size_t rows, size_t cols>
bool canRotate(char(&bucket)[rows][cols])
{
// creating a copy of the shape, rotating it so we can determine where in the bucket it will land.
TetrisShape _tmp = TetrisShape(activeShape);
_tmp.rotate();
for (int _t = 0; _t < 4; _t++)
{
for (int _z = 0; _z < 4; _z++)
{
if (_tmp.shapeArray[_t][_z] != ' ')
{
if (bucket[_tmp.shapeTopLeftX + _t][_tmp.shapeTopLeftY + _z] != ' ')
{
return false;
}
}
}
} return true;
} /* canEnter - Tests the direction in which a shape can enter*/
template <size_t rows, size_t cols>
bool canEnter(int dir, char(&bucket)[rows][cols])
{ // Check for collisions between shapes or with the bucket
// Determining in which direction the shapes are moving
int delta_x = 0, delta_y = 0;
switch (dir)
{
case GO_DOWN:
delta_y++;
break;
case GO_LEFT:
delta_x--;
break;
case GO_RIGHT:
delta_x++;
break;
case GO_ROTATE:
return canRotate(bucket);
break; } // Create the starting {x, y} position to test for collision
int test_b = activeShape.shapeTopLeftX + delta_x;
int test_k = activeShape.shapeTopLeftY + delta_y; for (int _b = 0; _b < 4; _b++)
{
for (int _k = 0; _k < 4; _k++)
{ if (activeShape.shapeArray[_b][_k] != ' ')
{
if (bucket[test_b + _b][test_k + _k] != ' ')
{
return false;
}
}
}
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.