The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 4 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Using Visual studio and the file already attached to the questionÂ
You have already displayed the Tetris Bucket, started dropping the shapes, stopped them at the bottom of the Bucket, dropped another shape from the top, got the user input and moved/rotated the shape in response to the user input.
In this assignment, you need to:
Â
Â
#include
#include
#include
#include
#include
#include "stdafx.h"
<iostream>
<Windows.h>
<cstdlib>
<time.h>
<conio.h> using namespace std;
class TetrisShape
{
public:
int shapeTopLeftX;
int shapeTopLeftY;
/// active bool is prevent user input and stop object moving
bool active = true;
TetrisShape()
{
shapeTopLeftX = 4;
shapeTopLeftY = 0;
} [0] =
[1] =
[2] =
[3] = [0] =
[1] =
[2] =
[3] = char shapeArray[4][4];
int stoppointY[4];
///stoppointY is a collision of shape array
///each differ shape have differ collision
void populateShapeArray(int shapeType)
{
int a = 0, b = 1, c = 2, d = 3;
switch (shapeType)
{
case 1:
shapeArray[a][0] = ' '; shapeArray[b][0]
' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1]
' '; shapeArray[d][1] = ' ';
shapeArray[a][2] = ' '; shapeArray[b][2]
'X'; shapeArray[d][2] = ' ';
shapeArray[a][3] = ' '; shapeArray[b][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]
' '; shapeArray[d][0] = ' ';
shapeArray[a][1] = ' '; shapeArray[b][1]
' '; shapeArray[d][1] = ' ';
shapeArray[a][2] = ' '; shapeArray[b][2]
' '; shapeArray[d][2] = ' ';
shapeArray[a][3] = ' '; shapeArray[b][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]
= 'X'; shapeArray[c]
= 'X'; shapeArray[c]
= ' '; shapeArray[c] = 'X'; shapeArray[c]
= 'X'; shapeArray[c]
= 'X'; shapeArray[c]
= 'X'; shapeArray[c] = 'X'; shapeArray[c] [0] = 'X'; shapeArray[d][0] = ' ';
shapeArray[a][1] =
[1] = 'X'; shapeArray[d][1] = ' ';
shapeArray[a][2] =
[2] = ' '; shapeArray[d][2] = ' ';
shapeArray[a][3] =
[3] = ' '; shapeArray[d][3] = ' ';
stoppointY[a] = 0;
stoppointY[b] = 2;
stoppointY[c] = 2;
stoppointY[d] = 0;
break;
case 4:
shapeArray[a][0] =
[0] = 'X'; shapeArray[d][0] = ' ';
shapeArray[a][1] =
[1] = ' '; shapeArray[d][1] = ' ';
shapeArray[a][2] =
[2] = ' '; shapeArray[d][2] = ' ';
shapeArray[a][3] =
[3] = ' '; shapeArray[d][3] = ' ';
stoppointY[a] = 2;
stoppointY[b] = 2;
stoppointY[c] = 1;
stoppointY[d] = 0;
break;
case 5:
shapeArray[a][0] =
[0] = ' '; shapeArray[d][0] = ' ';
shapeArray[a][1] =
[1] = 'X'; shapeArray[d][1] = ' ';
shapeArray[a][2] =
[2] = ' '; shapeArray[d][2] = ' ';
shapeArray[a][3] =
[3] = ' '; shapeArray[d][3] = ' '; [0] = ' ';
[1] = ' ';
[2] = ' ';
[3] = ' '; }; stoppointY[a] = 1;
stoppointY[b] = 2;
stoppointY[c] = 2;
stoppointY[d] = 0;
break;
case 6:
shapeArray[a][0] =
shapeArray[d][0] = ' ';
shapeArray[a][1] =
shapeArray[d][1] = ' ';
shapeArray[a][2] =
shapeArray[d][2] = ' ';
shapeArray[a][3] =
shapeArray[d][3] = ' ';
stoppointY[a] = 3;
stoppointY[b] = 3;
stoppointY[c] = 0;
stoppointY[d] = 0;
break;
} } const int width = 12;
const int height = 25;
int bucketobjectheight[width];
char bucket[height][width] =
{ ' '; shapeArray[b][1] = 'X'; shapeArray[c]
' '; shapeArray[b][2] = ' '; shapeArray[c]
' '; shapeArray[b][3] = ' '; shapeArray[c] ' '; shapeArray[b][0] = 'X'; shapeArray[c]
'X'; shapeArray[b][1] = 'X'; shapeArray[c]
' '; shapeArray[b][2] = ' '; shapeArray[c]
' '; shapeArray[b][3] = ' '; shapeArray[c] 'X'; shapeArray[b][0] = 'X'; shapeArray[c]
' '; shapeArray[b][1] = 'X'; shapeArray[c]
' '; shapeArray[b][2] = ' '; shapeArray[c]
' '; shapeArray[b][3] = ' '; shapeArray[c] ' '; shapeArray[b][0] = 'X'; shapeArray[c]
' '; shapeArray[b][1] = 'X'; shapeArray[c]
'X'; shapeArray[b][2] = 'X'; shapeArray[c]
' '; shapeArray[b][3] = ' '; shapeArray[c] {
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{
{ '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()
{
for (int i = 0; i<width; 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()
{
setCursorTo(0, 0);
for (int x = 0; x <= height; x++)
{
for (int y = 0; y < width; y++)
{
cout << bucket[x][y];
}
cout << endl;
///change this to draw bucket nicely
}
}
/// this variable need to declare outside this void ' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
'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' },
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
} /// else it will keep refresh and reset
int movedown = 0;
void updateBucket(TetrisShape localTetrisShape)
{
/*
movedown++;
int shapeTopLeftX = 6;
for (int i = 0; i<width; i++)
{
for (int a = 0; a<4; a++)
{
///an algorithm prevent array overload
if (i>i - 4)
{
break;
}
///to check the collision reach the goal/bottom or not
if (localTetrisShape.stoppointY[a] + movedown >=
bucketobjectheight[i + a] && localTetrisShape.active == true)
{
for (int j = 0; j < 4; j++)
{
///to assign new max goal distance
bucketobjectheight[localTetrisShape.shapeTopLeftX
+ j] -= localTetrisShape.stoppointY[j];
}
///deactive object
localTetrisShape.active = false;
}
}
}
*/
///object move
if (localTetrisShape.active == true)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (localTetrisShape.shapeArray[i][j]=='X')
bucket[localTetrisShape.shapeTopLeftY + i]
[localTetrisShape.shapeTopLeftX + j] = localTetrisShape.shapeArray[i][j];
}
}
}
}
int getUserInput()
{
int input = 0;
while (_kbhit())
{
input = _getch();
//cout << input;
}
return input;
} void cleanShape(TetrisShape shape)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bucket[shape.shapeTopLeftY + i][shape.shapeTopLeftX + j] !
= 'x' ) bucket[shape.shapeTopLeftY + i][shape.shapeTopLeftX + j] = ' ';
}
}
}
int moveShape(int input, int& newShapeTopLeftX, int& newShapeTopLeftY, char
array[4], TetrisShape shape)
{
enum key { UP_ARROW = 72, DOWN_ARROW = 80, LEFT_ARROW = 75, RIGHT_ARROW =
77 };
int flag = 0;
switch (input)
{
case UP_ARROW:
char arrayAux[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arrayAux[i][j] = array[3 - j][i];
}
}
if (shape.shapeTopLeftX <= 0)
{
for (int i = 0; i < 4; i++)
{
if (arrayAux[i][0] == 'X') flag = 1;;
}
}
if (shape.shapeTopLeftX+4 >= width)
{
for (int i = 0; i < 4; i++)
{
if (arrayAux[i][3] == 'X') flag = 1;
}
}
if (flag==0)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
//if (arrayAux[i][j]=='X')
array[i][j] = arrayAux[i][j];
}
}
}
break;
case DOWN_ARROW:
if (newShapeTopLeftY + 4 >= 25) flag = 2;
if (flag == 0) newShapeTopLeftY++;
break; } case LEFT_ARROW:
if (newShapeTopLeftX > 1 )
newShapeTopLeftX--;
else if (newShapeTopLeftY + 4 >= 25) flag = 2;
if (flag == 0) newShapeTopLeftY++;
break;
case RIGHT_ARROW:
if (newShapeTopLeftX+4 <12)
newShapeTopLeftX++;
else if (newShapeTopLeftY + 4 >= 25) flag = 2;
if (flag == 0) newShapeTopLeftY++;
break;
case 0:
if (newShapeTopLeftY + 4 >= 25) flag = 2;
if (flag==0) newShapeTopLeftY++;
default:
break;
}
return flag; int _tmain(int argc, _TCHAR* argv)
{
int gameOver = 0;
InitializeBucket();
while (gameOver == 0)
{
int userInput=0;
int bottom = 0;
///must update ur bucket first than only draw
/// else it will not accurate
srand(time(NULL));
int number = rand() % 6 + 1;
TetrisShape shape;
shape.populateShapeArray(number);
while (bottom!=2)
{
userInput = getUserInput();
cleanShape(shape);
bottom = moveShape(userInput, shape.shapeTopLeftX,
shape.shapeTopLeftY, shape.shapeArray, shape);
updateBucket(shape);
drawBucket();
Sleep(500);
}
}
return 0;
}
-----------