2014-11-01 140 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