猜拳遊戲問題解答 1.讓遊戲玩的次數可以選擇。 (但若輸入 1 以下, 則遊戲結束) int y,c,p,n; //y 遊戲次數計數. c 電腦猜測, p 玩家猜測, n 遊戲次數 int y,c,p,n;//y 遊戲次數計數. c 電腦猜測, p 玩家猜測, n 遊戲次數 printf("Input the number of games :");scanf("%d",&n); if(n<1){ printf("Game over."); } else { for(y=0;y<n;y++){ printf("Input your select (0:scissors 1:stone 2:cloth):"); scanf("%d",&p); c=rand()%3; // 0,1,2 剪刀,石頭,布 printf("Computer guess it is %d\nresult:",c); if(p==c){ //電腦與玩家猜得一樣 printf("draw\n"); } else if(((p==0)&&(c==1))||((p==1)&&(c==2))||((p==2)&&(c==0))){ printf("You loss\n"); } else { printf("You win\n"); } } } } 2.讓遊戲結束後可以統計玩家贏的次數與輸的次數, 並顯示玩家勝率。 (勝率公式: 勝的次數/遊戲總次數) //y 遊戲次數計數. c 電腦猜測, p 玩家猜測, n 遊戲次數, win 玩家贏次數, loss 玩家輸次數, x 勝率控制 int y,c,p,n,win,loss; printf("Input the number of games :");scanf("%d",&n); if(n<1){ printf("Game over."); } else { win=loss=0; for(y=0;y<n;y++){ printf("Input your select (0:scissors 1:stone 2:cloth):"); scanf("%d",&p); printf("Computer guess it is %d\nresult:",c); if(p==c){ //電腦與玩家猜得一樣 printf("draw\n"); } else if(((p==0)&&(c==1))||((p==1)&&(c==2))||((p==2)&&(c==0))){ printf("You loss\n"); loss++; } else { printf("You win\n"); win++; } } printf("The number of wins:%d\n",win); printf("The number of lost:%d\n",loss); printf("The possibility of winning the game:%4.1f%%\n",(float)win/n*100); } return 0; } 3.控制玩家必勝。 //y 遊戲次數計數. c 電腦猜測, p 玩家猜測, n 遊戲次數, win 玩家贏次數, loss 玩家輸次數 int y,c,p,n,win,loss; printf("Input the number of games :");scanf("%d",&n); if(n<1){ printf("Game over."); } else { win=loss=0; for(y=0;y<n;y++){ printf("Input your select (0:scissors 1:stone 2:cloth):"); scanf("%d",&p); c=(p+2)%3; // 相當於 p-1 並循環,當 p=1,則 c=0;當 p=2,則 c=1;當 p=0,則 c=2; printf("Computer guess it is %d\nresult:",c); if(p==c){ //電腦與玩家猜得一樣 printf("draw\n"); } else if(((p==0)&&(c==1))||((p==1)&&(c==2))||((p==2)&&(c==0))){ printf("You loss\n"); loss++; } else { printf("You win\n"); win++; } } printf("The number of wins:%d\n",win); printf("The number of lost:%d\n",loss); printf("The possibility of winning the game:%4.1f%%\n",(float)win/n*100); } return 0; } 4.控制玩家勝率約在 10% 左右。 //y 遊戲次數計數. c 電腦猜測, p 玩家猜測, n 遊戲次數, win 玩家贏次數, loss 玩家輸次數, x 勝率控制 int y,c,p,n,win,loss,x; printf("Input the number of games :");scanf("%d",&n); if(n<1){ printf("Game over."); } else { win=loss=0; for(y=0;y<n;y++){ printf("Input your select (0:scissors 1:stone 2:cloth):" ); scanf("%d",&p); x=rand()%100; if(x<10) //玩家必勝 c = (p+2)%3; // 相當於 p-1 並循環 else if(x<55) //電腦必勝 c = (p+1)%3; else //平手 c = p; printf("computer guess it is %d\nresult:",c); if(p==c){ //電腦與玩家猜得一樣 printf("draw\n"); } else if(((p==0)&&(c==1))||((p==1)&&(c==2))||((p==2)&&(c==0))){ printf("Computer win\n"); loss++; } else { printf("You win\n"); win++; } } printf("The number of wins:%d\n",win); printf("The number of lost:%d\n",loss); printf("The possibility of winning the game:%4.1f%%\n",(float)win/n*100); } |