Advertisement
Guest User

project 1 2019 c2 finished

a guest
Apr 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #define MIN 3
  7. #define MAX 24
  8.  
  9. int getCoords(char str[20], int arg);
  10. void bomb(int a, int b, char** level,int N,int M, int *score);
  11. void hammer(int a, int b, char** level, char c,int N,int M, int *score);
  12. void gravity(char **level, int type,int N,int M);
  13.  
  14. int main()
  15. {
  16.     int i,j, k, N, M, flag, x, y, exit, c=0, levelcomplete, pista=1, temp;
  17.     int *score;
  18.     char str[20];
  19.     char **level;
  20.  
  21.     do{
  22.         printf("Dwse arithmo diastasewn.\n");
  23.         scanf("%d%d",&N,&M);
  24.         if(N<MIN||N>MAX||M<MIN||M>MAX)
  25.             printf("\n[!] Oi dunates distaseis einai megaluteres tou %d kai mikroteres tou %d.\n\n",MIN,MAX);
  26.     }while(N<MIN||N>MAX||M<MIN||M>MAX);
  27.     level = (char**) malloc(N * sizeof(char*));
  28.     score = (int*)malloc(sizeof(int));
  29.     exit = 0;
  30.     str[8] = 0;
  31.     srand(time(NULL));
  32.     for(i=0;i<N;i++){
  33.         level[i] = (char*) malloc(M * sizeof(char));
  34.         for(j=0;j<M;j++){
  35.             level[i][j] = rand()%9 + 49;
  36.         }
  37.     }
  38.     if(level==NULL||score==NULL){
  39.         exit = 1;
  40.         printf("H KATAXWRHSH MNHMHS APETYXE\n");
  41.     }
  42.     *score = 0;
  43.     do{
  44.         if(exit==1)
  45.             continue;
  46.         levelcomplete = 0;
  47.         printf("    ");
  48.         for(i=0;i<M;i++){
  49.             printf("%d ",i+1); // sthles
  50.         }
  51.         printf("\n");
  52.         printf(" --");
  53.         if(M>9)
  54.             printf("-");
  55.         for(i=0;i<M;i++){
  56.             if(i>9){
  57.                 printf("-");
  58.             }
  59.             printf("--");
  60.         }
  61.         printf("\n");
  62.         for(i=0;i<N;i++) // grammes
  63.         {
  64.             if(i<9){
  65.                 printf(" ");
  66.             }
  67.             printf("%d|", i+1);
  68.             for(j=0;j<M;j++)
  69.             {
  70.                 if(j<9){
  71.                      printf(" %c",level[i][j]);
  72.                 }else
  73.                      printf("  %c",level[i][j]);
  74.             }
  75.  
  76.  
  77.             printf("\n");
  78.         }
  79.  
  80.         flag = 0;
  81.         do{
  82.             if(flag==1)
  83.             {
  84.                 printf("\n[!]Kati phge strava, prospathise pali!\n");
  85.             }
  86.  
  87.             printf("\n\nPista #%d, Score: %d\nDialekse thn epomenh kinhsh sou.\n", pista, *score);
  88.             if(c==0)
  89.                 while ((c = getchar()) != '\n' && c != EOF);
  90.             c=1;
  91.             fgets(str, 20, stdin);
  92.             printf("\n\n");
  93.             for(i=0;i<20;i++){
  94.                 if(str[i]=='\n')
  95.                     str[i]='\0';
  96.             }
  97.  
  98.             /*
  99.             Typoi Entolwn:
  100.  
  101.             h A,B
  102.             b A,B
  103.             pr
  104.             pl
  105.             a A,B C(1-9)
  106.             x
  107.  
  108.             */
  109.             flag = 0;
  110.             if((str[0]=='h'||str[0]=='b'||str[0]=='a')&&str[1]==' '){
  111.                 i = 1;
  112.                 do{
  113.                     i++;
  114.                     if(str[i]<48||str[i]>57){
  115.                        flag = 1;
  116.                     }
  117.  
  118.                 }while(flag==0);
  119.                 if(str[i]==','){
  120.                     flag = 0;
  121.                 }
  122.                 if(flag==0){
  123.                     do{
  124.                         i++;
  125.                         if(str[i]<48||str[i]>57){
  126.                            flag = 1;
  127.                         }
  128.  
  129.                     }while(flag==0);
  130.                 }
  131.                 if(str[i]=='\0'&&(str[0]=='h'||str[0]=='b')){
  132.                     flag = 0; // entolh typou h/b A,B
  133.                     x = getCoords(str, 1);
  134.                     y = getCoords(str, 2);
  135.                     if(x > 0 && x <= N && y > 0 && y <= M ){
  136.                         if(str[0]=='b')
  137.                         {
  138.                             bomb(x-1,y-1,level, N, M, score);
  139.                             gravity(level,0,N,M);
  140.                         }else if(str[0]=='h'){
  141.                             hammer(x-1,y-1,level,level[x-1][y-1], N, M, score);
  142.                             gravity(level,0,N,M);
  143.  
  144.                         }
  145.  
  146.                     }else{
  147.                         flag = 1;
  148.                     }
  149.                 }
  150.                 else if(str[i]=' '&&(str[i+1]>=49&&str[i+1]<=57)){
  151.                     flag = 0; //entolh typou a A,B C(1-9)
  152.                     x = getCoords(str, 1);
  153.                     y = getCoords(str, 2);
  154.                     if(x > 0 && x <= N && y > 0 && y <= M ){
  155.                         k=0;
  156.                         while(str[k]!='\0'){
  157.                             k++;
  158.                         }
  159.                         if(level[x-1][y-1]=='.'){
  160.                             level[x-1][y-1]=str[k-1];
  161.                             gravity(level,0,N,M);
  162.                         }
  163.                         else{
  164.                             printf("\n[!]To plakidio prepei na einai keno.\n");
  165.                             flag=1;
  166.                         }
  167.                     }
  168.                     else
  169.                         flag = 1;
  170.                 }
  171.             }
  172.             else if(str[0]=='p'){
  173.                 if(str[1]=='r'&&str[2]=='\0')
  174.                 {
  175.                     flag = 0; // entolh pr
  176.                     gravity(level,1,N,M);
  177.                 }
  178.  
  179.                 else if(str[1]=='l'&&str[2]=='\0')
  180.                 {
  181.                     flag = 0; // entolh pr
  182.                     gravity(level,2,N,M);
  183.                 }
  184.             }
  185.             else if((str[0]=='x'||str[0]=='X')&&str[1]=='\0')
  186.             {
  187.                 flag = 0;
  188.                 exit = 1;
  189.                 printf("\n\nHello darkness, my old friend\nIve come to talk with you again\nBecause a vision softly creeping\nLeft its seeds while I was sleeping\nAnd the vision that was planted in my brain\nStill remains\nWithin the sound of silence\n\nIn restless dreams I walked alone\nNarrow streets of cobblestone\nNeath the halo of a street lamp\nI turned my collar to the cold and damp\nWhen my eyes were stabbed by the flash of a neon light\nThat split the night\nAnd touched the sound of silence\n\nAnd in the naked light I saw\nTen thousand people, maybe more\nPeople talking without speaking\nPeople hearing without listening\nPeople writing songs that voices never share\nNo one dared\nDisturb the sound of silence\n\nFools said I, You do not know\nSilence like a cancer grows\nHear my words that I might teach you\nTake my arms that I might reach you\nBut my words like silent raindrops fell\nAnd echoed in the wells of silence\n\nAnd the people bowed and prayed\nTo the neon god they made\nAnd the sign flashed out its warning\nIn the words that it was forming\n\nAnd the sign said, The words of the prophets\nAre written on the subway walls\nAnd tenement halls\nAnd whispered in the sounds of silence\n\n");
  190.             }
  191.             else
  192.                 flag = 1;
  193.  
  194.         }while(flag == 1);
  195.         levelcomplete = 1;
  196.         i=0;
  197.         do{
  198.             j=0;
  199.             do{
  200.                 if(level[i][j]!='.')
  201.                     levelcomplete = 0;
  202.                 j++;
  203.             }while(j<M&&levelcomplete==1);
  204.             i++;
  205.         }while(i<N&&levelcomplete==1);
  206.         if(levelcomplete==1){
  207.             free(level);
  208.             printf("\n\nNikhses thn pista megethous: %d X %d!!!\n",N,M);
  209.             if(N<MAX&&M<MAX){
  210.                 N++;
  211.                 M++;
  212.                 printf("As anevasoume ligo thn duskolia me mia pista megethous: %d X %d\n\n",N,M);
  213.                 level = (char**) malloc(N * sizeof(char*));
  214.                 for(i=0;i<N;i++){
  215.                     level[i] = (char*) malloc(M * sizeof(char));
  216.                     for(j=0;j<M;j++){
  217.                         level[i][j] = rand()%9 + 49;
  218.                     }
  219.                 }
  220.             }
  221.             else{
  222.                 printf("Termatises to paixnidi sthn megaluterh pista WOW EISAI TOSO KALOOOOOS!!!!!\n");
  223.                 exit=1;
  224.             }
  225.             pista++;
  226.         }
  227.  
  228.     }while(exit == 0);
  229.     printf("\n\nPerasmenes Pistes: %d\n\nScore: %d\n\n", pista-1, *score);
  230.     free(level);
  231.     free(score);
  232.  
  233.  
  234.     return 0;
  235. }
  236.  
  237.  
  238.  
  239. void bomb(int a, int b, char** level,int N,int M, int *score){
  240.     if(level[a][b] != '.'){
  241.         level[a][b] = '.';
  242.         *score+=1;
  243.     }
  244.     if((a-1>=0)&&b-1>=0){
  245.         level[a-1][b-1] = '.';
  246.         *score+=1;
  247.     }
  248.     if((a+1<=N-1)&&(b+1<=M-1)){
  249.         level[a+1][b+1] = '.';
  250.         *score+=1;
  251.     }
  252.     if((a-1>=0)&&b+1<=M-1){
  253.         level[a-1][b+1] = '.';
  254.         *score+=1;
  255.     }
  256.     if((a+1<=N-1)&&b-1>=0){
  257.         level[a+1][b-1] = '.';
  258.         *score+=1;
  259.     }
  260.     if(b-1>=0){
  261.         level[a][b-1] = '.';
  262.         *score+=1;
  263.     }
  264.     if(b+1<=M-1){
  265.         level[a][b+1] = '.';
  266.         *score+=1;
  267.     }
  268.     if(a-1>=0){
  269.         level[a-1][b] = '.';
  270.         *score+=1;
  271.     }
  272.     if(a+1<=N-1){
  273.         level[a+1][b] = '.';
  274.         *score+=1;
  275.     }
  276. }
  277.  
  278. void hammer(int a, int b, char** level,char c,int N,int M,int *score){
  279.  
  280.     if(level[a][b] != '.'){
  281.         level[a][b] = '.';
  282.         *score+=3;
  283.         if(a-1>=0)
  284.             if(level[a-1][b] == c)
  285.             {
  286.                 hammer(a-1,b,level,c,N,M,score);
  287.             }
  288.         if(a+1<=N-1)
  289.             if(level[a+1][b] == c)
  290.             {
  291.                 hammer(a+1,b,level,c,N,M,score);
  292.             }
  293.         if(b-1>=0)
  294.             if(level[a][b-1] == c)
  295.             {
  296.                 hammer(a,b-1,level,c,N,M,score);
  297.             }
  298.         if(b+1<=M-1)
  299.             if(level[a][b+1] == c)
  300.             {
  301.                 hammer(a,b+1,level,c,N,M,score);
  302.             }
  303.     }
  304.  
  305. }
  306.  
  307.  
  308. void gravity(char **level, int type,int N,int M)
  309. {
  310.     int i,j,k,max;
  311.     if(N>M)
  312.         max = N;
  313.     else
  314.         max = M;
  315.     for(k=0;k<max+1;k++){
  316.         for(i=0;i<N;i++)
  317.         {
  318.             for(j=0;j<M;j++)
  319.             {
  320.                 if(type==0&&i+1<=N-1)
  321.                 {
  322.  
  323.                     if(level[i+1][j] =='.')
  324.                     {
  325.                         level[i+1][j] = level[i][j];
  326.                         level[i][j] = '.';
  327.                     }
  328.                 }
  329.                 else if(type==1&&j+1<=M-1)
  330.                 {
  331.  
  332.                     if(level[i][j+1] =='.')
  333.                     {
  334.                         level[i][j+1] = level[i][j];
  335.                         level[i][j] = '.';
  336.                     }
  337.                 }
  338.                 else if(type==2&&j-1>=0)
  339.                 {
  340.  
  341.                     if(level[i][j-1] =='.')
  342.                     {
  343.                         level[i][j-1] = level[i][j];
  344.                         level[i][j] = '.';
  345.                     }
  346.                 }
  347.             }
  348.         }
  349.  
  350.     }
  351.  
  352. }
  353.  
  354. int getCoords(char str[20], int arg){
  355.     int i,m,j, coord;
  356.     char stop='\0';
  357.     i = -1;
  358.     m = 0;
  359.     coord = 0;
  360.     if(str[0]=='a')
  361.         stop=' ';
  362.     if(arg == 1){
  363.         do{
  364.             i++;
  365.         }while(str[i]!=',');
  366.         do{
  367.             i--;
  368.             if(m==0)
  369.                 coord +=(str[i]-48);
  370.             else if(m==1)
  371.                 coord += (str[i]-48)*10;
  372.             m++;
  373.         }while(str[i-1]!=' ');
  374.     }
  375.     else{
  376.         do{
  377.             i++;
  378.         }while(str[i]!=stop); //stop: '\0' or ' '
  379.         if(stop ==' '){
  380.             do{
  381.                 i++;
  382.             }while(str[i]!=1); //stop: ' '
  383.  
  384.         }
  385.         do{
  386.             i--;
  387.             if(m==0)
  388.                 coord +=(str[i]-48);
  389.             else if(m==1)
  390.                 coord += (str[i]-48)*10;
  391.             m++;
  392.         }while(str[i-1]!=',');
  393.     }
  394.  
  395.     return coord;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement