Thread: C programming question.

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    2

    Exclamation C programming question.

    I hope I can have some help on this question:

    You are to write a C program that would roll from 1 to 3 dices for 1 to 5 players of

    board games like snakes and ladders and Monopoly.The program would give you the option to choose to have from 1 to 5 players (prompt
    players for their names) and you should also be able to choose to roll between 1, 2,
    or 3 dices. For example, if the option of 3 dices were chosen, the roll would display
    the count of dice 1, 2, and 3 individually PLUS the sum of all 3 the dices. E.g. say,
    the roll of Player Adriano is 2, 6, 4 as shown in the picture on the top right corner of
    this page, the roll should be displayed as below:
    Player Adriano Roll 1: 2, 6, 4  12
    Where 12 is the sum of the roll (2+6+4).
    You are to save the dice roll in a text file (a .txt file) as recording of the dice roll. The
    text file would also record some simple statistics of the rolls for each individual
    player, e.g. most frequently number rolled, average roll, and the percentage of roll.
    Eg. In 10 rolls, player Adriano, most frequently rolled 6 on 2 dices, average roll is
    6.3, and roll 2 2% of the time, 3 4% of the time, 4 7% of the time … and 12 2% of the
    time on 2 dices. The computation should only be done when the user choose to quit
    the program, as that is also the only time you have enough data to compute the
    statistics and store it in the text file.
    Consider also the common board games rule:-
    1. If 1 dice is used, the player can roll again if he/she rolled a 6 (six), up to 3
    times.
    2. If 2 dices were used, the player can roll again if he/she rolled a double (up to
    3 times).
    3. If 3 dices were used, the player can roll again if he/she rolled a triple (up to 3
    Attached Images Attached Images

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What is the question? You cant just dump a text and solution and expect us to do everything for you.
    learn to ask smart question and in return you get smart answers.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    2
    I am sry, this is my first time enter this forum. The attachment is my solution but seems there are some errors in it. I really have no ideas what mistakes I made in the solution... Hope someone give me a hint .... I am sorry.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post your code here on the forum, using code tags. And post any warnings and/or errors you are receiving.

    Also tell us exactly what is not working as expected. For instance, if you run the program and the output is wrong, give us the input that induces the problem, the expected output, and the actual output.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Here is the code.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<time.h>
    
    int diceroll();//function declaration
    void display_welcome();
    
    struct {
        char name[50];
        float freq[6];
        float average;
        float total;
        float percent[18];
        float sum[15];
        int freq1[6];
        int highest;
        int roll[3];
        int sum1;
        int count;
    } player[5];
    
    int main()
    {
        FILE *fp;//to create .txt file
        int a, b, c, num = 0, dice = 0, max, game=0;
        int count = 0;
        char stop = 'f';
        srand(time(NULL));
        system("clear");// to clear the screen
        display_welcome();
        if (num=1 || num<6){
            printf("\nEnter the number of players\n");
            scanf("%d",&num);
        }getchar();
        for ( a=1; a<=num; a++) {
            printf("\nPlease enter the name of Player %d : \n",a+0);
            player[a].count=0;
            gets(player[a].name);
        }
        system("clear");
        if (dice=0 || dice > 3);
        {
            printf("Number of dices want to be used : ");
            scanf("%d ",&dice);
        }
        system("clear");
        for (a=0;a<6;a++) {
            for (c=0;c<18;c++) {
                player[a].sum[c]=0;
            }
        }
        while ( stop!='f') {
            count++;
            if ( dice==1 ) {
                for ( a=0; a<num; a++) {
                    max = 1;
                    printf("Player %d : %15s : \n",a+1,player[a].name);
                    do {
                        player[a].roll[0] = diceroll();
                        if ( player[a].roll[0]==6 && max<3 ) {
                            printf("\n\t\t\t\t\tYou rolled a 6.\n");
                        } else
                            switch (player[a].roll[0]) {case 1: player[a].freq[0]++;
                                break;
                                case 2: player[a].freq[1]++;
                                break;
                                case 3: player[a].freq[2]++;
                                break;
                                case 4: player[a].freq[3]++;
                                break;
                                case 5: player[a].freq[4]++;
                                break;
                                case 6: player[a].freq[5]++;
                                break;
                            }
                        max++;
                    } while ( player[a].roll[0]==6 && max<4 );
                    player[a].sum1 = player[a].roll[0];
                    for (c=0;c<6;c++) {
                        if (player[a].sum1== c+1) {
                            player[a].sum[c]++;
                        }
                    }
                    printf("\n\t\t\t\t\tRoll %d : %d --> sum: %d\n",count,player[a].roll[0],player[a].sum1);
                }
            }
            if ( dice==2 ) {
                for ( a=0; a<num; a++ ) {
                    max = 1;
                    printf("Player %d : %15s :\n",a+1,player[a].name);
                    do {
                        player[a].roll[0]=diceroll();
                        player[a].roll[1]=diceroll();if ( player[a].roll[0]==player[a].roll[1] && max<3 ) {
                            printf("\n\t\t\t\t\tYou rolled a double: %d %d\n",player[a].roll[a],player[a].roll[a]);
                        }
                        else for ( b=0; b<2; b++ ) {
                            switch (player[a].roll[b]) {
                                case 1: player[a].freq[0]++;
                                break;
                                case 2: player[a].freq[1]++;
                                break;
                                case 3: player[a].freq[2]++;
                                break;
                                case 4: player[a].freq[3]++;
                                break;
                                case 5: player[a].freq[4]++;
                                break;
                                case 6: player[a].freq[5]++;
                                break;
                            }
                        }
                        max++;
                    } while ( player[a].roll[0]==player[a].roll[1] && max<4 );
                    player[a].sum1 = player[a].roll[0] + player[a].roll[1];
                    for (c=0;c<12;c++) {
                        if (player[a].sum1 == c+1) {
                            player[a].sum[c]++;
                        }
                    }
                    printf("\n\t\t\t\t\tRoll %d : %d %d --> sum: %d\n",count,player[a].roll[0],player[a].roll[1],player[a].sum1);
                }
            }
            if ( dice==3 ) {for ( a=0; a<num; a++ ) {
                max = 1;
                printf("Player %d : %15s :\n",a+1,player[a].name);
                do {
                    player[a].roll[0] = diceroll();
                    player[a].roll[1] = diceroll();
                    player[a].roll[2] = diceroll();
                    if (player[a].roll[0]==player[a].roll[1] && player[a].roll[1]==player[a].roll[2] && max<3) {
                        printf("\n\t\t\t\t\tYou rolled a triple: %d %d %d\n",player[a].roll[0],player[a].roll[1],player[a].roll[2]);
                    }
                    else for( b=0; b<3; b++ ) {
                        switch (player[a].roll[b]) {
                            case 1: player[a].freq[0]++;
                            break;
                            case 2: player[a].freq[1]++;
                            break;
                            case 3: player[a].freq[2]++;
                            break;
                            case 4: player[a].freq[3]++;
                            break;
                            case 5: player[a].freq[4]++;
                            break;
                            case 6: player[a].freq[5]++;
                            break;
                        }
                    }
                    max++;
                } while ( player[a].roll[0]==player[a].roll[1] && player[a].roll[1]==player[a].roll[2] && max<4 );
                player[a].sum1 = player[a].roll[0] + player[a].roll[1] + player[a].roll[2];
                for (c=0;c<18;c++) {
                    if (player[a].sum1 == c+1) {
                        player[a].sum[c]++;
                    }
                }
                printf("\n\t\t\t\t\tRoll %d : %d %d %d --> sum: %d\n",count,player[a].roll[0],player[a].roll[1],player[a].roll[2],player[a].sum1);
            }
            }
            for ( a=0; a<num; a++ ) {
                player[a].total = player[a].total + player[a].sum1;
            }
            getchar();
            printf("\n\n\nEnter any character to continue playing, 'f' to end game > ");
            scanf("%c",&stop);
            system("clear");
    
    /*
            for( a=0; a<num; a++) {
                printf("Player %d : %s\n",a+1,player[a].name);
                player[a].highest=0;
                for( b=0; b<6; b++) {
    //
                    printf("Dice %d : %.0f \n",b+1,player[a].freq[b]);
                    if ( player[a].freq[b] >= player[a].highest ) {
                        player[a].mostfreq[b] = b+1;
                        if ( player[a].freq[b] > player[a].highest ) {
                            player[a].highest = player[a].freq[b];
                        } else player[a].highest = player[a].freq[b];
                    }
                }
                printf("\nIn %d rolls, Player %s most frequently roll",count,player[a].name);
                for ( b=0; b<6; b++ ) {if (player[a].freq[b]==player[a].highest) {
                    printf(" '%d' ",player[a].mostfreq[b]);
                }
            }
            printf(" on %d dices",player[a].highest);
            player[a].avg = player[a].total/count;
            printf("\n\tAverage roll : %.2f\n",player[a].avg);
            for( c=0; c<18; c++ ) {
                player[a].per[c] = (player[a].sum[c]/count)*100;
                printf("\t\tRoll %d : \t %.2f%% of the time on %.0f dices\n",x+1,player[a].per[c],player[a].sum[c]);
            }
    */
    
            do {
                fp=fopen("a.txt","a");
            } while (fp==NULL);
            game++;
            fprintf(fp,"Game %d :\n\n",game);
            for( a=0; a<num; a++) {
                fprintf(fp,"\nPlayer %d : %s\n",a+1,player[a].name);
                player[a].highest=0;
                for( b=0; b<6; b++) {
                    if ( player[a].freq[b] >= player[a].highest ) {
                        player[a].freq[b] = b+1;
                        if ( player[a].freq[b] > player[a].highest ) {
                            player[a].highest = player[a].freq[b];
                        } else player[a].highest = player[a].freq[b];}
                }
                fprintf(fp,"\nIn %d rolls, Player %s most frequently roll",count,player[a].name);
                for ( b=0; b<6; b++ ) {
                    if (player[a].freq[b]==player[a].highest) {
                        fprintf(fp," '%d' ",player[a].freq1[b]);
                        (fp," on %d dices",player[a].highest);
                        player[a].average = player[a].total/count;
                        fprintf(fp,"\n\tAverage roll : %.2f\n",player[a].average);
                        for( c=0; c<18; c++ ) {
                            player[a].percent[c] = (player[a].sum[c]/count)*100;
                            fprintf(fp,"\t\tRoll %d : \t %.2f%% of the time on %.0f dices\n",c+1,player[a].percent[c],player[a].sum[c]);
                        }
                        printf("\n\n\n");
                    }
                }
            }
        }
        return 0;
    }
    
    int diceroll()//function call
    {
        int result;
        result = ((int) rand()%6) + 1;
        return (result);
    }
    
    void display_welcome(){
        printf("Welcome!!!");
        printf("\n\t\Dice Game of");
        printf("\n\n\t\t\t\t\Naren & Cx");return;
    }
    I have only cuted out from PDF-file and reformated the code.
    I hope the spaces are not lost.
    Other have classes, we are class

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Excellent! Now what's the question? You aren't going to get an answer without a question sadly :/
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Indeed, a question would be helpful.

    At a glance, a few things stuck out. See #4 and #6 here: 8 Common Programming Mistakes - Cprogramming.com

    Then read this for a helpful technique on reducing errors as you develop your program: A development process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Question
    By Buddha in forum C Programming
    Replies: 11
    Last Post: 05-17-2010, 03:02 PM
  2. Programming question
    By cagayanamerican in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2005, 04:39 PM
  3. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM
  4. UIL Programming Question
    By araknid in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2002, 08:11 PM

Tags for this Thread