2014-11-01 105 views
-3

我必須與兩名球員做一個遊戲。每個投擲一個骰子,然後骰子數量更大的那個需要一個點。這發生了十次。有兩名球員的骰子游戲

這個程序好嗎?我該如何做這個遊戲?

int main(int argc, char** argv) 

    int i; 
    int sumplayer1=0,sumplayer2=0; 
    int dice1 = 0; 
    int dice2 = 0; 
    time_t t; 
    srand(time(&t)); 

    for (i=0;i<=10; i++) 
    { 

    dice1 = (rand() % 6); 
    dice2 = (rand() % 6); 
     if (dice1>dice2) 
      sumplayer1=sumplayer1+1; 
     if (dice1<dice2) 
      sumplayer2=sumplayer2+1; 
     if (dice1==dice2){ 
      sumplayer1=sumplayer1; 
      sumplayer2=sumplayer2; 


    } 


      if (sumplayer1>sumplayer2){ 
       printf("player 1 won"); 
      } 
      if (sumplayer1<sumplayer2){ 
       printf("player 2 won"); 
      } 



    return 0; 

} 
} 
+0

排除縮進。在#includes中加入並添加評論。這裏和那裏的空間幾個不會出錯。 – 2014-11-01 12:28:52

+1

*「這個程序可以嗎?」* - 你測試過了嗎?發生了什麼? – jonrsharpe 2014-11-01 12:31:33

+0

調試,調試器,調試!沒有什麼比看你的代碼一行一行地執行來學習和發現像你一樣的錯誤。快樂學習:) – noelicus 2014-11-01 12:48:43

回答

0

No.Your code is not okay。你有一些錯誤在這裏。這裏是糾正的代碼,所有的錯誤更正:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> //neccessary headers 

int main(int argc, char** argv) 
{ // You forgot this 
    int i; 
    int sumplayer1=0,sumplayer2=0; 
    int dice1 = 0; 
    int dice2 = 0; 
    time_t t; 
    srand(time(&t)); 

    for (i=0;i<10; i++) // use < not <= as you want to loop 10 times 
    { 

    dice1 = (rand() % 6)+1; 
    dice2 = (rand() % 6)+1; //+1 because die values are 1-6 not 0-5 
     if (dice1>dice2) 
      sumplayer1++; 
     else if (dice1<dice2) //use else if 
      sumplayer2++; //++ is much easier to understand 
     /*if (dice1==dice2){ // No need of this part 
      sumplayer1=sumplayer1; 
      sumplayer2=sumplayer2;}*/ 
    } 
    if (sumplayer1>sumplayer2){ 
      printf("player 1 won"); 
     } 
     else if (sumplayer1<sumplayer2){ //use else if 
      printf("player 2 won"); 
     }else //There is a possibilty when both players get equal score 
      printf("Its a tie"); 


    return 0; 

} 
//} Extra one here 
+0

rand後的+1並不重要。 0-5,1-6會算出結果相同 – 2014-11-01 12:41:44

+0

我知道。但做OP更方便,因爲OP說它是一款Dice遊戲。 – 2014-11-01 12:53:35