Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. int Score(int score) { //FUNCTION TO CALCULATE SCORE
  2. score = score + 100;
  3. return score;
  4. }
  5.  
  6. int main() {
  7. // Two bucket arrays, one shown on the screen, the other with the permanent contents of the buckets (walls and any non-moving shapes)
  8. char bucket[12][25];
  9. char _bucket[12][25];
  10. int score = 0; //DECLARING SCORE VARIABLE FOR HOLDING SCORE
  11. int shapes[256] = {};
  12. int shapeIndex = 0;
  13. bool gameOver = false;
  14.  
  15. generateBucket(bucket);
  16. generateBucket(_bucket);
  17.  
  18. generateShapeStream();
  19.  
  20.  
  21. drawBucket(bucket);
  22.  
  23.  
  24. while (!gameOver) {
  25. gameOver = gameTick(bucket, _bucket);
  26. Sleep(50);
  27. cout << "Your Score: " << score << endl; //THIS IS THE SCORE VARIABLE NEEDING UPDATING
  28. currentTick++;
  29. }
  30. setCursorTo(25, 6);
  31. cout << "GAME OVER";
  32. system("pause");
  33. }
  34.  
  35. void setCursorTo(int x, int y)
  36. {
  37. HANDLE handle;
  38. COORD position;
  39. handle = GetStdHandle(STD_OUTPUT_HANDLE);
  40. position.X = x;
  41. position.Y = y;
  42. SetConsoleCursorPosition(handle, position);
  43. }
  44.  
  45. /* generateBucket - takes a bucket array of any size and
  46. * creates a semi-empty bucket, with a
  47. * few random shapes in the bottom few lines. */
  48. template <size_t rows, size_t cols>
  49.  
  50. void generateBucket(char(&bucket)[rows][cols]) {
  51. for (int w = 0; w < 12; w++) {
  52. for (int z = 0; z < 25; z++) {
  53. if (((w == 0) || (w == 11)) && (z == 0)) { bucket[w][z] = '.'; }
  54. else if (((w % 12 == 0) || (w % 12 == 11)) && ((z > 0) && (z < 24))) { bucket[w][z] = '|'; }
  55. else if (((w == 0) || (w == 11)) && (z == 24)) { bucket[w][z] = '+'; }
  56. else if (z == 24) { bucket[w][z] = '-'; }
  57. else { bucket[w][z] = ' '; }
  58. }
  59. }
  60. }
  61.  
  62. /* generateShapeStream - generates a pre-determined list of
  63. * shapes that will fall from the sky. */
  64. void generateShapeStream() {
  65. // Initialize the random number generator
  66. srand(time(NULL));
  67.  
  68. for (int i = 0; i < 256; i++) {
  69. shapes[i] = rand() % 6 + 1;
  70. }
  71. //cout << "In generate shape..." << endl;
  72. }
  73.  
  74. /* drawBucket - draws the actual bucket on the screen
  75. * including the currently dropping shape. */
  76. template <size_t rows, size_t cols>
  77. void drawBucket(char(&bucket)[rows][cols]) {
  78. setCursorTo(0, 0);
  79. for (int w = 0; w < 25; w++) {
  80. for (int z = 0; z < 12; z++) {
  81. cout << bucket[z][w];
  82. }
  83. cout << endl;
  84. }
  85. }
  86.  
  87. /* gameTick - this function does all of the different
  88. * processessing that happens throughout
  89. * the game. This also returns false to
  90. * stop the main loop once gameover has
  91. * been reached */
  92. template <size_t rows, size_t cols>
  93. bool gameTick(char(&bucket)[rows][cols], char(&perm_bucket)[rows][cols]) {
  94. drawBucket(bucket);
  95. if (!isDropping) {
  96. currentShape++;
  97. activeShape = TetrisShape(shapes[currentShape]);
  98.  
  99.  
  100. if (!canEnter(DIR_DOWN, perm_bucket)) {
  101. return true;
  102. }
  103. else {
  104. isDropping = true;
  105. updateBucket(bucket, false);
  106. }
  107. }
  108. else {
  109. if (currentTick % GAME_INTERVAL == 1) {
  110. // we are on a drop interval.
  111. if (canEnter(DIR_DOWN, perm_bucket)) {
  112.  
  113. updateBucket(bucket, moveShape(DIR_DOWN));
  114. }
  115. else {
  116. landShape(perm_bucket);
  117.  
  118. }
  119. }
  120. }
  121. int direction = getUserInput();
  122. if (canEnter(direction, perm_bucket)) {
  123.  
  124. updateBucket(bucket, moveShape(direction));
  125. }
  126.  
  127. if (!canEnter(DIR_DOWN, perm_bucket)) {
  128. landShape(perm_bucket);
  129. set_bucket(bucket, perm_bucket);
  130. }
  131.  
  132.  
  133. return false;
  134. }
  135.  
  136.  
  137. /* moveShape - Handles moving the shape in the bucket. */
  138. bool moveShape(int direction) {
  139. previousX = activeShape.shapeTopLeftX;
  140. previousY = activeShape.shapeTopLeftY;
  141. switch (direction) {
  142. case DIR_DOWN:
  143. activeShape.shapeTopLeftY++;
  144. return false;
  145. break;
  146. case DIR_RIGHT:
  147. activeShape.shapeTopLeftX++;
  148. return false;
  149. break;
  150. case DIR_LEFT:
  151. activeShape.shapeTopLeftX--;
  152. return false;
  153. break;
  154. case DIR_ROTATE:
  155. activeShape.rotate();
  156. return true;
  157. break;
  158. }
  159.  
  160. }
  161.  
  162. /* updateBucket - place the cureret shape in the bucket, remove the old shape*/
  163. template <size_t rows, size_t cols>
  164. void updateBucket(char(&bucket)[rows][cols], bool isRotation) {
  165. for (int _x = 0; _x < 4; _x++) {
  166. for (int _y = 0; _y < 4; _y++) {
  167. if (!isRotation) {
  168. if ((activeShape.shapeArray[_x][_y] != ' ') && (bucket[_x + previousX][_y + previousY] != '|') && (bucket[_x + previousX][_y + previousY] != '-')) {
  169. bucket[_x + previousX][_y + previousY] = ' ';
  170. }
  171. }
  172. else {
  173. if ((bucket[_x + previousX][_y + previousY] != '|') && (bucket[_x + previousX][_y + previousY] != '-')) {
  174. bucket[_x + previousX][_y + previousY] = ' ';
  175. }
  176. }
  177. }
  178. }
  179. for (int _x = 0; _x < 4; _x++) {
  180. for (int _y = 0; _y < 4; _y++) {
  181. if (activeShape.shapeArray[_x][_y] != ' ') {
  182. bucket[_x + activeShape.shapeTopLeftX][_y + activeShape.shapeTopLeftY] = activeShape.shapeArray[_x][_y];
  183. }
  184.  
  185. }
  186. }
  187.  
  188.  
  189. }
  190.  
  191. /* landShape - Sets the shape in place once it hits the
  192. bottom of the bucket. Moves the shape to the permanent bucket (_bucket) */
  193. template <size_t rows, size_t cols>
  194. void landShape(char(&bucket)[rows][cols]) {
  195.  
  196. updateBucket(bucket, false);
  197. previousX = 6; previousY = 0;
  198. check_bucket(bucket);
  199.  
  200. isDropping = false;
  201.  
  202. }
  203.  
  204. /* getUserInput - Reads the user input from the player*/
  205. int getUserInput() {
  206. setCursorTo(35, 9);
  207. if ((GetKeyState(VK_DOWN) != 0) && (GetKeyState(VK_DOWN) != 1)) { return DIR_DOWN; }
  208. if ((GetKeyState(VK_RIGHT) != 0) && (GetKeyState(VK_RIGHT) != 1)) { return DIR_RIGHT; }
  209. if ((GetKeyState(VK_LEFT) != 0) && (GetKeyState(VK_LEFT) != 1)) { return DIR_LEFT; }
  210. if ((GetKeyState(VK_UP) != 0) && (GetKeyState(VK_UP) != 1)) { return DIR_ROTATE; }
  211.  
  212. return 0;
  213. }
  214.  
  215.  
  216. /* canRotate - if we are adjacent to another shape, then we CANNOT rotate */
  217. template <size_t rows, size_t cols>
  218. bool canRotate(char(&bucket)[rows][cols]) {
  219. // The only real way to do this is to create a copy of the shape, rotate it, then try to determine where it will be in the bucket.
  220. TetrisShape _tmp = TetrisShape(activeShape);
  221. _tmp.rotate();
  222. for (int _x = 0; _x < 4; _x++) {
  223. for (int _y = 0; _y < 4; _y++) {
  224. if (_tmp.shapeArray[_x][_y] != ' ') {
  225. if (bucket[_tmp.shapeTopLeftX + _x][_tmp.shapeTopLeftY + _y] != ' ') {
  226. return false;
  227. }
  228. }
  229. }
  230. }
  231.  
  232. return true;
  233. }
  234.  
  235. /* canEnter - Tests to see if the falling blocks can
  236. enter in any direction. */
  237. template <size_t rows, size_t cols>
  238. bool canEnter(int dir, char(&bucket)[rows][cols]) {
  239. // Check for collision with any elements of the shape array, and any elements of the bucket.
  240.  
  241. // Determine which direction we are moving.
  242. int delta_x = 0, delta_y = 0;
  243. switch (dir) {
  244. case DIR_DOWN:
  245. delta_y++;
  246. break;
  247. case DIR_LEFT:
  248. delta_x--;
  249. break;
  250. case DIR_RIGHT:
  251. delta_x++;
  252. break;
  253. case DIR_ROTATE:
  254. return canRotate(bucket);
  255. break;
  256. }
  257.  
  258. // Create the starting {x, y} position to test for collsion
  259. int test_x = activeShape.shapeTopLeftX + delta_x;
  260. int test_y = activeShape.shapeTopLeftY + delta_y;
  261.  
  262. for (int _x = 0; _x < 4; _x++) {
  263. for (int _y = 0; _y < 4; _y++) {
  264.  
  265. if (activeShape.shapeArray[_x][_y] != ' ') {
  266. if (bucket[test_x + _x][test_y + _y] != ' ') {
  267. return false;
  268. }
  269. }
  270. }
  271. }
  272. return true;
  273. }
  274.  
  275. template <size_t rows, size_t cols>
  276. void check_bucket(char(&bucket)[rows][cols]) {
  277. for (int y = 0; y < 25; y++) {
  278. int tmp_count = 0;
  279. for (int x = 0; x < 13; x++) {
  280. if (bucket[x][y] == 'X') {
  281. tmp_count++;
  282. }
  283. }
  284. if (tmp_count == 10) {
  285. Score(score); // CALLING SCORE TO UPDATE THE SCORE FOR GETTING A FULL LINE
  286. //UNDECLARED IDENTIFIER?
  287. for (int x = 1; x < 11; x++) {
  288. for (int _y = y; _y > 0; _y--) {
  289. bucket[x][_y] = bucket[x][_y - 1];
  290.  
  291.  
  292. }
  293. }
  294. }
  295. }
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement