Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "nalloc.h"
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. void initialize(char*,int,int);
  8. void clearscreen(void);
  9. int main(void) {
  10.   int a,b,i,j,position,highest=0,place,k;
  11.   printf("Enter the number of balls to drop:");
  12.   scanf("%d",&a);//球數
  13.   printf("Enter the number of slots in the bean machine:");
  14.   scanf("%d",&b);//槽數
  15.   int *lr=nalloc(a*(b-1)*sizeof(int), NULL);//每顆球的路徑(左右)
  16.   int *slot=nalloc(b*sizeof(int), NULL);//每個槽的球數
  17.   int *ball_drop_place=nalloc(a*sizeof(int),NULL);
  18.   srand(time(NULL));
  19. for(i=0;i<a;i++){//隨機產生結果
  20.   position=b-1;
  21.   for(j=0;j<b-1;j++){
  22.     *(lr+i*(b-1)+j)=rand()%2;//0->rigjt  1->left
  23.     position=*(lr+i*(b-1)+j)?position-1:position+1;
  24.   }
  25.   position/=2;
  26.   *(ball_drop_place+i)=position+1;
  27.   *(slot+position)+=1;
  28. }
  29. for(i=0;i<b;i++){//計算最高處
  30.   if(*(slot+i)>highest){
  31.     highest=*(slot+i);
  32.   }
  33. }
  34. int horizon=2*b+2;
  35. char* pinball=(char*)nalloc((highest+b+1)*(2*b+2)*sizeof(char)+1,NULL);
  36. initialize(pinball,b,highest);
  37. clearscreen();
  38. for(i=0;i<a;i++){
  39.     place=b;
  40.     *(pinball+place)='.';
  41.     printf("%s",pinball);
  42.     fflush(stdout);
  43.     usleep(170000);
  44.     clearscreen();
  45.     for(j=0;j<b-1;j++){//彈珠滾動區
  46.       *(pinball+j*horizon+place)=' ';
  47.       place+=*(lr+i*(b-1)+j)?-1:1;
  48.       *(pinball+(j+1)*horizon+place)='.';
  49.       printf("%s",pinball);
  50.       fflush(stdout);
  51.       usleep(170000);
  52.       clearscreen();
  53.     }
  54.     for(k=0;k<highest;k++){//彈珠收集區(槽)
  55.       if(*(pinball+(k+j+1)*horizon+place)==' '){
  56.       *(pinball+(k+j)*horizon+place)=' ';
  57.        *(pinball+(k+j+1)*horizon+place)='.';
  58.       }
  59.       else{
  60.         break;
  61.       }
  62.       printf("%s",pinball);
  63.       fflush(stdout);
  64.       usleep(50000);
  65.       clearscreen();
  66.     }
  67. }
  68. sleep(1);
  69.  
  70. for(i=0;i<a;i++){//結果輸出
  71.   printf("%d號球路徑:",i+1);
  72.   for(j=0;j<b-1;j++){
  73.     printf("%c ", *(lr+i*(b-1)+j)?'L':'R');
  74.   }
  75.   printf(" 落在%d號槽\n",*(ball_drop_place+i));
  76. }
  77. printf("\nballs:");
  78. for(i=0;i<b;i++){
  79.   printf("%4d",*(slot+i));
  80. }
  81. printf("   個\n\n");
  82. for(i=highest;i>0;i--){//row
  83. printf("       |");
  84.   for(j=0;j<b;j++){//column
  85.     printf(" %c |",*(slot+j)-i>=0?'o':' ');
  86.   }
  87.   printf("\n");
  88. }
  89. printf("        ");
  90. for(i=1;i<=b;i++){
  91.   printf("——— ");
  92. }
  93. printf("\n         ");
  94. for(i=1;i<=b;i++){
  95.   printf("%d   ",i);
  96. }
  97. printf("號槽");
  98.  
  99.   nfree(ball_drop_place);
  100.   nfree(pinball);
  101.   nfree(lr);
  102.   nfree(slot);
  103.   return 0;
  104. }
  105.  
  106. void initialize(char* pinball,int slot,int highest){
  107.   int i=0,j=0,k=0;
  108.   bool nail;
  109.   int horizon=2*slot+2;
  110.   char bottom=' ';
  111.   for(i=0;i<slot;i++){//彈珠滾動區
  112.     nail=false;
  113.    
  114.     for(j=0;j<slot-i-1;j++){
  115.      *(pinball+i*horizon+j)=' ';
  116.      *(pinball+(i+1)*horizon-j-2)=' ';
  117.     }
  118.     if(i==0){
  119.       *(pinball+slot-1)='|';
  120.       *(pinball+slot+1)='|';
  121.       *(pinball+slot)='.';
  122.     }
  123.     else{
  124.       //j++;不能加
  125.       *(pinball+i*horizon+j)='/';
  126.       j++;
  127.       for(k=0;k<(i*2+1);k++){
  128.           *(pinball+i*horizon+j+k)=nail?'o':' ';
  129.           nail=!nail;
  130.       }
  131.       *(pinball+i*horizon+j+k)='\\';
  132.     }
  133.    
  134.   }
  135.   for(i=0;i<=highest;i++){//彈珠收集區
  136.   nail=true;
  137.   if(i==highest) bottom='=';
  138.  
  139.     for(j=0;j<horizon-1;j++){
  140.       *(pinball+(slot+i)*horizon+j)=nail?'|':bottom;
  141.       nail=!nail;
  142.     }
  143.   }
  144.   for(i=0;i<=slot+highest;i++){//設定換行
  145.     *(pinball+i*horizon+horizon-1)=i!=slot+highest?'\n':'\0';
  146.   }
  147. }
  148.  
  149. void clearscreen(){
  150.     printf("\e[1;1H\e[2J");
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement