# Create a Program to Implement Sorting in C++ Assignment Solution

June 18, 2024
Harry F.
C++
Harry F. Grimmett, with a master’s in computer science from the University of Kent, is a C++ homework helper, boasting six years of field experience.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Sorting algorithms are fundamental to programming and mastering them is essential. You've got this! Remember to break down the problem into smaller steps, choose an appropriate sorting method, and test your code thoroughly. Completing a C++ assignment like this will not only enhance your problem-solving skills but also deepen your understanding of the language.

## Requirements and Specifications

Source Code

``` // libraries #include "yahtzee.h" enum scores { one, two, three, four, five, six, threekind, fourkind, fullhouse, smstraight, lgstraight, yahtzee, chance }; // main function int main() { // while loop control variable int play = 1; // seed the random function srand((unsigned int)time(NULL)); // continue to loop until the player quits or the game ends while (play) { switch (displayGameMenu()) { case RULES: // call function gameRules gameRules(); break; case GAME: // call function clearScreen() clearScreen(); // call function displayRandomDice // displayRandomDice(); // call function play playGame(); break; case EXIT: printf("Thank you for playing!\n"); play = 0; break; default: printf("Incorrect option, hit enter and try again\n"); char enter; scanf("%c", &enter); break; } } // program executed successfully return 0; } // gameRules function displays the Yahtzee and rules of the game void gameRules() { printf("\t\t\t\tLET'S PLAY YAHTZEE!!! \n\n"); printf("RULES OF THE GAME:\n"); printf("\t1. The scorecard used for Yahtzee is composed of an upper section and a lower section.\n"); printf("\t2. A total of 13 scoring combinations are divided amongst the sections. \n"); printf("\t3. The upper section consists of boxes that are scored by summing the value of the dice matching the faces of the box.\n"); printf("\t4. If a player rolls four 3's, then the score placed in the 3's box is the sum of the dice which is 12. \n"); printf("\t5. Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for future rounds.\n"); printf("\t6. If the sum of the scores in the upper section is greater than or equal to 63, then 35 more points are added \n"); printf("\tto the players overall score as a bonus. The lower section contains a number of poker like combinations.\n"); } // function displayExplicitDice displays a hardcoded version of a rolled dice void displayExplicitDice() { printf("|---------------------------------------|\n"); printf("| | | | | |\n"); printf("| 1 | 2 | 3 | 4 | 5 |\n"); printf("| | | | | |\n"); printf("|---------------------------------------|\n"); } // function displayRandomDice displays a randomly generated dice for three rolls void displayRandomDice() { // looping variable int roll; int die1; int die2; int die3; int die4; int die5; for (roll = 0; roll < ROLLS; roll++) { die1 = rollDie(); die2 = rollDie(); die3 = rollDie(); die4 = rollDie(); die5 = rollDie(); printf("|---------------------------------------|\n"); printf("| | | | | |\n"); printf("| %d | %d | %d | %d | %d |\n", die1, die2, die3, die4, die5); printf("| | | | | |\n"); printf("|---------------------------------------|\n"); } } // clears the screen void clearScreen() { printf("\n\t\t\t\tHit to continue!\n"); char enter; scanf("%c", &enter); // send the clear screen command Windows system("cls"); // send the clear screen command for UNIX flavor operating systems // system("clear"); } // presents a menu to the player int displayGameMenu() { int select = 0; do { printf("\n\n\t\t\t\tLET'S PLAY YAHTZEE!!! \n\n"); printf("%d. Display Game Rules\n", RULES); printf("%d. Start a game of Yahtzee\n", GAME); printf("%d. Exit\n", EXIT); scanf("%d", &select); } while ((select < RULES) && (select > EXIT)); return select; } // randomly rolls a die int rollDie() { int dieValue = 0; dieValue = rand() % 6 + 1; return dieValue; } void initializeDice(int dice[DICE]) { int die; for (die = 0; die < DICE; die++) { dice[die] = 0; } } void playGame() { int roll; int turn; int dice[DICE]; int keep[DICE]; int scoreCard[13][2]; int category; initializeScores(scoreCard); // continue to loop until game is over for (turn = 0; turn < TURNS; turn++) { int current = turn + 1; printf("Turn %d of game\n\n", current); // for each turn reset the keep[] and dice arrays initializeDice(dice); resetKeep(keep); // one turn for (roll = 0; roll < ROLLS; roll++) { printf("Rolling the dice...\n"); rollDice(dice, keep); displayDice(dice); if (roll < 2) selectDice(dice, keep); } category = selectCategory(); updateScores(scoreCard, category, dice); displayScoreCard(scoreCard); } } int displayDice(int dice[DICE]) { int die; die = 0; char enter; printf("+-------+ +-------+ +-------+ +-------+ +-------+\n"); printf("| | | | | | | | | |\n"); for (die = 0; die < DICE; die++) { printf("| %d | ", dice[die]); } printf("\n"); printf("| | | | | | | | | |\n"); printf("+-------+ +-------+ +-------+ +-------+ +-------+\n"); return scanf("%c", &enter); } void resetKeep(int keep[DICE]) { int die; // loop through the five dice for (die = 0; die < DICE; die++) { // set each die to 0 meaning false keep[die] = 0; } } void rollDice(int dice[DICE], int keep[DICE]) { int die; // loop through the five dice for (die = 0; die < DICE; die++) { // only roll dice that have not been selected to keep if (keep[die] == 0) { dice[die] = rollDie(); } } } void initializeScores(int scoreCard[13][2]) { int row; int col; for (row = 0; row < 13; row++) for (col = 0; col < 2; col++) scoreCard[row][col] = 0; } int selectCategory() //Options for player to choose the category for their dice { int select; select = 0; printf(" Select category for dice\n"); printf("1. Ones\n"); printf("2. Twos\n"); printf("3. Threes\n"); printf("4. Fours\n"); printf("5. Fives\n"); printf("6. Sixes\n"); printf("7. Three of a kind\n"); printf("8. Four of a kind\n"); printf("9. Full house\n"); printf("10. Small straight\n"); printf("11. Large straight\n"); printf("12. Yahtzee\n"); printf("13. Chance\n"); scanf("%d", &select); return select; } void selectDice(int dice[DICE], int keep[DICE]) //Screen selection for player to pick desired dice to keep { int die = 0; char data[20]; char *value; int valid = 0; resetKeep(keep); printf("Select dice to keep, enter values 1 through 5 with spaces between numbers\n"); fgets(data, 10, stdin); value = strtok(data, " "); while (value != NULL) { valid = 0; while (!valid) { switch (*value) { case '1': keep[0] = 1; valid = 1; break; case '2': keep[1] = 1; valid = 1; break; case '3': keep[2] = 1; valid = 1; break; case '4': keep[3] = 1; valid = 1; break; case '5': keep[4] = 1; valid = 1; break; default: /* ignores invalid characters */ continue; } } value = strtok(NULL, " "); } } void updateScores(int scoreCard[13][2], int category, int dice[DICE]) { switch (category) { case ONE: printf("Scoring Ones...\n"); scoreCard[one][COL] = sumCategory(dice, category); break; case TWO: printf("Scoring Twos...\n"); scoreCard[two][COL] = sumCategory(dice, category); break; case THREE: printf("Scoring Threes...\n"); scoreCard[three][COL] = sumCategory(dice, category); break; case FOUR: printf("Scoring Fours...\n"); scoreCard[four][COL] = sumCategory(dice, category); break; case FIVE: printf("Scoring Fives...\n"); scoreCard[five][COL] = sumCategory(dice, category); break; case SIX: printf("Scoring Sixes...\n"); scoreCard[six][COL] = sumCategory(dice, category); break; case THREEKIND: printf("Scoring Three of a kind...\n"); scoreCard[threekind][COL] = checkThreeKind(dice); break; case FOURKIND: printf("Scoring Four of a kind...\n"); scoreCard[fourkind][COL] = checkFourKind(dice); break; case FULLHOUSE: printf("Scoring Full house...\n"); scoreCard[fullhouse][COL] = checkFullHouse(dice); break; case SMSTRAIGHT: printf("Scoring Small straight...\n"); scoreCard[smstraight][COL] = checkSmStraight(dice); break; case LGSTRAIGHT: printf("Scoring Large straight...\n"); scoreCard[lgstraight][COL] = checkLgStraight(dice); break; case YAHTZEE: printf("Scoring Yahtzee...\n"); scoreCard[yahtzee][COL] = checkYahtzee(dice); break; case CHANCE: printf("Scoring Chance...\n"); scoreCard[chance][COL] = sumChance(dice); break; default: break; } } void displayScoreCard(int scoreCard[13][2]) { int total; int grandTotal; total = sumUpper(scoreCard) + checkBonus(scoreCard); grandTotal = total + sumLower(scoreCard); printf(" YAHTZEE SCORECARD \n"); printf("+-----------------+-----------+\n"); displayUpperSection(scoreCard); displayLowerSection(scoreCard); printf("| TOTAL (upper) | %d |\n", total); printf("+-----------------+-----------+\n"); printf("| GRAND TOTAL | %d |\n", grandTotal); printf("+-----------------+-----------+\n"); } void displayUpperSection(int scoreCard[13][2]) //displays the upper section of the game score { int totalScore; int bonus; int total; totalScore = sumUpper(scoreCard); bonus = checkBonus(scoreCard); total = bonus + totalScore; printf("| UPPER SECTION | SCORE |\n"); printf("+-----------------+-----------+\n"); printf("| ONES | %d |\n", scoreCard[one][COL]); printf("+-----------------+-----------+\n"); printf("| TWOS | %d |\n", scoreCard[two][COL]); printf("+-----------------+-----------+\n"); printf("| THREES | %d |\n", scoreCard[three][COL]); printf("+-----------------+-----------+\n"); printf("| FOURS | %d |\n", scoreCard[four][COL]); printf("+-----------------+-----------+\n"); printf("| FIVES | %d |\n", scoreCard[five][COL]); printf("+-----------------+-----------+\n"); printf("| SIXES | %d |\n", scoreCard[six][COL]); printf("+-----------------+-----------+\n"); printf("| TOTAL SCORE | %d |\n", totalScore); printf("+-----------------+-----------+\n"); printf("| BONUS | %d |\n", bonus); printf("+-----------------+-----------+\n"); printf("| SCORE | %d |\n", total); printf("+-----------------+-----------+\n"); } void displayLowerSection(int scoreCard[13][2]) //will display the lower section of the game score { int total; total = sumLower(scoreCard); printf("+-----------------+-----------+\n"); printf("| LOWER SECTION | SCORE |\n"); printf("+-----------------+-----------+\n"); printf("| THREE OF A KIND | %d |\n", scoreCard[threekind][COL]); printf("+-----------------+-----------+\n"); printf("| FOUR OF A KIND | %d |\n", scoreCard[fourkind][COL]); printf("+-----------------+-----------+\n"); printf("| FULL HOUSE | %d |\n", scoreCard[fullhouse][COL]); printf("+-----------------+-----------+\n"); printf("| SM STRAIGHT | %d |\n", scoreCard[smstraight][COL]); printf("+-----------------+-----------+\n"); printf("| LG STRAIGHT | %d |\n", scoreCard[lgstraight][COL]); printf("+-----------------+-----------+\n"); printf("| YAHTZEE | %d |\n", scoreCard[yahtzee][COL]); printf("+-----------------+-----------+\n"); printf("| CHANCE | %d |\n", scoreCard[chance][COL]); printf("+-----------------+-----------+\n"); printf("| TOTAL (lower) | %d |\n", total); printf("+-----------------+-----------+\n"); } int sumCategory(int dice[DICE], int category) { int sum; int die; sum = 0; for (die = 0; die < DICE; die++) if (dice[die] == category) sum += dice[die]; return sum; } int sumChance(int dice[DICE]) { int sum; int die; sum = 0; for (die = 0; die < DICE; die++) sum += dice[die]; return sum; } int checkLgStraight(int dice[DICE]) { int sum; sum = 0; sortDice(dice); if (dice[0] == 1 && dice[1] == 2 && dice[2] == 3 && dice[3] == 4 && dice[4] == 5) sum = 40; else if (dice[0] == 2 && dice[1] == 3 && dice[2] == 4 && dice[3] == 5 && dice[4] == 6) sum = 40; return sum; } int checkSmStraight(int dice[DICE]) { int sum; sum = 0; sortDice(dice); if (dice[0] == 1 && dice[1] == 2 && dice[2] == 3 && dice[3] == 4) sum = 30; else if (dice[0] == 2 && dice[1] == 3 && dice[2] == 4 && dice[3] == 5) sum = 30; else if (dice[0] == 3 && dice[1] == 4 && dice[2] == 5 && dice[3] == 6) sum = 30; if (dice[1] == 1 && dice[2] == 2 && dice[3] == 3 && dice[4] == 4) sum = 30; else if (dice[1] == 2 && dice[2] == 3 && dice[3] == 4 && dice[4] == 5) sum = 30; else if (dice[1] == 3 && dice[2] == 4 && dice[3] == 5 && dice[4] == 6) sum = 30; return sum; } int checkYahtzee(int dice[DICE]) { int sum; sum = 0; if (dice[0] == dice[1] && dice[1] == dice[2] && dice[2] == dice[3] && dice[3] == dice[4]) sum = 50; return sum; } int checkFourKind(int dice[DICE]) { int sum; sum = 0; sortDice(dice); if (dice[0] == dice[1] && dice[1] == dice[2] && dice[2] == dice[3]) sum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4]; else if (dice[1] == dice[2] && dice[2] == dice[3] && dice[3] == dice[4]) sum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4]; return sum; } int checkThreeKind(int dice[DICE]) { int sum; sum = 0; sortDice(dice); if (dice[0] == dice[1] && dice[1] == dice[2]) sum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4]; else if (dice[1] == dice[2] && dice[2] == dice[3]) sum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4]; else if (dice[2] == dice[3] && dice[3] == dice[4]) sum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4]; return sum; } int checkFullHouse(int dice[DICE]) { int sum; sum = 0; sortDice(dice); if (dice[0] == dice[1] && dice[1] == dice[2] && dice[3] == dice[4]) sum = 25; else if (dice[0] == dice[1] && dice[2] == dice[3] && dice[3] == dice[4]) sum = 25; return sum; } int sumUpper(int scoreCard[CATEGORIES][COLS]) { int sum; sum = 0; for (int i = 0; i < SIX; i++) sum += scoreCard[i][COL]; return sum; } int sumLower(int scoreCard[CATEGORIES][COLS]) { int sum; sum = 0; for (int i = SIX; i < CHANCE; i++) sum += scoreCard[i][COL]; return sum; } int checkBonus(int scoreCard[CATEGORIES][COLS]) { int sum; int score; sum = 0; score = sumUpper(scoreCard); if (score >= 63) sum = 35; return sum; } void sortDice(int dice[DICE]) { int temp; int outer; int inner; for (outer = 0; outer < DICE - 1; outer++) { for (inner = outer + 1; inner < DICE; inner++) { if (dice[outer] > dice[inner]) { temp = dice[outer]; dice[outer] = dice[inner]; dice[inner] = temp; } } } } ```

## Similar Samples

Browse through our curated selection of programming homework samples at ProgrammingHomeworkHelp.com. Our examples cover a wide range of programming languages and concepts, showcasing our proficiency and dedication to delivering high-quality solutions. These samples serve as a testament to our expertise in assisting students with their programming assignments. Explore them to see how we can help you excel in your programming studies.