PS I dont know how to calculate the score and how to put it
(P/S : I don\'t know how to calculate the score and how to put it inside the table, I include code as well )
For this assignment, you will be implementing the classic dice game of Yahtzee. For those who are not familiar with it, Yahtzee is a game played with five dice. On a players turn, they roll their dice, and then have up to two additional rolls in which they may reroll any number of the dice. After the rerolls, the player chooses the category in which to score the roll.
Dice categories The thirteen categories of dice configurations and their scores are: 1. Ones. Any dice configuration is valid for this category. The score is equal to the sum of all of the 1’s showing on the dice, which is 0 if there are no 1’s showing.
2–6. Twos, Threes, Fours, Fives, and Sixes. (Similar to above but for different values). Any dice configuration is valid for these categories. The score is equal to the sum of the 2’s, 3’s, 4’s, 5’s, or 6’s, showing on the dice.
7. Three of a Kind. At least three of the dice must show the same value. The score is equal to the sum of all of the values showing on the dice.
8. Four of a Kind. At least four of the dice must show the same value. The score is equal to the sum of all of the values showing on the dice.
9. Full House. The dice must show three of one value and two of another value (Special case: a five-ofa-kind also counts as a full house). The score is 25 points.
10. Small Straight. The dice must contain at least four consecutive values, such as the sequence 2-3-4- 5. The score is 30 points.
11. Large Straight. The dice must contain five consecutive values, such as the sequence 1-2-3-4-5. The score is 40 points.
12. Yahtzee! All of the dice must show the same value. The score is 50 points. 13. Chance. Any dice configuration is valid for this category. The score is equal to the sum of the values showing on the dice.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
void printRoll(int n1, int n2, int n3, int n4, int n5);
bool askReroll(int n);
void printSeparator();
void printScore(int onesScore, int twosScore, int threesScore, int foursScore,
int fivesScore, int sixesScore, int threeOfAKind,
int fourOfAKind, int fullHouse, int smallStraight,
int largeStraight, int yahtzee, int chance);
void printScoreLine(string name, int score);
int getScoreOption(int onesScore, int twosScore, int threesScore, int foursScore,
int fivesScore, int sixesScore, int threeOfAKind,
int fourOfAKind, int fullHouse, int smallStraight,
int largeStraight, int yahtzee, int chance);
int tabulateDice(int n, int d1, int d2, int d3, int d4, int d5);
const int NUM_CATEGORIES = 13;
const int SIDES = 6;
const int EMPTY = -1;
enum Category { ONES = 1, TWOS, THREES, FOURS, FIVES, SIXES, THREE_OF_A_KIND,
FOUR_OF_A_KIND, FULL_HOUSE, SMALL_STRAIGHT, LARGE_STRAIGHT,
YAHTZEE, CHANCE };
int main()
{
int die1, die2, die3, die4, die5;
bool redo1, redo2, redo3, redo4, redo5;
int ones, twos, threes, fours, fives, sixes;
int onesScore, twosScore, threesScore, foursScore, fivesScore, sixesScore;
int threeOfAKind;
int fourOfAKind;
int fullHouse;
int smallStraight;
int largeStraight;
int yahtzee;
int chance;
onesScore = twosScore = threesScore = foursScore = fivesScore = sixesScore = EMPTY;
threeOfAKind = fourOfAKind = fullHouse = smallStraight = largeStraight = yahtzee = chance = EMPTY;
cout << \"Welcome to Yahtzee!\" << endl;
srand(time(0));
for (int turn = 0; turn < NUM_CATEGORIES; turn++)
{
int round = 1;
ones = twos = threes = fours = fives = sixes = 0;
die1 = rand() % SIDES + 1;
die2 = rand() % SIDES + 1;
die3 = rand() % SIDES + 1;
die4 = rand() % SIDES + 1;
die5 = rand() % SIDES + 1;
printRoll(die1, die2, die3, die4, die5);
do
{
redo1 = askReroll(1);
redo2 = askReroll(2);
redo3 = askReroll(3);
redo4 = askReroll(4);
redo5 = askReroll(5);
if (redo1)
{
die1 = rand() % SIDES + 1;
}
if (redo2)
{
die2 = rand() % SIDES + 1;
}
if (redo3)
{
die3 = rand() % SIDES + 1;
}
if (redo4)
{
die4 = rand() % SIDES + 1;
}
if (redo5)
{
die5 = rand() % SIDES + 1;
}
printRoll(die1, die2, die3, die4, die5);
round++;
} while ((redo1 || redo2 || redo3 || redo4 || redo5) && round < 3);
ones = tabulateDice(1, die1, die2, die3, die4, die5);
twos = tabulateDice(2, die1, die2, die3, die4, die5);
threes = tabulateDice(3, die1, die2, die3, die4, die5);
fours = tabulateDice(4, die1, die2, die3, die4, die5);
fives = tabulateDice(5, die1, die2, die3, die4, die5);
sixes = tabulateDice(6, die1, die2, die3, die4, die5);
int scoreOption = getScoreOption(onesScore, twosScore, threesScore, foursScore,
fivesScore, sixesScore, threeOfAKind,
fourOfAKind, fullHouse, smallStraight,
largeStraight, yahtzee, chance);
/*switch (scoreOption)
{
case ONES:
onesScore = scoreOnes(ones, twos, threes, fours, fives, sixes);
break;
case TWOS:
twosScore = scoreTwos(ones, twos, threes, fours, fives, sixes);
break;
case THREES:
threesScore = scoreThrees(ones, twos, threes, fours, fives, sixes);
break;
case FOURS:
foursScore = scoreFours(ones, twos, threes, fours, fives, sixes);
break;
case FIVES:
fivesScore = scoreFives(ones, twos, threes, fours, fives, sixes);
break;
case SIXES:
sixesScore = scoreSixes(ones, twos, threes, fours, fives, sixes);
break;
case THREE_OF_A_KIND:
threeOfAKind = scoreThreeOfAKind(ones, twos, threes, fours, fives, sixes);
break;
case FOUR_OF_A_KIND:
fourOfAKind = scoreFourOfAKind(ones, twos, threes, fours, fives, sixes);
break;
case FULL_HOUSE:
fullHouse= scoreFullHouse(ones, twos, threes, fours, fives, sixes);
break;
case SMALL_STRAIGHT:
smallStraight = scoreSmallStraight(ones, twos, threes, fours, fives, sixes);
break;
case LARGE_STRAIGHT:
largeStraight = scoreLargeStraight(ones, twos, threes, fours, fives, sixes);
break;
case YAHTZEE:
yahtzee = scoreYahtzee(ones, twos, threes, fours, fives, sixes);
break;
case CHANCE:
chance = scoreChance(ones, twos, threes, fours, fives, sixes);
break;
}*/
printScore(onesScore, twosScore, threesScore, foursScore, fivesScore, sixesScore,
threeOfAKind, fourOfAKind, fullHouse, smallStraight, largeStraight, yahtzee, chance);
}
}
/*********************************************************
*
* printRoll
* ------------------
* This function prints out the current state of the dice,
* with blank lines before and after the print-out.
*
*********************************************************/
void printRoll(int n1, int n2, int n3, int n4, int n5)
{
cout << endl;
cout << \"Your roll is:\" << endl;
cout << n1 << \" \" << n2 << \" \" << n3 << \" \" << n4 << \" \" << n5 << endl;
cout << endl;
}
/*********************************************************
*
* askReroll
* ------------------
* This function ask the user if they\'d like to reroll one
* one of the dice. The function takes an integer which is
* the number of the die being rerolled, and returns true if
* the die should be rerolled, false otherwise. The integer
* argument is used only for instruction display, this function
* does not actually reroll any dice. Responses accepted are
* \'Y\', \'N\', \'y\', and \'n\'.
*
*********************************************************/
bool askReroll(int n)
{
char ch;
while (true)
{
cout << \"Would you like to reroll die \" << n << \"? (Y/N) \";
cin >> ch;
switch (ch)
{
case \'Y\': case \'y\':
return true;
case \'N\': case \'n\':
return false;
default:
cout << \"Invalid response\" << endl;
}
}
}
/********************************
*
* printScore
* --------------------
* This function prints out the complete score table
* for the yahtzee game in its current state.
*
********************************/
void printScore(int onesScore, int twosScore, int threesScore, int foursScore,
int fivesScore, int sixesScore, int threeOfAKind,
int fourOfAKind, int fullHouse, int smallStraight,
int largeStraight, int yahtzee, int chance)
{
printSeparator();
printScoreLine(\"Ones\", onesScore);
printSeparator();
printScoreLine(\"Twos\", twosScore);
printSeparator();
printScoreLine(\"Threes\", threesScore);
printSeparator();
printScoreLine(\"Fours\", foursScore);
printSeparator();
printScoreLine(\"Fives\", fivesScore);
printSeparator();
printScoreLine(\"Sixes\", sixesScore);
printSeparator();
printScoreLine(\"Three of a kind\", threeOfAKind);
printSeparator();
printScoreLine(\"Four of a kind\", fourOfAKind);
printSeparator();
printScoreLine(\"Full House\", fullHouse);
printSeparator();
printScoreLine(\"Small Straight\", smallStraight);
printSeparator();
printScoreLine(\"Large Straight\", largeStraight);
printSeparator();
printScoreLine(\"Yahtzee\", yahtzee);
printSeparator();
printScoreLine(\"Chance\", chance);
printSeparator();
}
/********************************
*
* printSeperator
* --------------------
* This helper function prints out a single
* separator line used as part of the score
* printing function.
*
********************************/
void printSeparator()
{
cout << \"+-------------------+-------+\" << endl;
}
/********************************
*
* printScoreLine
* --------------------
* This function prints out a single line of the score.
* The string argument is the name of the category, while
* the in is the value of the score. The category name is left
* aligned, while the score value is right aligned.
*
********************************/
void printScoreLine(string name, int score)
{
cout << \"| \" << left << setw(18) << name << \"| \";
if (score >=0)
{
cout << right << setw(5) << score;
}
else
{
cout << \" \";
}
cout << \" |\" << endl;
}
/********************************
*
* getScoreOption
* --------------------
* This function gets the score category which the player wishes to score
* with the current roll. The function ensures that the response corresponds
* to a valid category. It also check to make sure that the selected category
* has not yet been scored (unscored categories contain the value EMPTY).
*
********************************/
int getScoreOption(int onesScore, int twosScore, int threesScore, int foursScore,
int fivesScore, int sixesScore, int threeOfAKind,
int fourOfAKind, int fullHouse, int smallStraight,
int largeStraight, int yahtzee, int chance)
{
int ans;
bool valid = false;
cout << \"Here are the categories: \" << endl;
cout << \"1. Ones\" << endl;
cout << \"2. Twos\" << endl;
cout << \"3. Threes\" << endl;
cout << \"4. Fours\" << endl;
cout << \"5. Fives\" << endl;
cout << \"6. Sixes\" << endl;
cout << \"7. Three of a kind\" << endl;
cout << \"8. Four of a kind\" << endl;
cout << \"9. Full House\" << endl;
cout << \"10. Small Straight\" << endl;
cout << \"11. Large Straight\" << endl;
cout << \"12. Yahtzee\" << endl;
cout << \"13. Chance\" << endl;
do
{
cout << \"How would you like to score this? \";
cin >> ans;
while (ans < 0 || ans > NUM_CATEGORIES)
{
cout << \"Please enter a number between 1 and \" << NUM_CATEGORIES << \": \";
cin >> ans;
}
switch (ans)
{
case ONES:
if (onesScore == EMPTY) valid = true;
break;
case TWOS:
if (twosScore == EMPTY) valid = true;
break;
case THREES:
if (threesScore == EMPTY) valid = true;
break;
case FOURS:
if (foursScore == EMPTY) valid = true;
break;
case FIVES:
if (fivesScore == EMPTY) valid = true;
break;
case SIXES:
if (sixesScore == EMPTY) valid = true;
break;
case THREE_OF_A_KIND:
if (threeOfAKind == EMPTY) valid = true;
break;
case FOUR_OF_A_KIND:
if (fourOfAKind == EMPTY) valid = true;
break;
case FULL_HOUSE:
if (fullHouse == EMPTY) valid = true;
break;
case SMALL_STRAIGHT:
if (smallStraight == EMPTY) valid = true;
break;
case LARGE_STRAIGHT:
if (largeStraight == EMPTY) valid = true;
break;
case YAHTZEE:
if (yahtzee == EMPTY) valid = true;
break;
case CHANCE:
if (chance == EMPTY) valid = true;
break;
}
if (!valid)
{
cout << \"That category has already been used\" << endl;
}
} while (!valid);
return ans;
}
/********************************
*
* tabulateDice
* --------------------
* This function calculates and returns the number of dice
* which show the value n.
*
********************************/
int tabulateDice(int n, int d1, int d2, int d3, int d4, int d5)
{
int ans = 0;
if (d1 == n) ans++;
if (d2 == n) ans++;
if (d3 == n) ans++;
if (d4 == n) ans++;
if (d5 == n) ans++;
return ans;
}
Solution
Scoring
To score your combination of 5 dice, you click one of the 13 boxes, or write it on the scorecard (scoresheet). There are two sections to the score table - the Upper Section and the Lower Section.
Once a box has been scored, it cannot be scored again for the rest of the game (except the Yahtzee category), so choose wisely.
Upper Section Scoring
If you score in the upper section of the table, your score is the total of the specified die face.
So if you roll:
5 - 2 - 5 - 6 - 5 and score in the Fives category, your total for the category would be 15, because there are three fives, which are added together.
If the One, Three or Four Categories were selected for scoring with this roll, you would score a zero.
If placed in the Two or Six category, you would score 2 and 6 respectively.
Bonus If the total of Upper scores is 63 or more, add a bonus of 35. Note that 63 is the total of three each of 1s, 2s, 3s, 4s, 5s and 6s.
Lower Section Scoring
In the lower scores, you score either a set amount, or zero if you don\'t satisfy the category requirements.
3 and 4 of a kind For 3 of a kind you must have at least 3 of the same die faces. You score the total of all the dice. For 4 of a kind you would need 4 die faces the same.












