Advertisement
MUstar

IoT C언어 0807 - 여름방학프로젝트(nya_mozzi.c)

Aug 9th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "resource/nyamain.h"
  6. //#include "resource/nyaeater.c"
  7.  
  8. void title(void);
  9.  
  10. void addrank(void); //rank Input & Rank Test
  11. void howto(void);
  12. int stage(void);
  13. int game(int stage, int setnum, int *total_score, int *total_time);
  14. void setfood(int base_num, char (*block)[base_num], int setnum, int stage);
  15. void playboard(int base_num, char (*block)[base_num], int cnt, int score, int stage, int time);
  16. void clear(int base_num, char (*block)[base_num]);
  17. int end(int ckend, int eatscr,int bonus, int playscr, int score, int stage);
  18. void prorank(char player[10], int total_score, int total_time);
  19. void sortrank(int *n1, int *n2);
  20. void viewrank(void);
  21. int gameover(int ckend, int total_score,int total_time);
  22.  
  23. int main(void)
  24. {
  25.     srand(time(NULL));
  26.     title();
  27.     system("clear");
  28.     printf("GAME SYSTEM END!\n");
  29.     return 0;
  30. }
  31.  
  32. void title(void)
  33. {
  34.     int i,select;
  35.     while(1)
  36.     {
  37.         __fpurge(stdin);
  38.         system("clear");
  39.         printf("======================\n");
  40.         printf("======================\n");
  41.         for(i=0;i<5;i++)printf("|||                |||\n");
  42.         printf("|||   =K!MOZZI!=   |||\n");
  43.         printf("||| =EAT THE FOOD= |||\n");
  44.         printf("|Press a Key To Start|\n");
  45.         printf("|||  R=Rank|E=End  |||\n");
  46.         printf("|||  M=HowToPLAY?  |||\n");
  47.         for(i=0;i<5;i++)printf("|||                |||\n");
  48.         printf("|||   (C)T.H.Kim   |||\n");
  49.         printf("======================\n");
  50.         printf("======================\n");
  51.         sleep(2);
  52.         select=getch();
  53.    
  54.         if(select==109) howto();
  55.         else if(select==114) viewrank();
  56.         else if(select==112) addrank();
  57.         else if(select==100)
  58.         {
  59.             int temp;
  60.             FILE *rank;
  61.             rank = fopen("resource/rank.sav","w");
  62.             fclose(rank);
  63.             printf("\n\nOK! RANK CLEAR!\nPress Any Key\n");
  64.             temp = getch();
  65.         }
  66.         else if(select==101) break;
  67.         else stage();
  68.     }  
  69. }
  70.  
  71.  
  72. void addrank(void)
  73. {
  74.     system("clear");
  75.     char player[5];
  76.     int score , time;
  77.     printf("enter name/score/time\n>");
  78.     scanf("%s%d%d", player , &score, &time);
  79.     getchar();
  80.     prorank(player,score,time);
  81.     return;
  82. }
  83.  
  84. void howto(void)
  85. {
  86.     system("clear");
  87.     int i;
  88.     printf("======HowToPLAY?======\n");
  89.     printf("======================\n");
  90.     for(i=0;i<6;i++)printf("|||                |||\n");
  91.     printf("|||  D-KEY[<]MOVE  |||\n");
  92.     printf("|||  EAT[F].CLEAR  |||\n");
  93.     printf("|||IF U PL 100sOVER|||\n");
  94.     printf("|||  UR GAMEOVER!  |||\n");
  95.     for(i=0;i<4;i++)printf("|||                |||\n");
  96.     printf("|||================|||\n");
  97.     printf("||| PRESS ANY KEY. |||\n");
  98.     printf("======================\n");
  99.     printf("======================\n");
  100.     int temp = getch();
  101. }
  102.  
  103. int stage(void)
  104. {
  105.     int err, total_score=0, total_time=0;
  106.     time_t start = time(NULL);
  107.     err = game(1,2,&total_score,&total_time);
  108.     if(err!=1)err = game(2,5,&total_score,&total_time);
  109.     if(err!=1)err = game(3,8,&total_score,&total_time);
  110.     if(err!=1)err = game(4,11,&total_score,&total_time);
  111.     if(err!=1)err = game(5,13,&total_score,&total_time);
  112.     time_t gamecls = time(NULL);
  113.     gameover(err,total_score,gamecls-start);
  114. }
  115. int game(int stage, int setnum, int *total_score, int *total_time)
  116. {
  117.     __fpurge(stdin);
  118.     int base_num=16;
  119.     char block[base_num][base_num];
  120.     int cnt=0, x=0,y=0;
  121.     int score=0, bonus=0, playscr;
  122.     time_t start = time(NULL);
  123.    
  124.    
  125.     clear(base_num,block);
  126.     setfood(base_num,block,setnum,stage);
  127.     while(2)
  128.     {
  129.         system("clear");
  130.         int now = time(NULL);
  131.         int time = now - start;
  132.         if(time>100) return 1;
  133.         if(block[x][y]=='F')
  134.         {
  135.             cnt++;
  136.             int randnum = (rand()%10);
  137.             bonus+=randnum;
  138.             score+=100+randnum;
  139.         }
  140.         if(cnt==setnum)
  141.         {
  142.             score+=(100-time);
  143.             *total_score+=score;
  144.             *total_time+=time;
  145.             end(0,setnum*100,bonus,100-time,score,stage);
  146.         }
  147.         block[x][y] = '<';
  148.         if(cnt==setnum)break;
  149.         playboard(base_num,block,cnt,score,stage,time);
  150.         //int value = linux_kbhit();
  151.        
  152.         //while(linux_kbhit()!=-1)
  153.         while(3)
  154.         {
  155.             int plust;
  156.             plust = getch();
  157.             if(plust==27)
  158.             {
  159.                 plust = getch();
  160.                 if(plust==91)
  161.                 {
  162.                     plust = getch();
  163.                     block[x][y] = ' ';
  164.                     switch(plust)
  165.                     {
  166.                         case 65: if(x>0){x--;} break;
  167.                         case 66: if(x<base_num-1){x++;} break;
  168.                         case 67: if(y<base_num-1){y++;} break;
  169.                         case 68: if(y>0){y--;} break;
  170.                         default: break;
  171.                     }
  172.                 }
  173.                 //else title();
  174.             }
  175.             break;
  176.         }
  177.     }
  178.     return 0;
  179. }
  180.  
  181. void setfood(int base_num, char (*block)[base_num], int setnum, int stage)
  182. {
  183.     int foodnum = base_num-1;
  184.     int x, y, cnt=0;
  185.     int rx , ry , r_cnt=0;
  186.     while(1)
  187.     {
  188.         x = (rand()%foodnum);
  189.         y = (rand()%foodnum);
  190.         //printf("%d|%d\n",x,y);
  191.         if(cnt==setnum) break;
  192.         else if(block[x][y]!='F')
  193.         {
  194.             block[x][y]='F';
  195.             cnt++;
  196.         }
  197.     }
  198. }
  199.  
  200. void playboard(int base_num, char (*block)[base_num], int cnt, int score, int stage, int time)
  201. {
  202.     int x=0,y=0;
  203.     //printf("==NYA!MOZZI VER.01A==\n");
  204.     printf("========STAGE%d========\n",stage);
  205.     printf("======================\n");
  206.     while(x<base_num)
  207.     {
  208.         printf("|||");
  209.         while(y<base_num)
  210.         {
  211.             printf("%c",block[x][y]);
  212.             y++;
  213.         }
  214.         printf("|||\n");
  215.         y=0;
  216.         x++;
  217.     }
  218.     printf("======================\n");
  219.     printf("==EAT:%02d|SCORE:%05d==\n",cnt,score);
  220. }
  221.  
  222. void clear(int base_num, char (*block)[base_num])
  223. {
  224.     int x=0,y=0;
  225.     while(x<base_num)
  226.     {
  227.         while(y<base_num)
  228.         {
  229.             block[x][y] = ' ';
  230.             y++;
  231.         }
  232.         y=0;
  233.         x++;
  234.     }
  235. }
  236.  
  237. int end(int ckend, int eatscr,int bonus, int playscr, int score, int stage)
  238. {
  239.     int i;
  240.     printf("========STAGE%d========\n",stage);
  241.     printf("======================\n");
  242.     for(i=0;i<6;i++)printf("|||                |||\n");
  243.     printf("||| EATSCORE:%05d |||\n",eatscr);
  244.     printf("||| BONUSSCR:%05d |||\n",bonus);
  245.     printf("||| PLYTMSCR:%05d |||\n",playscr);
  246.     printf("||| ALLSCORE:%05d |||\n",score);
  247.     for(i=0;i<4;i++)printf("|||                |||\n");
  248.     printf("|||================|||\n");
  249.     printf("||| PRESS ANY KEY. |||\n");
  250.     printf("======================\n");
  251.     printf("======================\n");
  252.     sleep(2);
  253.     int temp = getch();
  254.     //printf("EATSCORE:%05d\n",eatscr);
  255.     //printf("BONUSSCR:%05d\n",bonus);
  256.     //printf("PLYTMSCR:%05d\n",playscr);
  257.     //printf("ALLSCORE:%05d\n",score);
  258.     //printf("PRESS ANY KEY\n");
  259.  
  260. }
  261.  
  262. void prorank(char player[10], int total_score, int total_time)
  263. {
  264.     FILE *rank;
  265.     char name[15][10];
  266.     int score[15];
  267.     int time[15];
  268.     int res, cnt=0, w_cnt=0, err, i, j;
  269.  
  270.     rank = fopen("resource/rank.sav", "r");
  271.     if(rank==NULL)
  272.     {
  273.         rank = fopen("resource/rank.sav","w");
  274.         fclose(rank);
  275.         rank = fopen("resource/rank.sav", "r");
  276.         if(rank==NULL)
  277.         {
  278.             printf("SORRY! RANK ERROR\n");
  279.             sleep(5);
  280.             return;
  281.         }
  282.     }
  283.     strcpy(name[cnt],player);
  284.     score[cnt] = total_score;
  285.     time[cnt] = total_time;
  286.     cnt++;
  287.    
  288.     while(1)
  289.     {
  290.         res = fscanf(rank,"%s%d%d", name[cnt],&score[cnt],&time[cnt]);
  291.         if(res==EOF){fclose(rank);break;}
  292.         cnt++;
  293.     }
  294.  
  295.    
  296.     if(cnt>1)
  297.     {
  298.         for(i=0;i<cnt;i++)
  299.         {
  300.             for(j=i+1;j<cnt;j++)
  301.             {
  302.                 if(score[i]<=score[j])
  303.                 {
  304.                     char temp[10];
  305.                     strcpy(temp,name[i]);
  306.                     strcpy(name[i],name[j]);
  307.                     strcpy(name[j],temp);
  308.  
  309.                     sortrank(&score[i],&score[j]);
  310.                     sortrank(&time[i],&time[j]);
  311.                 }
  312.             }
  313.         }
  314.     }
  315.  
  316.     rank = fopen("resource/rank.sav", "w");
  317.     while(1)
  318.     {
  319.         fprintf(rank,"%s %d %d\n", name[w_cnt],score[w_cnt],time[w_cnt]);
  320.         w_cnt++;
  321.         if(cnt==1){fclose(rank);break;}
  322.         else if(w_cnt==cnt){fclose(rank);break;}
  323.         else continue;
  324.     }
  325.     viewrank();
  326. }
  327.  
  328. void sortrank(int *n1, int *n2)
  329. {
  330.     int temp;
  331.     temp = *n1;
  332.     *n1 = *n2;
  333.     *n2 = temp;
  334. }
  335.  
  336. void viewrank(void)
  337. {
  338.     __fpurge(stdin);
  339.     system("clear");
  340.    
  341.     FILE *rank;
  342.     char name[10][10];
  343.     int score[10]={0}, time[10]={0};
  344.     int res, err, i, cnt=0;
  345.  
  346.  
  347.     for(i=0;i<10;i++) strcpy(name[i],"MOZZI");
  348.     rank = fopen("resource/rank.sav", "r");
  349.     if(rank==NULL) err=1;
  350.  
  351.     while(err!=1)
  352.     {
  353.         res = fscanf(rank,"%s%d%d", name[cnt],&score[cnt],&time[cnt]);
  354.         if(res==EOF){fclose(rank);break;}
  355.         cnt++;
  356.     }
  357.  
  358.     printf("=========RANK=========\n");
  359.     printf("======================\n");
  360.     if(err==1)
  361.     {
  362.         for(i=0;i<7;i++)printf("|||                |||\n");
  363.         printf("|||  SORRY.NORANK  |||\n");
  364.         printf("||| UR.1ST.PLANER! |||\n");
  365.         for(i=0;i<5;i++)printf("|||                |||\n");
  366.         printf("|||================|||\n");
  367.     }
  368.     else
  369.     {
  370.         printf("|NO|PNAME|SCORE|TI:ME|\n");
  371.         printf("|====================|\n");
  372.         for(i=0;i<10;i++)printf("|%02d|%5s|%05d|%02d:%02d|\n",i+1,name[i],score[i],time[i]/60,time[i]%60);
  373.         for(i=0;i<4;i++)printf("|||================|||\n");  
  374.     }
  375.     printf("||| PRESS ANY KEY. |||\n");
  376.     printf("======================\n");
  377.     printf("======================\n");
  378.     sleep(3);
  379.     int temp = getch();
  380. }
  381.  
  382.  
  383.  
  384. int gameover(int ckend, int total_score, int total_time)
  385. {
  386.     __fpurge(stdin);
  387.     char player[10];
  388.     system("clear");
  389.     int i;
  390.     printf("=======GAMEOVER=======\n");
  391.     printf("======================\n");
  392.     for(i=0;i<7;i++)printf("|||                |||\n");
  393.     if(ckend!=1)
  394.     {
  395.         printf("|||    GAMECLR!    |||\n");
  396.         printf("|||SEE U NEXT TIME!|||\n");
  397.     }
  398.     else
  399.     {
  400.         printf("|||    GAMEOVER    |||\n");
  401.         printf("|||U NEED SPEED UP!|||\n");
  402.         for(i=0;i<4;i++)printf("|||                |||\n");
  403.         printf("|||TOTALSCORE|%05d|||\n",total_score);
  404.         printf("||| PLAYTIME |%02d:%02d|||\n",total_time/60,total_time%60);
  405.         printf("||| PRESS ANY KEY. |||\n");
  406.         printf("======================\n");
  407.         printf("======================\n");
  408.         sleep(5);
  409.         int temp = getch();
  410.         return 0;
  411.     }
  412.     for(i=0;i<5;i++)printf("|||                |||\n");
  413.     printf("|||TOTALSCORE|%05d|||\n",total_score);
  414.     printf("||| PLAYTIME |%02d:%02d|||\n",total_time/60,total_time%60);
  415.     printf("======================\n");
  416.     printf("===ENTER YOUR NICK!===\n");
  417.     printf("(MAX 5)>");
  418.     scanf("%s",player);
  419.     sleep(3);
  420.     prorank(player,total_score,total_time);
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement