mercredi 12 août 2020

How to calculate the sum of 2 sets created by two group datas from a random number generator?

I am trying to make a programming assignment in my school that requires me to create a dice game in C where the winner is determined based on which sum of 10 dice rolls (between the computer and the player) is higher. However I seem to have encountered a problem where my program would add the sum of the computer dice rolls to that of the user causing a guaranteed win to the user. The program must make use of the srand and rand function and I currently have no idea how to fix this due to this being my entry into programming. Below is the code I have made as I do not how to properly explain this and I have no clue the error could be. Thank you very much for any possible help as I cannot contact my professor at the moment

#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, 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");
    }while(sel!=2);
return 0;   
}



Aucun commentaire:

Enregistrer un commentaire