This is assignment 4, Yahtzee, which is a dice game. This assignment comes once again from the Stanford CS106A course. I have included the basic assignment 4 code, and over the next two posts going to explain about the thoughts behind working out the categories within the game, e.g. yahtzee, straight etc. And then in the 3rd post I am going to do the extensions of the assignment.
So to start with here are the different types of categories that the 5 dice can fall into with also there score associated value. ( the number at the start is the category that the dice would fall into)
- 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. (same as 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, and so on, 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. 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 all of the values showing on the dice.
There is a few hints within the assignment
- There’s not much difference between determining the validity for Three of a Kind, Four of a Kind, Yahtzee, and Full House.
- There’s not much difference between determining the validity for Small Straight and Large Straight.
- Any dice configuration is valid for Ones, Twos, Threes, Fours, Fives, Sixes, and Chance.
- A dice configuration assigned to a category where it doesn’t meet the requirements receives a score of 0.
which kinder give some basic help, on how to figure out the different categories.
Categories 1-6, counting single values
From the start I am going to figure out the first 6 categories, which means that if any of the values within the 5 dice are from 1-6 (each category) then add that dice value into that score. So, what we need to do is loop thought the dice’s and then sum up the dice that meet that requirement (e.g. either a 1 or 2 or 3 depending on that category), so a basic function would require the dice array and also the category (number) to check against and if any of the dice are in that category then add that value of the dice to the returning value. Also since the 13th category, (chance), adds up all of the values of the dice, then can use this function as well. Below is my java function to achieve these categories
// add single categories e.g. 1,2,3,4,5 and only the dice that have those values
private int addSingle(int[] dice, int category)
{
int sum =0;
for (int i =0; i < YahtzeeConstants.N_DICE; i++)
{
if (category == dice[i]) sum+=dice[i];
if (category == YahtzeeConstants.CHANCE)sum+=dice[i];
}
return sum;
}
Categories 7,8, 12 – Three/Four of a kind, and Yahtzee
Next, we are going to tackle the 3/4/5(Yahtzee) of a kind, e.g. to start with, I thought that if we had the array dice in a sorted array (e.g. 2,2,2,4,5 as such) then it would be easier to find the groups of dices that are the same, but I am using the sorting for other categories as well, so the sorting function is lower in this post. So once the dice are ordered, what we need to do is to count up the values of the dice (the sum value below) which is going to be return value if there is the correct amount of found dice in the dice array. Finding the correct amount (X being 3/4/5 of dice), we need to count up the number of dice that are of the same value, so we need to have a count value and also whilst going through the array check with the present dice with the previous value, if they are the same increment the count value, otherwise if they are not then re-set the counting value to 1 ONLY if we are below the categories check e.g. three of a kind (3), or four of a kind (4) or yahtzee (5), and then check at the end of the function if the count is above the X value (the categories value) and if so return the sum’d value of the dice array or return 0.
So here it is, the java code that will find out if there is 3 / 4 / 5 dice of the same value and return the sum of the dice.
// X being the three / four / five (Yahtzee!!)
private int checkXOfAKind(int[] dice, int X)
{
// if count = 3 then there is a three of a kind
int count =1, sum = dice[0];
for (int i =1; i < YahtzeeConstants.N_DICE; i++)
{
// if the same value
if (dice[i] == dice[i-1])
{
count++;
}
else
{
if (count < X)
count = 1;
}
sum += dice[i];
}
if (count >= X)
return sum;
else
return 0;
}
Bubble sort
Here is am sorting the array of dice into a order, using the bubble sort algorithm, which makes it easier to find the groups of dice values, bubble sort will basically move (bubble) up the array if that value is the highest in the array or find the place where it would fit, for example if you had a array of 5 values
and the first time though the bubble you select the number 4 (since that is at the start of the array, and call the position in the array to be checked positionCheck) and check against the next value, 3 in this case and since 4 is greater than 3 then it will swap those values, so after the first check the new values would be
but in the second check on this first run, the next value is 4 (since we have moved one up in the array, and after the swap, it is 4 again and positionCheck is 2 now) but now the next value is 5 which is greater than 4 and so we do not need to swap them values, so positionCheck moves up one more (value of 3) and 5 is greater than 2, thus swap and then again since positionCheck would be 4 and this value is 5 (after the swapping of 5 and 2) which once again 5 is greater than 1, so swap .. so after the first run though we have found the highest number and moved that to the end, so the new array would be
so we would need to do the bubble of few more times (length of array amount of times minus 1, since the highest value is moved to the top at the start), but in the code I have not taken off the minus 1 since it just looks slightly better at times, not much of a waste of CPU cycles this time around since there is only 5 dice to check.
// to find the values, first order the dice, thus able to create the straight and also if the dice are
// sorted then can easily find the ones, twos etc.
private void orderDice(int[] dice)
{
// use the bubble sort
for (int i =0; i < YahtzeeConstants.N_DICE; i++)
{
for (int j =0; j < YahtzeeConstants.N_DICE; j++)
{
// swap if right value is greater than the left
if (dice[i] < dice[j])
{
int temp = dice[i];
dice[i] = dice[j];
dice[j] = temp;
}
}
}
}
the next post will include the other categories, and here is link to the second post about the yahtzee game.