Advertisement
Guest User

c

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int main(void)
  5. {
  6. int i,j,k1,k2,k3,k4,r,answer;
  7. srand((unsigned) time(NULL) + getpid());
  8.  
  9. for(i=0;i<1;i++)
  10. {
  11. r=rand()%9999;
  12. }
  13. int a,A,B,chance,counterA,counterB;
  14. int w = r / 1 % 10;
  15. int x = r / 10 % 10;
  16. int y = r / 100 % 10;
  17. int z = r / 1000 % 10;
  18.  
  19. printf("歡迎使用1a2b小遊戲\n");
  20. printf("\n請輸入猜謎次數上限次數: ",chance);
  21. scanf("%d",&chance);
  22. printf("遊戲次數共有%d次 \n",chance);
  23.  
  24.  
  25. for( j = 1; j <= chance ; j++ )
  26. {
  27.  
  28. printf("\n請輸入四位數字: ");
  29. scanf("%d",&a);
  30. printf("您輸入的數字是:%04d \n",a);
  31. printf("剩餘遊戲次數: %d 次\n",chance-j);
  32. int b = a / 1 % 10;
  33. int c = a / 10 % 10;
  34. int d = a / 100 % 10;
  35. int e = a / 1000 % 10;
  36.  
  37. counterA = counterB = 0;
  38.  
  39. if(b==w){counterA++;}
  40. else if(b==x){counterB++;}
  41. else if(b==y){counterB++;}
  42. else if(b==z){counterB++;}
  43.  
  44. if(c==x){counterA++;}
  45. else if(c==w){counterB++;}
  46. else if(c==y){counterB++;}
  47. else if(c==z){counterB++;}
  48.  
  49.  
  50. if(d==y){counterA++;}
  51. else if(d==w){counterB++;}
  52. else if(d==x){counterB++;}
  53. else if(d==z){counterB++;}
  54.  
  55. if(e==z){counterA++;}
  56. else if(e==w){counterB++;}
  57. else if(e==x){counterB++;}
  58. else if(e==y){counterB++;}
  59. printf("目前的狀況是:%d A %d B \n",counterA,counterB);
  60. if (counterA == 4 )
  61. {printf("\n恭喜你答對了!Congratulations!\n"); }
  62.  
  63. if(0 >= chance-j)
  64. {
  65. printf("\n遊戲結束!你輸了!\n");
  66. printf("正確答案是:%04d",r);
  67. }
  68.  
  69. }
  70. while (answer == 1 || answer == 0 || counterA == 4 )
  71. {
  72. printf("\n\n是否重新進行遊戲?(輸入1繼續,輸入0結束):",answer);
  73. scanf("%d",&answer);
  74. if(answer == 1){printf("\n\n"); return main();}
  75. else if(answer == 0){printf("\n\n遊戲結束!");break;}
  76. }
  77.  
  78.  
  79. system("pause");
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement