Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "nalloc.h"
- #include <time.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdbool.h>
- void initialize(char*,int,int);
- void clearscreen(void);
- int main(void) {
- int a,b,i,j,position,highest=0,place,k;
- printf("Enter the number of balls to drop:");
- scanf("%d",&a);//球數
- printf("Enter the number of slots in the bean machine:");
- scanf("%d",&b);//槽數
- int *lr=nalloc(a*(b-1)*sizeof(int), NULL);//每顆球的路徑(左右)
- int *slot=nalloc(b*sizeof(int), NULL);//每個槽的球數
- int *ball_drop_place=nalloc(a*sizeof(int),NULL);
- srand(time(NULL));
- for(i=0;i<a;i++){//隨機產生結果
- position=b-1;
- for(j=0;j<b-1;j++){
- *(lr+i*(b-1)+j)=rand()%2;//0->rigjt 1->left
- position=*(lr+i*(b-1)+j)?position-1:position+1;
- }
- position/=2;
- *(ball_drop_place+i)=position+1;
- *(slot+position)+=1;
- }
- for(i=0;i<b;i++){//計算最高處
- if(*(slot+i)>highest){
- highest=*(slot+i);
- }
- }
- int horizon=2*b+2;
- char* pinball=(char*)nalloc((highest+b+1)*(2*b+2)*sizeof(char)+1,NULL);
- initialize(pinball,b,highest);
- clearscreen();
- for(i=0;i<a;i++){
- place=b;
- *(pinball+place)='.';
- printf("%s",pinball);
- fflush(stdout);
- usleep(170000);
- clearscreen();
- for(j=0;j<b-1;j++){//彈珠滾動區
- *(pinball+j*horizon+place)=' ';
- place+=*(lr+i*(b-1)+j)?-1:1;
- *(pinball+(j+1)*horizon+place)='.';
- printf("%s",pinball);
- fflush(stdout);
- usleep(170000);
- clearscreen();
- }
- for(k=0;k<highest;k++){//彈珠收集區(槽)
- if(*(pinball+(k+j+1)*horizon+place)==' '){
- *(pinball+(k+j)*horizon+place)=' ';
- *(pinball+(k+j+1)*horizon+place)='.';
- }
- else{
- break;
- }
- printf("%s",pinball);
- fflush(stdout);
- usleep(50000);
- clearscreen();
- }
- }
- sleep(1);
- for(i=0;i<a;i++){//結果輸出
- printf("%d號球路徑:",i+1);
- for(j=0;j<b-1;j++){
- printf("%c ", *(lr+i*(b-1)+j)?'L':'R');
- }
- printf(" 落在%d號槽\n",*(ball_drop_place+i));
- }
- printf("\nballs:");
- for(i=0;i<b;i++){
- printf("%4d",*(slot+i));
- }
- printf(" 個\n\n");
- for(i=highest;i>0;i--){//row
- printf(" |");
- for(j=0;j<b;j++){//column
- printf(" %c |",*(slot+j)-i>=0?'o':' ');
- }
- printf("\n");
- }
- printf(" ");
- for(i=1;i<=b;i++){
- printf("——— ");
- }
- printf("\n ");
- for(i=1;i<=b;i++){
- printf("%d ",i);
- }
- printf("號槽");
- nfree(ball_drop_place);
- nfree(pinball);
- nfree(lr);
- nfree(slot);
- return 0;
- }
- void initialize(char* pinball,int slot,int highest){
- int i=0,j=0,k=0;
- bool nail;
- int horizon=2*slot+2;
- char bottom=' ';
- for(i=0;i<slot;i++){//彈珠滾動區
- nail=false;
- for(j=0;j<slot-i-1;j++){
- *(pinball+i*horizon+j)=' ';
- *(pinball+(i+1)*horizon-j-2)=' ';
- }
- if(i==0){
- *(pinball+slot-1)='|';
- *(pinball+slot+1)='|';
- *(pinball+slot)='.';
- }
- else{
- //j++;不能加
- *(pinball+i*horizon+j)='/';
- j++;
- for(k=0;k<(i*2+1);k++){
- *(pinball+i*horizon+j+k)=nail?'o':' ';
- nail=!nail;
- }
- *(pinball+i*horizon+j+k)='\\';
- }
- }
- for(i=0;i<=highest;i++){//彈珠收集區
- nail=true;
- if(i==highest) bottom='=';
- for(j=0;j<horizon-1;j++){
- *(pinball+(slot+i)*horizon+j)=nail?'|':bottom;
- nail=!nail;
- }
- }
- for(i=0;i<=slot+highest;i++){//設定換行
- *(pinball+i*horizon+horizon-1)=i!=slot+highest?'\n':'\0';
- }
- }
- void clearscreen(){
- printf("\e[1;1H\e[2J");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement