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

Hi, I am working on tetris project and I am having problem with my code program

ID: 3769652 • Letter: H

Question

Hi,

I am working on tetris project and I am having problem with my code program and I was wondering if someone could help me compile it with no error.

I can someone help me create a good functioning tetris game that can show the score and that can make the user rotate the blocks and build up the blocks properly all the way top of the gride line??

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


using namespace std;

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


void initializeBucket();
void displayBucket();
void setCursorTo(int x, int y);
void userInput();
void moveShape();
void shapeDrop();
void clearRow();
void score();
void beginGame();
void endGame();

int main()
{
   int score = 0;
   char playAgain;
   //this is my working game loop, keeps replaying until n is entered
   cout << " Welcome to Tetris!!! Would you like to play? (y/n): ";
   cin >> playAgain;
   while ((playAgain != 'y') && (playAgain != 'n'))
   {
       cout << " Please enter y or n: ";
       cin >> playAgain;
   }
   if (playAgain == 'y')
   {
       //rand for shape
       srand(static_cast<unsigned int>(time(0)));
       int shapeType = rand() % 7;

       while (playAgain == 'y')
       {

           displayBucket();
           setCursorTo(0, 0);
           beginGame();
           cout << " Would you like to play again? (y/n): ";
           cin >> playAgain;
           while ((playAgain != 'y') && (playAgain != 'n'))
           {
               cout << " Please enter y or n: ";
               cin >> playAgain;
           }
       }
   }
   else


       system("pause");
   return 0;
}
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 beginGame()
{
   //this function calls the gameplay functions
   //each function here is what keeps the shapes moving and user playing

   userInput();
   moveShape();
   shapeDrop();
   clearRow();
   endGame();
}
void displayBucket()
{
   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 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] = ' ';
           }

       }
   }

}
class TetrisShape {
public:
   int shapeTopLeftX;
   int shapeTopLeftY;
   TetrisShape() {
       shapeTopLeftX = 6;
       shapeTopLeftY = 0;
   }
   char shapeArray[4][4];
   void populateShapeArray(int shapeType) {
       switch (shapeType) {
       case 1:
           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 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] = '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 4:
           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 5:
           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 6:
           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;
       }
   }
};

void userInput(char shapeType)
{
   char shapeArray[4][4];
   char mvDir; // Move Direction
               //user presses keys to flip/move shape
   cout << " User moves shape" << endl;
   cout << "Please enter move direction: L = left, R = right, U = Up, D = Down ";
   cin >> mvDir;
   switch (mvDir) {
   case '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 'R':
       shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';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] = 'X';shapeArray[2][2] = 'X';shapeArray[3][2] = ' ';
       shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
       break;

   case 'U':
       shapeArray[0][0] = ' ';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] = 'X';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
       shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
       break;

   case 'D':
       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] = ' ';shapeArray[2][3] = 'X';shapeArray[3][3] = ' ';
       break;
   } // end of switch

}
void moveShape(int shapeType)
{
   char shapeArray[4][4];
   //shape reacts to user input
   cout << " Computer moves shape according to user request" << endl;
   int mvDir; // Move Direction
               //user presses keys to flip/move shape
   cout << "Please enter move direction: 1 = Up & left, 2 = Up & right, 3 = Down & Left, 4 = Down & Right";
   cin >> mvDir;
   switch (mvDir) {
   case '1':
       shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = ' ';shapeArray[3][0] = 'X';
       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 '2':
       shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = 'X ';shapeArray[3][0] = ' ';
       shapeArray[0][1] = ' ';shapeArray[1][1] = 'X';shapeArray[2][1] = ' ';shapeArray[3][1] = 'X';
       shapeArray[0][2] = ' ';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':
       shapeArray[0][0] = ' ';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] = 'X';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
       break;

   case '4':
       shapeArray[0][0] = ' ';shapeArray[1][0] = 'X';shapeArray[2][0] = ' ';shapeArray[3][0] = 'X';
       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] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
       break;
   } // end of switch


}
void shapeDrop()
{
   char shapeArray[4][4];
   //when shape drops on bottom and stops
   cout << " Shape drops to bottom" << endl;
   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] = ' ';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
   shapeArray[0][3] = 'X';shapeArray[1][3] = 'X';shapeArray[2][3] = 'X';shapeArray[3][3] = 'X';

}
void clearRow()
{
   char shapeArray[4][4];
   //when a row is filled, will clear and score
   cout << " A row is filled, it clears" << endl;
   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] = ' ';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
   shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';

   score();
}
void score()
{
   //calculates score
   int score;
   cout << " Add to score" << endl;
   score++;
}

void endGame()
{
   //when shape hits top, clear screen and display/save score
   cout << " Clear screen and save score" << endl;
   clearRow();
   cout << " Score = " << score;
}

Explanation / Answer

Here I have given you the new code which will compile and run efficiently.

#include "DarkGDK.h"


//Global
int Block_Size=20;
const DWORD Red = dbRGB(255,0,0);
const DWORD Green = dbRGB(0,255,0);
const DWORD Blue = dbRGB(0,0,255);
const DWORD Magenta = dbRGB(255,0,255);
const DWORD Black = dbRGB(0,0,0);
const DWORD White = dbRGB(255,255,255);
const DWORD Yellow = dbRGB(255,255,0);
const DWORD Cyan = dbRGB(0,255,255);
const DWORD Orange = dbRGB(255,165,0);
//end global

//Classes

class Block {
private:
   int x,y;
   DWORD color;
public:
   Block();
   Block(int, int, DWORD);
   void draw();
   void move(int, int);
void clear();

};
Block::Block(){
x = 0;
y =0;
color = White;
}
//Methods (Block)

Block::Block(int X, int Y, DWORD COLOR)
{
   x=X;
   y=Y;
   color=COLOR;
}

void Block::draw()
{
   int x1, y1, x2, y2;
   x1=x*Block_Size;
   y1=y*Block_Size;
   x2=x1+Block_Size;
   y2=y1+Block_Size;

//   dbInk(color, Black);
   dbBox(x1,y1,x2,y2);
}

void Block::clear()
{

   dbInk(Black, Black);
   //dbBox(x1,y1,x2,y2);
   draw();
}

void Block::move(int dx, int dy)
{
   x=x+dx;
   y=y+dy;
   dbInk(color, Black);
   draw();
}
//End Method (Block)

class Shape {

private:
   Block blocks[4];
   //int pos[8];
public:
   int pos[8];
   DWORD color;
Shape();
   void make_shape();
   void move_shape(int,int);
   void draw_shape();
};
//Methods (Shape)
Shape::Shape() {
blocks[0] = Block(0,0, Red);
blocks[1] = Block(0,0, Red);
blocks[2] = Block(0,0, Red);
blocks[3] = Block(0,0, Red);
}
void Shape::make_shape()
{
   blocks[0] = Block(pos[0],pos[1],color);
   blocks[1] = Block(pos[2],pos[3],color);
   blocks[2] = Block(pos[4],pos[5],color);
   blocks[3] = Block(pos[6],pos[7],color);
  
}

void Shape::draw_shape()
{
   for (int i=0; i<4; i++)
   {
       blocks[i].draw();
   }
}

void Shape::move_shape(int dx, int dy)
{
   for (int i=0; i<4; i++)
   {
       blocks[i].clear();
   }
   for (int i=0; i<4; i++)
   {
       blocks[i].move(dx,dy);
   }
}


class I_Shape: public Shape{
public:
   I_Shape(int,int);
  
private:
   DWORD color;
   //int pos[8];
};

//Methods (I_Shape)


I_Shape::I_Shape(int x, int y):Shape()
{
//make_shape(pos[8],color);
   color=Blue;
   pos[0]=x-1;
   pos[1]=y;
   pos[2]=x;
   pos[3]=y;
   pos[4]=x+1;
   pos[5]=y;
   pos[6]=x+2;
   pos[7]=y;
   make_shape();
}

// the main entry point for the application is this function
void DarkGDK ( void )
{
   //Block NewBlock(0,0,Red);
   I_Shape First(5,3);
   // turn on sync rate and set maximum rate to 1 fps
   dbSyncOn ( );
   dbSyncRate ( 1 );
   First.draw_shape();
   First.move_shape(5,0);

   //NewBlock.draw();
   //   NewBlock.move(2,2);
   // our main loop
   while ( LoopGDK ( ) )
   {
      
      
           First.move_shape(0,1);
      


      
       // update the screen
       dbSync ( );
   }

   // return back to windows
   return;
}

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