骰子游戏与两名球员

时间:2014-11-01 12:27:02

标签: c

我必须和两名球员一起比赛。每个骰子抛出一个骰子,然后骰子数量更大的骰子需要一个骰子。这发生了十次。

这个程序好吗?我该怎么做这个游戏?

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;

}
}

1 个答案:

答案 0 :(得分:0)

不。你的代码不行。你有一些错误。这是纠正错误的纠正代码:

#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