如何计算由随机数生成器的两个组数据创建的2个集合的总和?

问题描述

我正在学校里进行编程任务,要求我用C创建一个骰子游戏,其中赢家是根据(在计算机和玩家之间)掷出的10个骰子总数中的较高者确定的。但是我似乎遇到了一个问题,我的程序会将计算机掷骰子的总和加到用户的掷骰子上,从而确保用户获胜。该程序必须使用srand和rand函数,由于这是我的编程入门,所以我目前不知道如何解决此问题。以下是我编写的代码,因为我没有正确解释该错误的原因,我也不知道该错误可能是什么。非常感谢您的帮助,因为我目前无法联系我的教授

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//Computer Dice Roll
Comp_Dice(int CP_Value,int CP_Sum){
    srand(time(0));
    for(int i = 0; i <10; i++){
    CP_Value = rand()%6+1;
    printf("%d ",CP_Value);
    CP_Sum += CP_Value;

    }
    printf("\nPlayer 1 Total: %d",CP_Sum); 
}
//End of Computer Dice Roll

//Player Data
Plyr_Dice(int Plyr_Value,int Plyr_Sum,int x){
    srand(x);
    for(int e = 0; e <10; e++){
    Plyr_Value = rand()%6+1;
    printf("%d ",Plyr_Value);
    Plyr_Sum +=Plyr_Value;
    
    }
    printf("\nPlayer 2 Total: %d",Plyr_Sum);
}
//End of Player Data

int main(){
    int x,CP_Value,CP_Sum,Plyr_Value,Plyr_Sum,sel;
    
    system("cls");
    
    do{ 
    system("cls");
    printf("\n\t\tDice Roll Game");
    printf("\n\t1.Play");
    printf("\n\t2.Exit");
    printf("\nSelect>>: ");
    fflush(stdin);
    scanf("%d",&sel);
    printf("\n\n\n");
    
    switch(sel){
        case 1: printf("Computer dice generated: ");
                Comp_Dice(CP_Value,CP_Sum);
    
                printf("\nPlayer dice has been generated please Input integer seed: ");
                scanf("%d",&x);
    
                Plyr_Dice(Plyr_Value,x);
                if (Plyr_Sum>CP_Sum){
                printf("\nPlayer 2 Wins!\n");
                }else printf("\nPlayer 1 Wins!\n");
                break;
        case 2: printf("Thank you for playing");
                break;
        default: printf("invalid input please choose 1 or 2\n");
        }
        system("pause");
    }while(sel!=2);
return 0;   
}

解决方法

您对功能参数的管理错误。如果您希望函数修改Plyr_SumCP_Sum,则不能为整数。

函数原型应为:

  • void Comp_Dice(int CP_Value,int *CP_Sum),然后别忘了初始化CP_Sum

  • int Comp_Dice(int CP_Value),只需返回总和即可。

通过这种方式,您应该使用无符号变量。

,

如果您解决了打开警告时编译器提示的问题,那么您的代码将更接近正确运行。

这是我的编译器显示的列表:

Build Status (prog.prj - Debug)
 main29.c - 6 warnings
  16,1    warning: function does not return a value 
  29,1    warning: function does not return a value 
  49,27    warning: variable 'CP_Value' may be uninitialized when used here 
      33,20    note: initialize the variable 'CP_Value' to silence this warning
  49,37    warning: variable 'CP_Sum' may be uninitialized when used here 
      33,28    note: initialize the variable 'CP_Sum' to silence this warning
  54,27    warning: variable 'Plyr_Value' may be uninitialized when used here 
      33,40    note: initialize the variable 'Plyr_Value' to silence this warning
  54,39    warning: variable 'Plyr_Sum' may be uninitialized when used here 
      33,50    note: initialize the variable 'Plyr_Sum' to silence this warning
 Link play.exe
 "c:\play\prog.exe" successfully created.
Build succeeded.

其他一些建议:

根据C标准,它是undefined behavior to use fflush(stdin)

srand()放在main(){的开头,并且在执行期间仅调用一次。 More on proper use of srand() here` here

要更改在任何函数中作为参数传递的变量的值,传递的不是变量本身,而是该变量的地址

Comp_Dice的原型更改为:

Comp_Dice(int CP_Value,int *CP_Sum){

然后按如下所示进行调用:

Comp_Dice(CP_Value,&CP_Sum);//& is the `address of` operator  

Plyr_Dice的原型更改为:

void Plyr_Dice(int Plyr_Value,int Plyr_Sum,int x);
^^^^  

Plyr_Dice

在您第一次致电Comp_Dice时,CP_Value未初始化。其他几个变量也是如此。 Plyr_ValuePlyr_Sum,...初始化所有它们,即使您认为它们将在首次使用时被分配值。初始化是个好习惯。

编辑 解决代码注释中的特定问题。

以下是您的代码经过修改才能运行。我已注释掉一些在我的系统上不起作用的事情,而我发现的事情是错误的。它可能需要调试,但是应该运行。

//Computer Dice Roll
void Comp_Dice(int CP_Value,int *CP_Sum){
    srand(time(0));
    for(int i = 0; i <10; i++){
    CP_Value = rand()%6+1;
    printf("%d ",CP_Value);
    *CP_Sum += CP_Value;

    }
    printf("\nPlayer 1 Total: %d",*CP_Sum); 
}
//End of Computer Dice Roll

//Player Data
void Plyr_Dice(int Plyr_Value,int *Plyr_Sum,int x){
    srand(x);
    for(int e = 0; e <10; e++){
    Plyr_Value = rand()%6+1;
    printf("%d ",Plyr_Value);
    *Plyr_Sum +=Plyr_Value;
    
    }
    printf("\nPlayer 2 Total: %d",*Plyr_Sum);
}
//End of Player Data

int main(void){
    int x,CP_Value=0,CP_Sum,Plyr_Value=0,Plyr_Sum,sel;
    
    //system("cls");//okay,but does not work on my system.
    
    do{ 
    //system("cls");
    printf("\n\t\tDice Roll Game");
    printf("\n\t1.Play");
    printf("\n\t2.Exit");
    printf("\nSelect>>: ");
    //fflush(stdin);// undefined behavior
    scanf("%d",&sel);
    printf("\n\n\n");
    
    switch(sel){
        case 1: printf("Computer dice generated: ");
                Comp_Dice(CP_Value,&CP_Sum);
    
                printf("\nPlayer dice has been generated please Input integer seed: ");
                scanf("%d",&x);
    
                Plyr_Dice(Plyr_Value,&Plyr_Sum,x);
                if (Plyr_Sum>CP_Sum){
                printf("\nPlayer 2 Wins!\n");
                }else printf("\nPlayer 1 Wins!\n");
                break;
        case 2: printf("Thank you for playing");
                break;
        default: printf("invalid input please choose 1 or 2\n");
        }

        //system("pause");//okay,but does not work on my system.
    }while(sel!=2);
    return 0;   
}