In C++ You need to call the function inside the game loop so that it is always d
ID: 3762668 • Letter: I
Question
In C++
You need to call the function inside the game loop so that it is always displayed. But, you might need to initialize it once before the game loop. Below you will find some detailed instruction on the bucket implementation.
Bucket will be a 2-D array of char of 25 x 12 dimension.
Fill the left border (column 1), right border (column 12), and bottom border (row 25) of the bucket with any char that you like to create the border with. Remember, the array index is '1' lower than the actual # of columns. This can be your "initializeBucket()" function. Fill the other cells with empty string ' '. You know that, to go over a 2-D array you need two nested for loops. Right? You also need conditional statements.
Display the bucket in a game loop. How do you do that? You use two for loops and use a "cout....". You can name the function "displayBucket()".
For Windows, use the "Windows.h" library. Use the function below to put your cursor, where you want to display something. Remember the top and left most location of the console is (0,0). So, if you want to draw from the top-left most, you call the function this way, "setCursorTo(0, 0);". Then do your 'cout << "etc etc ..."<<endl;' (see below). If you want to display at some other location, call "setCursorTo" for that location and do a cout again. You can do this "setCursorTo" and "cout<<...." combination as many times as you want. To give bucket a bucket shape, you need to use this "setCursorTo" function appropriately.
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 main(){
setCursorTo(0, 0);
cout <<"etc etc ..."<<endl;
}
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 main(){
setCursorTo(0, 0);
cout <<"etc etc ..."<<endl;
}
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;
const int WIDTH = 400;
const int HEIGHT = 400;
const int buckHeight = 25;
const int buckWidth = 12;
char bucket[buckHeight][buckWidth];
bool running = true;
bool shapeStuck = false;
int randNum;
int shapeTopLeftX = 0;
int shapeTopLeftY = 5;
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};
class TetrisShape
{
public:
void createShape(int shapeType);
char shapeArray[4][4];
private:
enum Shapes{Box, Line, S, Z, L, J, T};
};
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();
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");
}
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);
}