Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.27 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <conio.h>
  7. #include <random>
  8.  
  9. using namespace std;
  10.  
  11. /* Constants for heights and widths */
  12. const int CONSOLE_WIDTH = 400, CONSOLE_HEIGHT = 400;    // console window (400x400)
  13. const int BUCKET_HEIGHT = 25, BUCKET_WIDTH = 12;        // bucket size (25x12)
  14.  
  15. /* Bucket array 25x12 */
  16. char bucket[BUCKET_HEIGHT][BUCKET_WIDTH];   // allocate memory for the bucket array
  17.  
  18. /* Public variables of type bool */
  19. bool gameLost = false;
  20. bool shapeStuck = false;
  21.  
  22. /* Public variables of type int */
  23. int shapeTopLeftX = 0;
  24. int shapeTopLeftY = 5;
  25. int oldTopLeftX = 0;
  26. int oldTopLeftY = 5;
  27.  
  28. /* TetrisShape Class Definition*/
  29. class TetrisShape {
  30. public:
  31.     void createTetronimo(int shapeType);
  32.     char shapeArray[4][4];
  33. private:
  34.     enum Shapes { Box, Line, S, Z, L, J, T };
  35. };
  36.  
  37. //function declarations
  38. void setConsole();  // positions
  39. void setCursorTo(short x, short y);
  40. void initializeBucket();
  41. void drawBucket();
  42. void getRandNum(int seed);
  43. void drawShape();
  44. void updateBucket(TetrisShape shape, int x, int y);
  45. void processDownArrow(TetrisShape& tetrishape);
  46. void removeTrail(int x, int y);
  47. void moveShape(TetrisShape shape);
  48. int getRandomNumber();
  49. void dropShape(TetrisShape shape);
  50. void inline timeControl(int timer);
  51. void stuckCheck(TetrisShape shape);
  52. void dropNewShape();
  53.  
  54. int wmain()
  55. {
  56.     srand((unsigned int) time(0));
  57.     initializeBucket();
  58.  
  59.     //creates a shape
  60.     TetrisShape shape, temp;
  61.     shape.createTetronimo(getRandomNumber());
  62.  
  63.     while (!gameLost) {
  64.         temp = shape;
  65.         drawBucket();
  66.        
  67.         // testing out a shape dropping
  68.         shapeTopLeftX++; // shape falls, need to check and see if it is stuck still.
  69.         /*
  70.         processDownArrow(shape);
  71.         if (shapeStuck) {
  72.             shape.createTetronimo(getRandomNumber());
  73.  
  74.             system("Pause");
  75.         }
  76.         */
  77.         // end test excerpt
  78.         if (!shapeStuck) {
  79.             removeTrail(oldTopLeftX, oldTopLeftY);
  80.         }
  81.         else {
  82.             dropNewShape();
  83.         }
  84.  
  85.         updateBucket(temp, shapeTopLeftX, shapeTopLeftY);
  86.         drawBucket();
  87.         timeControl(350);
  88.  
  89.         //system("Pause");
  90.  
  91.     }
  92.     system("Pause");
  93. }
  94.  
  95. /* setConsole() - http://www.cplusplus.com/forum/beginner/1481/
  96.    set the console's width and height for windows users,
  97.    solves issues with the console being redrawn. */
  98. void setConsole() {
  99.     HWND console = GetConsoleWindow();
  100.     RECT ConsoleRect;
  101.     GetWindowRect(console, &ConsoleRect);
  102.     MoveWindow(console, ConsoleRect.left, ConsoleRect.top, CONSOLE_WIDTH, CONSOLE_HEIGHT, true);
  103. }
  104.  
  105. /* Gets a random number using the <random> library */
  106. int getRandomNumber() {
  107.     int randomNumber = rand() % 7 + 1;
  108.     return randomNumber;
  109. }
  110.  
  111. // from module 4
  112. void setCursorTo(short x, short y) {
  113.     HANDLE handle;
  114.     COORD position;
  115.     handle = GetStdHandle(STD_OUTPUT_HANDLE);
  116.     position.X = x;
  117.     position.Y = y;
  118.     SetConsoleCursorPosition(handle, position);
  119. }
  120.  
  121. // draw the bucket, created for module 4
  122. void initializeBucket() {
  123.     for (int i = 0; i < BUCKET_HEIGHT; i++) {   // using the constant BUCKET_HEIGHT declared above
  124.         for (int j = 0; j < BUCKET_WIDTH; j++) {// using the constant BUCKET_WIDTH declared above
  125.             if (j == 0 || j == 11 || i == 24) { // columns 1 and 12 and the bottom row (i==24) will be filled
  126.                 bucket[i][j] = '#';
  127.             }
  128.             else if (bucket[i][j] == 'X') { // if the bucket's x,y coords are occupied, do not modify
  129.                 continue;
  130.             }
  131.             else { // if neither condition exists, place an empty char ' '
  132.                 bucket[i][j] = ' ';
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. void drawBucket() {
  139.     setConsole();
  140.     setCursorTo(0, 1);
  141.     initializeBucket();
  142.     for (int i = 0; i < BUCKET_HEIGHT; i++) {
  143.         for (int j = 0; j < BUCKET_WIDTH; j++) {
  144.             cout << bucket[i][j];
  145.             if (j == (BUCKET_WIDTH-1)) cout << endl;    // draws the second column then begins again
  146.         }
  147.     }
  148. }
  149.  
  150. // create the various Tetronimo shapes shown at http://www.tetrisfriends.com/help/tips_appendix.php#tetristerminology
  151. void TetrisShape::createTetronimo(int shapeNumber) {
  152.     if (shapeNumber == 1) {     // O-Tetronimo
  153.         shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = 'X';   shapeArray[3][0] = ' ';
  154.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = 'X';   shapeArray[3][1] = ' ';
  155.         shapeArray[0][2] = ' ';   shapeArray[1][2] = ' ';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  156.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  157.     }
  158.     else if (shapeNumber == 2) {    // I-Tetronimo
  159.         shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = ' ';   shapeArray[3][0] = ' ';
  160.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = ' ';   shapeArray[3][1] = ' ';
  161.         shapeArray[0][2] = ' ';   shapeArray[1][2] = 'X';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  162.         shapeArray[0][3] = ' ';   shapeArray[1][3] = 'X';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  163.     }
  164.     else if (shapeNumber == 3) {    // S-Tetronimo
  165.         shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = 'X';   shapeArray[3][0] = ' ';
  166.         shapeArray[0][1] = 'X';   shapeArray[1][1] = 'X';   shapeArray[2][1] = ' ';   shapeArray[3][1] = ' ';
  167.         shapeArray[0][2] = ' ';   shapeArray[1][2] = ' ';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  168.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  169.     }
  170.     else if (shapeNumber == 4) {    // Z-Tetronimo
  171.         shapeArray[0][0] = 'X';   shapeArray[1][0] = 'X';   shapeArray[2][0] = ' ';   shapeArray[3][0] = ' ';
  172.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = 'X';   shapeArray[3][1] = ' ';
  173.         shapeArray[0][2] = ' ';   shapeArray[1][2] = ' ';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  174.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  175.     }
  176.     else if (shapeNumber == 5) {    // L-Tetronimo
  177.         shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = ' ';   shapeArray[3][0] = ' ';
  178.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = ' ';   shapeArray[3][1] = ' ';
  179.         shapeArray[0][2] = ' ';   shapeArray[1][2] = 'X';   shapeArray[2][2] = 'X';   shapeArray[3][2] = ' ';
  180.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  181.  
  182.     }
  183.     else if (shapeNumber == 6) {    // J-Tetronimo
  184.         shapeArray[0][0] = ' ';   shapeArray[1][0] = 'X';   shapeArray[2][0] = ' ';   shapeArray[3][0] = ' ';
  185.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = ' ';   shapeArray[3][1] = ' ';
  186.         shapeArray[0][2] = 'X';   shapeArray[1][2] = 'X';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  187.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  188.     }
  189.     else if (shapeNumber == 7) { // T-Tetronimo
  190.         shapeArray[0][0] = 'X';   shapeArray[1][0] = 'X';   shapeArray[2][0] = 'X';   shapeArray[3][0] = ' ';
  191.         shapeArray[0][1] = ' ';   shapeArray[1][1] = 'X';   shapeArray[2][1] = ' ';   shapeArray[3][1] = ' ';
  192.         shapeArray[0][2] = ' ';   shapeArray[1][2] = ' ';   shapeArray[2][2] = ' ';   shapeArray[3][2] = ' ';
  193.         shapeArray[0][3] = ' ';   shapeArray[1][3] = ' ';   shapeArray[2][3] = ' ';   shapeArray[3][3] = ' ';
  194.     }
  195. }
  196.  
  197. void updateBucket(TetrisShape shape, int x, int y) {
  198.     for (int i = 0; i < 4; i++) {
  199.         for (int j = 0; j < 4; j++) {
  200.             bucket[i + x][j + y] = shape.shapeArray[i][j];
  201.         }
  202.     }
  203.     oldTopLeftX = shapeTopLeftX;
  204.     oldTopLeftY = shapeTopLeftY;
  205. }
  206.  
  207. void removeTrail(int x, int y) {
  208.     for (int i = 0; i < 4; i++) {
  209.         for (int j = 0; j < 4; j++) {
  210.             if (bucket[i + x][j + y] != '#') {
  211.                 bucket[i + x][j + y] = ' ';
  212.             }
  213.         }
  214.     }
  215. }
  216.  
  217. void inline timeControl(int timer) {
  218.     Sleep(timer);
  219. }
  220.  
  221. void processDownArrow(TetrisShape& tetrisShape) {
  222.     stuckCheck(tetrisShape);
  223.     if (!shapeStuck) {
  224.         shapeTopLeftX++;
  225.     }
  226.     else {
  227.         dropNewShape();
  228.     }
  229. }
  230.  
  231. void stuckCheck(TetrisShape shape) {
  232.     for (int i = 0; i < BUCKET_HEIGHT; i++) {
  233.         for (int j = 0; j < BUCKET_WIDTH; j++) {
  234.             if ((shape.shapeArray[i][j] != ' ') || (shape.shapeArray[i][j] != 'X')) {
  235.                 if (j > 0 && j < 21){
  236.                     shapeStuck = true;
  237.                     return;
  238.                 }
  239.             }
  240.         }
  241.     }
  242.     //shapeStuck = false;
  243. }
  244.  
  245. void dropNewShape() {
  246.     TetrisShape shape;
  247.     shape.createTetronimo(getRandomNumber());
  248.     shapeTopLeftX = 0;
  249.     shapeTopLeftY = 5;
  250.     oldTopLeftX = 0;
  251.     oldTopLeftY = 5;
  252.     shapeStuck = false;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement