Leonardo Olvera
Leonardo Olvera

Reputation: 67

Most Efficient Way to Add a Score?

I'm still trying to make the dice game, and I just need to know what's the best way to add a score to my game so I can print out a clear winner. Would I need a bunch of if-else statements, or would it be easier to create a header file?

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

void clean_stdin(void) {
int c;
do {
    c = getchar();
} while (c != '\n' && c != EOF);
}


int main() {

int i, r, diceRoll;
char player1[20];
int score1[5];
int sum;
srand(time(NULL));

printf("\t\t\t Welcome to Farkle Lite!\n");
printf(" Highest score wins!\n\n");
printf(" The rules:\n");
printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n");
printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n");
printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n");
printf("Enter name for Player 1:\n");
fgets (player1, 20, stdin);
clean_stdin();

printf("\n\n %s Dice Roll:\n\n", player1);

for(i = 0; i < 6; i ++){
     r = (rand()%6)+1;
    diceRoll= r;
    score1[i]= diceRoll;
    sum = score[i];
    printf(" test %d \n", score1[i]);
}
printf(" %d", score1[i]);
return 0;
}

Upvotes: 0

Views: 93

Answers (1)

Chris
Chris

Reputation: 2763

Here is a good opportunity to use a function. Each player will role the die six times, each player will have their scored totaled, and each player will be able to enter their name. We can create a function to centralize this behavior. To do this we need to store the player names and player scores in arrays so that the second time we run the function we do not overwrite the information from the first run. Below is an example:

void play (int playerNo, char* name, int* score)
{
    int loop;
    int role;

    printf("Enter name for Player %d:\n", playerNo);
    fgets (name, 20, stdin);
    clean_stdin();

    printf("\n\n %s Dice Roll:\n\n", name);

    *score = 0;
    for(loop = 0; loop<6; loop++)
    {
        role = (rand()%6)+1;
        *score += role;
        printf (" %d\n", role);
    }
}

This function will allow user to enter name and will then role the die 6 times, totaling the score. Upon exit, the appropriate entries in the playerNames and playerScores arrays will be updated.

Having got this we just just need to loop through the players, calling this function for each player:

int main() 
{
    const int   playerCount = 2;
    char        playerNames [playerCount][20];
    int         playerScores[playerCount];
    int         loop;
    int         highestScore = 0;
    int         highestPlayer;

    // Seed rng
    srand(time(NULL));

    // Instructions
    printf("\t\t\t Welcome to Farkle Lite!\n");
    printf(" Highest score wins!\n\n");
    printf(" The rules:\n");
    printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n");
    printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n");
    printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n");

    // Let each player play the game
    for (loop=0; loop<playerCount; loop++)
        play (loop+1, &playerNames[loop][0], &playerScores[loop]);

    // Tally scores
    for (loop=0; loop<playerCount; loop++)
    {
        if (playerScores[loop] > highestScore)
        {
            highestScore = playerScores[loop];
            highestPlayer = loop;
        }
    }

    // Display winner
    printf ("Player %d: %s wins with a scrore of %d", 
        highestPlayer+1,
        playerNames[highestPlayer],
        playerScores[highestPlayer]);

    return 0;
}

Afterwards we can go through all the scores to record which is the highest and then display the winner.

Upvotes: 1

Related Questions