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

So, I have my code from the last assignment, and I know that it\'s not perfect b

ID: 3765068 • Letter: S

Question

So, I have my code from the last assignment, and I know that it's not perfect by any means, but I'm having some difficulty implementing this portion of the assignment:

User input is provided using the arrow keys. By default (with no user input being entered), the shape falls one cell in each loop. The shape can also be made to fall more quickly by pressing the down-arrow key. Assuming that you are using a switch block to handle the user input, in each switch case, you will change the top left x, y and calling different functions to process that arrow input. Regardless of which of the two methods triggered the shape's downward movement, you will probably call the function processDownArrow(TetrisShape& tetrisShape). You will probably find that you need separate functions to handle different key inputs.

In each of these arrow-key input process functions, you need to check whether the shape is stuck and needs to stop. You need to go over the values of the shape array using two nested "for loops". Remember, the shape is not stuck if the cell is empty. Below is the logic to check when the shape is stuck:

if (shapeArray[x][y] != ' '){
     if (x,y dimensions are inside the bucket){
          if bucket cell is not empty{
               The shape is stuck
          }
     }
     else if (x dimension reaches the bottom){
          The shape is stuck, so stop the shape.
     }
     else{
          /*Here the shape hits the side wall, but not stuck. The situation is, the shape is at the left wall, and user is pressing the left arrow. In this case, the input is ineffective. So, we will let the shape drop one cell here, and call the processDownArrow(*this). The processDownArrow function will return a bool value whether the shape is stuck or not.*/

          if (the shape is stuck){
               Stop the shape
          }
          else{
               the shape is not stuck, let it fall
          }
     }
}

When the current shape stops at the bottom, the next shape starts to drop. In the main() function, before the while loop (game loop), you will create two Tetris objects at the same time: currentTetrisShape, and nextTetrisShape. Inside the game loop, when the current shape is stuck, you check whether a line is complete. Then, you call currentTetrisShape.setShape(nextTetrisShapeType). In the setShape() function, you assign the properties of the shape: shapeType, top left X and Y, shapeArray, etc. Then you callnextTetrisShape.setShape (use a new randomly generated int).

And here is my code before this part:

#include <iostream>
#include <windows.h>
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
using namespace std;


class TetrisShape
{

public:
   char shapeArray[4][4];
   TetrisShape(int shapetype);
   TetrisShape();

};

//Constructor that stores shape into shapeArray depending on the number given
TetrisShape::TetrisShape(int shapetype){
   switch (shapetype)
   {
   case 1:
       //L
       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] = ' ';
   case 2:
       //I
       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] = ' ';
   case 3:
       //J
       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] = ' ';
   case 4:
       //O
       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] = ' ';
   case 5:
       //T
       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] = ' ';
   case 6:
       //S
       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] = ' ';
   case 7:
       //Z
       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] = ' ';
   }

}

int shapeTopLeftX = 0;
int shapeTopLeftY = 6;

char bucket[25][13] = { "# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"############" };


//Function displays bucket
void displayBucket(){

   //Loop that prints bucket
  

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

}

//Updates Bucket by going through loops of shapeArray
void updateBucket(TetrisShape localTetrisShape)
{
   for (int i = 0; i < 4; i++)
   {
       for (int j = 0; j < 4; j++)
       {
           bucket[i + shapeTopLeftX][j + shapeTopLeftY] = localTetrisShape.shapeArray[i][j];
       }
   }
}
//Given SetCursorTo Function for visibility
void setCursorTo(int x, int y)
{
   HANDLE handle;
   COORD position;
   handle = GetStdHandle(STD_OUTPUT_HANDLE);
   position.X = x;
   position.Y = y;
   SetConsoleCursorPosition(handle, position);
}

int main()
{
  
   //Implementation of Cursor
   setCursorTo(0, 0);
   int game = 1;
   //Randomly generates a number and sends it to the constructor to choose shape
   int shapeType = (rand() % 7) + 1;
   TetrisShape currentShape = TetrisShape(shapeType);
   TetrisShape nextShape = TetrisShape(shapeType);

   //updates and displays bucket
   updateBucket(currentShape);
   displayBucket();
  
   //Starts Up Game
   while (game != 0)
   {
       //Creates New Leftx & y coordinates
       int newShapeTopLeftX = 0;
       int newShapeTopLeftY = shapeTopLeftY + 1;

      
       //Updates and Displays Bucket
       updateBucket(currentShape);

       //Stores old shape coordinates into new;
       newShapeTopLeftX = shapeTopLeftX;
       newShapeTopLeftY = shapeTopLeftY;

       //Sleeps for the game
       std::this_thread::sleep_for(std::chrono::seconds(3));


      
      

   }
   system("CLS");
   return 0;
}

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>

using namespace std;

//console width height
const int WIDTH = 400;
const int HEIGHT = 400;

//bucket row and column size
const int buckHeight = 25;
const int buckWidth = 12;

//bucket array
char bucket[buckHeight][buckWidth];

//bool variables
bool running = true;
bool shapeStuck = false;

//variables
int randNum;
int shapeTopLeftX = 0;
int shapeTopLeftY = 6;
int newShapeTopLeftX;
int newShapeTopLeftY;
int oldTopLeftX;
int oldTopLeftY;
int key;
char tempbucket;

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

//shape class
class TetrisShape {
  
public:
  
   void createShape(int shapeType);
  
   char shapeArray[4][4];
  
  
private:
   enum Shapes{Box, Line, S, Z, L, J, T};
  
  
};

//function declarations
void setConsole();
void setCursorTo(int x, int y);
void initializeBucket();
void printBucket();
void getRandNum(int seed);
void drawShape();
void updateBucket(TetrisShape shape, int x, int y);
void removeTrail(int x, int y);
void getUserInput(TetrisShape temp);
void moveShape(TetrisShape shape, int arrowKeys);
void stuckCheck(TetrisShape shape);
void processDown();
void inline timeControl(int timer);

int wmain(int argc, _TCHAR* argv[])
{
   srand((unsigned) time(NULL));
  
   initializeBucket();
  
    //creates a shape
   TetrisShape shape;
   TetrisShape temp;
   getRandNum(6);
   shape.createShape(randNum);
  
  
   while(running) {
       temp = shape;
       printBucket();
       processDown();
       getUserInput(temp);
       updateBucket(temp, shapeTopLeftX, shapeTopLeftY);
       printBucket();
       removeTrail(oldTopLeftX, oldTopLeftY);
      
       timeControl(500);
   }
  
   system("Pause");
  
}

//functions
void setConsole() {
  
   HWND console = GetConsoleWindow();
   RECT ConsoleRect;
   GetWindowRect(console, &ConsoleRect);
  
   MoveWindow(console, ConsoleRect.left, ConsoleRect.top, WIDTH, HEIGHT, true);
}
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 initializeBucket() {
  
   for(int i = 0; i < buckHeight; i++) {
       for(int j = 0; j < buckWidth; j++) {
          
          
           if(j == 0 || j == 11 || i == 24) {
               bucket[i][j] = '#';
           }
           else if(bucket[i][j] == 'X'){
               continue;
           }
           else {
               bucket[i][j] = ' ';
           }
          
       }
   }
}
void printBucket() {
   setConsole();
   setCursorTo(0, 1);
  
   initializeBucket();
  
   for(int i = 0; i < buckHeight; i++) {
       for(int j = 0; j < buckWidth; j++) {
           cout << bucket[i][j];
          
           if(j == 11) cout << endl;
       }
   }
}
void getRandNum(int seed) {
  
   randNum = (rand() % seed + 1);
}
void TetrisShape::createShape(int shapeType) {
  
   switch(shapeType) {
          
        case 1: //Box
          
            shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = 'X';   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;
        case 2: //Line
          
            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: //S
          
            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 4: // Z
          
            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;
        case 5: // L
          
            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 6: //J
          
            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 7: //T
          
            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;
        default:
            cout << "None" << endl;
            break;
   }
  
}
void updateBucket(TetrisShape shape, int x, int y) {
  
  
   for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4 ; j++) {
          
           bucket[i + x][j + y] = shape.shapeArray[i][j];
          
       }
   }
   oldTopLeftX = shapeTopLeftX;
   oldTopLeftY = shapeTopLeftY;
  
}
void removeTrail(int x, int y) {
   for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          
           bucket[i + x][j + y] = ' ';
       }
   }
}
void getUserInput(TetrisShape temp) {
  
   if(_kbhit()) {
       key = _getch();
   }
   moveShape(temp, key);
}
void moveShape(TetrisShape shape, int arrowKeys) {
   switch (arrowKeys) {
          
       case UP_ARROW:
           tempbucket = shape.shapeArray[0][0];
           shape.shapeArray[0][0] = shape.shapeArray[0][3];
           shape.shapeArray[0][3] = shape.shapeArray[3][3];
           shape.shapeArray[3][3] = shape.shapeArray[3][0];
           shape.shapeArray[3][0] = tempbucket;
          
           tempbucket = shape.shapeArray[0][1];
           shape.shapeArray[0][0] = shape.shapeArray[0][3];
           shape.shapeArray[0][3] = shape.shapeArray[3][3];
           shape.shapeArray[3][3] = shape.shapeArray[3][0];
           shape.shapeArray[3][0] = tempbucket;
          
           tempbucket = shape.shapeArray[0][2];
           shape.shapeArray[0][2] = shape.shapeArray[2][3];
           shape.shapeArray[2][3] = shape.shapeArray[3][1];
           shape.shapeArray[3][1] = shape.shapeArray[1][0];
           shape.shapeArray[1][0] = tempbucket;
          
           tempbucket = shape.shapeArray[1][1];
           shape.shapeArray[1][1] = shape.shapeArray[1][2];
           shape.shapeArray[1][2] = shape.shapeArray[2][2];
           shape.shapeArray[2][2] = shape.shapeArray[2][1];
           shape.shapeArray[2][1] = tempbucket;
          
            for(int i = 0; i < 4; i++) {
                for(int j = 0; j < 4 ; j++) {
                  
                    bucket[i + shapeTopLeftX][j + shapeTopLeftY] = shape.shapeArray[i][j];
                  
                }
            }
           break;
          
       case DOWN_ARROW:
           shapeTopLeftX += 3;
          
           processDown();
          
           break;
          
       case LEFT_ARROW:
           stuckCheck(shape);
          
           if(!shapeStuck){
                shapeTopLeftY--;
           }else processDown();
           break;
          
       case RIGHT_ARROW:
           shapeTopLeftY++;
           break;
          
       default:
           shapeTopLeftX++;
           break;
    }
}
void stuckCheck(TetrisShape shape){
  
   for(int i = 0; i < buckHeight; i++) {
       for(int j = 0; j < buckWidth; j++) {
          
           if(shape.shapeArray[i][j] != ' ') {
               if(j > 0 && j < 24){
                   shapeStuck = true;
               }
           }
       }
   }
}
void processDown() {
   shapeTopLeftX++;
   printBucket();
  
}
void inline timeControl(int timer)

{
   Sleep(timer);
}

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