Guest User

Match 3

a guest
Aug 27th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define SIZE 10
  5. char matrix[SIZE][SIZE] = {
  6.                 {'$','@','*','%','&','#','@','#','#','@'},
  7.                 {'%','$','@','%','#','%','@','*','&','*'},
  8.                 {'#','*','&','*','#','@','@','&','%','@'},
  9.                 {'%','@','*','$','%','&','#','*','@','&'},
  10.                 {'$','*','&','&','&','@','#','%','*','*'},
  11.                 {'#','#','@','#','&','%','*','$','#','#'},
  12.                 {'&','$','$','#','@','#','@','$','%','*'},
  13.                 {'@','$','$','*','&','$','#','*','#','*'},
  14.                 {'%','$','*','@','&','@','&','#','#','#'},
  15.                 {'#','@','%','*','#','&','#','$','%','#'}
  16.             };
  17. char arr[] = {'@','#','$','%','&','*'};
  18. int i,j,k,m,n;
  19. int mxa=10,mya=10,mxb=10,myb=10,play=1;
  20. char chx;
  21. int main(int argc, char *argv[]) {
  22.     srand(time(NULL));
  23.     reCreateMatrix();
  24.     system("PAUSE");   
  25.     return 0;
  26. }
  27. // to make the game feel more animated
  28. waitForIt() { int n; for(n=0;n<30000000;n++); }
  29. // to make the game feel more animated
  30. waitForIt2(int times) { int n; for(n=0;n<30000000*times;n++); }
  31. reCreateMatrix() {
  32.     char r;
  33.     for (i=0; i<SIZE-1; i++) {
  34.         for (j=0;j<SIZE;j++) {
  35.             r = rand_lim(5); // 0 to 5
  36.             matrix[i][j] = arr[r];
  37.         }
  38.     }
  39.     waitForIt();
  40.     doIt();
  41. }
  42. doIt() {
  43.     system("cls");
  44.     if(mxa<10||mya<10||mxb<10||myb<10) {
  45.          chx = matrix[mxa][mya];
  46.          matrix[mxa][mya] = matrix[mxb][myb];
  47.          matrix[mxb][myb] = chx;
  48.     }
  49.     int x = 0;
  50.     while(x<SIZE) {
  51.          // Remove vertical matches
  52.         removeVerticals();
  53.         // Remove horizontal matches
  54.         removeHorizontals();
  55.         // Move down
  56.         moveDown(1);
  57.         printf("Ran : %d",x);
  58.         x++;
  59.     }
  60.    
  61.     //system("cls"); printScreen();
  62.     /*
  63.     printf("Move from:\n");
  64.     printf("Enter X:\n");
  65.     scanf("%d",&mxa);
  66.     printf("Enter Y:\n");
  67.     scanf("%d",&mya);
  68.     printf("Move to:\n");
  69.     printf("Enter X:\n");
  70.     scanf("%d",&mxb);
  71.     printf("Enter Y:\n");
  72.     scanf("%d",&myb);
  73.     play=0;
  74.     if(mxa>9||mya>9||mxb>9||myb>9) {
  75.          printf("Enter a number less than 10 for each value.\n");
  76.          printf("Redirecting....\n");
  77.          waitForIt();
  78.          waitForIt();
  79.          doIt();
  80.          play = 1;
  81.     } else {
  82.          doIt();
  83.          play = 1;
  84.     }*/
  85. }
  86. // to print the screen
  87. printScreen() {
  88.     printf("  | ");
  89.     for (i=0; i<SIZE; i++) {
  90.         printf("%d ",i);
  91.     }
  92.     printf("\n----");
  93.     for (i=0; i<SIZE; i++) {
  94.         printf("--");
  95.     }
  96.     printf("\n");
  97.     for (i=0; i<SIZE; i++) {
  98.         printf("%d | ",i);
  99.         for (j=0;j<SIZE;j++) {
  100.             printf("%c ",matrix[i][j]);
  101.         }
  102.         printf("\n");
  103.     }
  104. }
  105. // remove Verticals
  106. removeVerticals() {
  107.     for (i=0; i<SIZE-2; i++) { //checking only till the third last row
  108.         for (j=0;j<SIZE;j++) {
  109.             if (matrix[i][j] == matrix[i+1][j] && matrix[i+1][j] == matrix[i+2][j]) {
  110.                 matrix[i][j] = ' ';
  111.                 matrix[i+1][j] = ' ';
  112.                 matrix[i+2][j] = ' ';
  113.             }
  114.         }
  115.     }
  116. }
  117. // remove Horizontals
  118. removeHorizontals() {
  119.     for (i=0; i<SIZE; i++) {
  120.         for (j=0;j<SIZE-2;j++) {
  121.             if (matrix[i][j] == matrix[i][j+1] && matrix[i][j+1] == matrix[i][j+2]) {
  122.                 matrix[i][j] = ' ';
  123.                 matrix[i][j+1] = ' ';
  124.                 matrix[i][j+2] = ' ';
  125.             }
  126.         }
  127.     }
  128. }
  129. // to move the digits down
  130. moveDown(int printx) {
  131.     int moved = 0,r;
  132.     for (m=0; m<SIZE; m++) {
  133.         moved = 0;
  134.         for (i=0; i<SIZE-1; i++) {
  135.             for (j=0;j<SIZE;j++) {
  136.                 if (matrix[i+1][j] == ' ') {
  137.                     matrix[i+1][j] = matrix[i][j];
  138.                     matrix[i][j] = ' ';
  139.                     moved = 1;
  140.                 }
  141.             }
  142.         }
  143.         i=0;
  144.         for (j=0;j<SIZE;j++) {
  145.             r = rand_lim(5); // 0 to 5
  146.             if (matrix[i][j] == ' ') {
  147.                 matrix[i][j] = arr[r];
  148.             }
  149.         }
  150.         if(printx==1 && moved==1) {
  151.              system("cls"); printScreen();
  152.              // Remove vertical matches
  153.              removeVerticals();
  154.              // Remove horizontal matches
  155.              removeHorizontals();
  156.              waitForIt();
  157.              moved = 0;
  158.         }
  159.     }
  160. }
  161. int rand_lim(int limit) {
  162. /* return a random number between 0 and limit inclusive.
  163.  */
  164.     int divisor = RAND_MAX/(limit+1);
  165.     int retval;
  166.  
  167.     do {
  168.         retval = rand() / divisor;
  169.     } while (retval > limit);
  170.  
  171.     return retval;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment