Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- int i,j,k1,k2,k3,k4,r,answer;
- srand((unsigned) time(NULL) + getpid());
- for(i=0;i<1;i++)
- {
- r=rand()%9999;
- }
- int a,A,B,chance,counterA,counterB;
- int w = r / 1 % 10;
- int x = r / 10 % 10;
- int y = r / 100 % 10;
- int z = r / 1000 % 10;
- printf("歡迎使用1a2b小遊戲\n");
- printf("\n請輸入猜謎次數上限次數: ",chance);
- scanf("%d",&chance);
- printf("遊戲次數共有%d次 \n",chance);
- for( j = 1; j <= chance ; j++ )
- {
- printf("\n請輸入四位數字: ");
- scanf("%d",&a);
- printf("您輸入的數字是:%04d \n",a);
- printf("剩餘遊戲次數: %d 次\n",chance-j);
- int b = a / 1 % 10;
- int c = a / 10 % 10;
- int d = a / 100 % 10;
- int e = a / 1000 % 10;
- counterA = counterB = 0;
- if(b==w){counterA++;}
- else if(b==x){counterB++;}
- else if(b==y){counterB++;}
- else if(b==z){counterB++;}
- if(c==x){counterA++;}
- else if(c==w){counterB++;}
- else if(c==y){counterB++;}
- else if(c==z){counterB++;}
- if(d==y){counterA++;}
- else if(d==w){counterB++;}
- else if(d==x){counterB++;}
- else if(d==z){counterB++;}
- if(e==z){counterA++;}
- else if(e==w){counterB++;}
- else if(e==x){counterB++;}
- else if(e==y){counterB++;}
- printf("目前的狀況是:%d A %d B \n",counterA,counterB);
- if (counterA == 4 )
- {printf("\n恭喜你答對了!Congratulations!\n"); }
- if(0 >= chance-j)
- {
- printf("\n遊戲結束!你輸了!\n");
- printf("正確答案是:%04d",r);
- }
- }
- while (answer == 1 || answer == 0 || counterA == 4 )
- {
- printf("\n\n是否重新進行遊戲?(輸入1繼續,輸入0結束):",answer);
- scanf("%d",&answer);
- if(answer == 1){printf("\n\n"); return main();}
- else if(answer == 0){printf("\n\n遊戲結束!");break;}
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement