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

You have already displayed the Tetris Bucket, started dropping the shapes, stopp

ID: 3838602 • Letter: Y

Question

You have already displayed the Tetris Bucket, started dropping the shapes, stopped them at the bottom of the Bucket, and dropped another shape from the top. During this Module, you move/rotate the shape in response to the player input. 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.

enum {UP_ARROW=72,DOWN_ARROW=80, LEFT_ARROW=75, RIGHT_ARROW=77};

Create a function getUserInput() to get the user inputs.

Create a moveShape(int input, int& newShapeTopLeftX, int& newShapeTopLeftY) function. This will have a switch case block that will include a default case which will increase the Y by 1, (i.e., by default the shape will keep falling). Other values of the switch case would be the arrow keys. Here is an enumeration of the arrow key values that you will use.

Handling "left", "right", and "down" arrow keys are easy. However, the "up" arrow key rotates the shape which is little more challenging. Here are two (2) approaches:

Multi-dimensional arrays: You can assign all the rotational shapes into multi-dimensional arrays (as outlined in the previous Module). There is no trick to it but it is little laborious and lengthy.

Swap the values of the shape array: Visualize the 2-D array map like a chess board. If you rotate the shape clock-wise, do you see which cells' values go to which cells? You will do just that. With each rotate you will do a clock-wise rotation just by swapping the values of the shape array cells. It will be little inaccurate, but is enough for this project. Feel free to add more conditions to make it correct. Here is how the swap would look.

This is my code this far:

#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;
int x[4] = { -1, -1, -1, -1 };
int y[4] = { -1, -1, -1, -1 };
bool isDropping = false;.
int currentTick = 0; template <size_t rows, size_t cols>
void generateBucket(char(&bucket)[rows][cols]);
void generateShapeStream();
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]);
template <size_t rows, size_t cols>
void landShape(char(&bucket)[rows][cols]);
template <size_t rows, size_t cols>
void drawBucket(char(&bucket)[rows][cols]);
int getUserInput();bool canEnter(int direction, char(&bucket)[rows][cols]);
void setCursorTo(int x, int y);
int previousX = 6, previousY = 0;
int shapes[256]; TetrisShape activeShape; int main()
{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);
} 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] = ' ';
}
}
}
}
void generateShapeStream()
{
srand(time(NULL)); for (int p = 0; p < 256; p++)
{
shapes[p] = rand() % 6 + 1;
}
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;
}
}

>
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)
{
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; }
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;

}
}
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];
} }
}
template <size_t rows, size_t cols>
void landShape(char(&bucket)[rows][cols])
{
updateBucket(bucket, false);
previousX = 6; previousY = 0; isDropping = false;

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;
} template <size_t rows, size_t cols>
bool canRotate(char(&bucket)[rows][cols])
{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;
}
template <size_t rows, size_t cols>
bool canEnter(int dir, char(&bucket)[rows][cols])
{

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; }
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;
}

enum {UP_ARROW=72,DOWN_ARROW=80, LEFT_ARROW=75, RIGHT_ARROW=77};

Explanation / Answer

#include "stdafx.h"
#include<iostream>
#include<windows.h>
#include<thread>
#include<chrono>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

using namespace std;

class TetrisShape
{
public:
   char shapeArray[4][4];
   int shapeType;

   int shapeTopLeftX;
   int shapeTopLeftY;

   TetrisShape(int shapetype);
   TetrisShape();

   void setShape(int shapetype);
   bool moveShape(int input, int& newShapeTopLeftX, int& newShapeTopLeftY);
};

char bucket[25][13];
enum{ UP_ARROW = 72, DOWN_ARROW = 80, LEFT_ARROW = 75, RIGHT_ARROW = 77 };

void initializeBucket()
{
   int i, j;
   for (i = 0; i < 25; i++)
   {
       for (j = 0; j < 13; j++)
           bucket[i][j] = ' ';
       bucket[i][0] = '#';
       bucket[i][11] = '#';
   }

   for (i = 0; i < 12; i++)
       bucket[24][i] = '#';
}

void setCursorTo(int x, int y)//coordinate setup
{
   HANDLE handle;
   COORD position;
   handle = GetStdHandle(STD_OUTPUT_HANDLE);
   position.X = x;
   position.Y = y;
   SetConsoleCursorPosition(handle, position);
}
void displayBucket()//display bucket
{

   //for loop to display bucket
   for (int y = 0; y <25; y++)
   {
       setCursorTo(0, y);
       for (int j = 0; j < 12; j++)
           cout << bucket[y][j];
       cout << endl;
   }
}

void updateBucket(TetrisShape localTetrisShape)//update bucket
{
   int topX = localTetrisShape.shapeTopLeftX;
   int topY = localTetrisShape.shapeTopLeftY;

   for (int i = 0; i <4; i++)
   {
       for (int j = 0; j <4; j++)
       {
           if (localTetrisShape.shapeArray[i][j] != ' ')
               bucket[i + topY][j + topX] = localTetrisShape.shapeArray[i][j];
       }
   }
}

void clearOldShape(TetrisShape localTetrisShape)
{
   int topX = localTetrisShape.shapeTopLeftX;
   int topY = localTetrisShape.shapeTopLeftY;

   for (int i = 0; i <4; i++)
   {
       for (int j = 0; j <4; j++)
       {
           if (localTetrisShape.shapeArray[i][j] != ' ')
               bucket[i + topY][j + topX] = ' ';
       }
   }
}

bool stuckCheck(TetrisShape shape, int newTopX, int newTopY)//for collision detection
{
   for (int i = 0; i <4; i++)
   {
       for (int j = 0; j <4; j++)
       {
           if (shape.shapeArray[i][j] != ' ')
           {
               int y = newTopY + i;
               int x = newTopX + j;

               if (y < 0 || y >= 25)
                   return true;

               if (x < 0 || x >= 12)
                   return true;

               // inside bucket
               if (bucket[y][x] != ' ')
                   return true;
           }
       }
   }
   return false;
}

bool processDownArrow(TetrisShape& shape)
{
   if (stuckCheck(shape, shape.shapeTopLeftX, shape.shapeTopLeftY + 1))
       return true;
   shape.shapeTopLeftY += 1;
   return false;
}

void processLeftArrow(TetrisShape& shape)
{

   if (stuckCheck(shape, shape.shapeTopLeftX - 1, shape.shapeTopLeftY))
       return;
   shape.shapeTopLeftX -= 1;
}

void processRightArrow(TetrisShape& shape)
{
   if (stuckCheck(shape, shape.shapeTopLeftX + 1, shape.shapeTopLeftY))
       return;
   shape.shapeTopLeftX += 1;
}

int getUserInput()
{
   int key = 0;
   if (_kbhit())
   {
       key = _getch();
   }
   return key;
}

int checkFullRow()
{
   int i, j, k;
   int count = 0;
   for (i = 23; i >= 0; i--)
   {
       bool full = true;
       for (j = 1; j <= 10 && full; j++)
       {
           if (bucket[i][j] == ' ')
               full = false;
       }

       // if this row is full, remove this row
       if (full)
       {
           count++;
           for (k = i; k > 0; k--)
           {
               for (j = 1; j <= 10; j++)
                   bucket[k][j] = bucket[k - 1][j];
           }

           // fill blank at top
           for (j = 1; j <= 10; j++)
               bucket[0][j] = ' ';

           // check this row again
           i++;
       }
   }
   return count;
}

void inline timeControl(int timer)//call this timer at last in while loop of main function.
{
   Sleep(timer);
}

int main()
{
   srand(time(0)); // initialize the random seed

   int gameOver = 0;
   int shapeType = (rand() % 7) + 0;//Randomly generates a number and sends it to the constructor to choose shape with the 0 to 6 condition
   TetrisShape currentShape(shapeType);
   TetrisShape nextShape(rand() % 7);

   int newShapeTopLeftY;
   int newShapeTopLeftX;
   int input;
   bool reached = false;   // reach bottom
   int timeCount = 0;
   bool display = false;
   int score = 0;

   initializeBucket();

   updateBucket(currentShape);//updates and displays bucket
   displayBucket();

   while (gameOver == 0)//while to start game
   {
       clearOldShape(currentShape);

       input = getUserInput();
       display = false;
       reached = false;

       if (input == LEFT_ARROW || input == RIGHT_ARROW ||
           input == UP_ARROW || input == DOWN_ARROW)
       {
           reached = currentShape.moveShape(input, newShapeTopLeftX, newShapeTopLeftY);
           currentShape.shapeTopLeftY = newShapeTopLeftY;
           currentShape.shapeTopLeftX = newShapeTopLeftX;
           display = true;
       }

       timeCount++;
       if (timeCount == 100)
       {
           // every 2 seconds drop
           reached = currentShape.moveShape(0, newShapeTopLeftX, newShapeTopLeftY);
           currentShape.shapeTopLeftY = newShapeTopLeftY;
           currentShape.shapeTopLeftX = newShapeTopLeftX;
           display = true;
           timeCount = 0;
       }

       updateBucket(currentShape);

       if (reached)
       {
           int cnt = checkFullRow();
           if (cnt == 1)
               score += 10;
           else if (cnt == 2)
               score += 25;
           else if (cnt == 3)
               score += 40;
           else if (cnt == 4)
               score += 60;

           // create another
           currentShape.setShape(nextShape.shapeType);
           nextShape.setShape(rand() % 7);

           if (stuckCheck(currentShape, currentShape.shapeTopLeftX,
               currentShape.shapeTopLeftY))
           {
               gameOver = 1;
           }
           else
           {
               updateBucket(currentShape);//updates and displays bucket
               display = true;
           }
       }

       if (display)
       {
           displayBucket();
           setCursorTo(25, 0);
           cout << "Current Score: " << score << "     ";
       }

       //Sleeps for the game
       timeControl(20); // 20 mill seconds

   }

   system("PAUSE");
   return 0;
}


// Default constructor
TetrisShape::TetrisShape()
{
   shapeTopLeftX = 6;
   shapeTopLeftY = 0;
   shapeType = -1;
}
//Constructor for shape arrays
TetrisShape::TetrisShape(int shapetype)
{
   setShape(shapetype);
}

void TetrisShape::setShape(int shapetype)
{
   shapeType = shapetype;
   shapeTopLeftX = 6;
   shapeTopLeftY = 0;

   switch (shapetype)
   {
   case 0:
       //L Shape
       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] = ' ';
       break;
   case 1:
       //I Shape
       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 2:
       //J Shape
       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] = 'X'; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
       shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
       break;
   case 3:
       //O Shape
       shapeArray[0][0] = ' '; shapeArray[1][0] = ' '; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
       shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; 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] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
       break;
   case 4:
       //T Shape
       shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
       shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; 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 5:
       //S Shape
       shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
       shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; 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 6:
       //Z Shape
       shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
       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;
   }
}

// Return if reach bottom
bool TetrisShape::moveShape(int input, int& newShapeTopLeftX, int& newShapeTopLeftY)
{
   int i;
   int oldX = shapeTopLeftX;
   int oldY = shapeTopLeftY;
   bool reached = false;
   char tempCellVal;

   switch (input)
   {
   case UP_ARROW:
       // after rotation, sometimes we will have stuck
       // stop such rotate
       do
       {
           tempCellVal = shapeArray[0][0];
           shapeArray[0][0] = shapeArray[0][3];
           shapeArray[0][3] = shapeArray[3][3];
           shapeArray[3][3] = shapeArray[3][0];
           shapeArray[3][0] = tempCellVal;
           tempCellVal = shapeArray[0][1];
           shapeArray[0][1] = shapeArray[1][3];
           shapeArray[1][3] = shapeArray[3][2];
           shapeArray[3][2] = shapeArray[2][0];
           shapeArray[2][0] = tempCellVal;
           tempCellVal = shapeArray[0][2];
           shapeArray[0][2] = shapeArray[2][3];
           shapeArray[2][3] = shapeArray[3][1];
           shapeArray[3][1] = shapeArray[1][0];
           shapeArray[1][0] = tempCellVal;
           tempCellVal = shapeArray[1][1];
           shapeArray[1][1] = shapeArray[1][2];
           shapeArray[1][2] = shapeArray[2][2];
           shapeArray[2][2] = shapeArray[2][1];
           shapeArray[2][1] = tempCellVal;
       } while (stuckCheck(*this, shapeTopLeftX, shapeTopLeftY));

       break;
   case DOWN_ARROW:
       for (i = 0; i < 3 && !reached; i++)
           reached = processDownArrow(*this);
       break;
   case LEFT_ARROW:
       processLeftArrow(*this);
       break;
   case RIGHT_ARROW:
       processRightArrow(*this);
       break;
   default:
       reached = processDownArrow(*this);
       break;
   }

   // get the new position
   newShapeTopLeftX = shapeTopLeftX;
   newShapeTopLeftY = shapeTopLeftY;

   // recover current position to old
   shapeTopLeftX = oldX;
   shapeTopLeftY = oldY;

   return reached;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote