// This program tracks scores of players whilst they play a dice game // The player with the highest score wins // Learning to solve problems with arrays // Sasha Vassar, Week 3 Lecture 6 #include #define N_PLAYERS 4 struct dice{ int die_one; int die_two; }; // PROBLEM: Four players are playing a dice game. For every round of the game, // each player rolls two dice, and the sum of those dice is their final score // for that round. After everyone has rolled their dice in the game, we want // to be able to find out who won that game (highest score), and we also want // to know what the total of the scores is in any given round. // // Solution Steps: // 1. Four players always - does not change use define // 2. Roll the dice, what was their dice? - use scanf? // 2.1 Let's use a struct to group the two dice together from each player // 3. Sum the dice // 4. When we finish rolling one turn each, find out who wins with highest scores // - if? // - keep track of which player wins // 5. Total score - add up all the player scores void print_scores (struct dice roll[]); int main (void) { // Declare an array of structs // int parcel_week[] struct dice roll[N_PLAYERS] = {0}; // Roll the dice - that ahppens outside of the program // Scan in the numbers int i = 0; while (i < N_PLAYERS) { printf("Please enter Player %d's first die: ", i + 1); scanf("%d", &roll[i].die_one); printf("Please enter Player %d's second die: ", i + 1); scanf("%d", &roll[i].die_two); i++; } print_scores(roll); int sum_dice[N_PLAYERS] = {0}; i = 0; while (i < N_PLAYERS) { sum_dice[i] = roll[i].die_one + roll[i].die_two; i++; } //loop through sum_dice array and find the highest score i = 0; while (i < N_PLAYERS) { i++; } return 0; } // Function: OUTPUT TYPE name_of_function (INPUT) void print_scores (struct dice roll[]) { int i = 0; while (i < N_PLAYERS) { printf("Player %d rolled a %d %d\n", i + 1, roll[i].die_one, roll[i].die_two); i++; } }