C coding assignment
Hints for Game Logic for CSlam
The following is just a suggestion for determining what a roll is. You are more than welcome to develop your own logic. You
are also welcome to sort the array if you think that helps – I think you find that sorting an array in C is more work than you
expected and won’t really help with the issue – but that’s up to you.
Create another array called HowMany. This array will hold a count of how many of each face is found in your dice roll. For
example, if my dice roll is 1 3 3 2 4, then HowMany will be
HowMany[0] = 1 // because my roll contains one 1
HowMany[1] = 1 // because my roll contains one 2
HowMany[2] = 2 // because my roll contains two 3s
HowMany[3] = 1 // because my roll contains one 4
HowMany[4] = 0 // because my roll contains zero 5s
HowMany[5] = 0 // because my roll contains zero 6s
After each roll of the dice (after you have used the random number generator to fill up the dice array with random numbers
between 1 and 6), loop as many times as you have dice (this should be a define).
For each element of the dice array (each element represents the upperward face of a die), increment the corresponding value
in HowMany.
For example, if the player rolls
1 6 4 3 6
then, after the loop completes, the array HowMany will have the following in it
Cell 0 in the array HowMany contains how many 1's are in the player's roll – there's one.
Cell 1 in the array HowMany contains how many 2's are in the player's roll – there's none.
Cell 2 in the array HowMany contains how many 3's are in the player's roll – there's one.
Cell 3 in the array HowMany contains how many 4's are in the player's roll – there's one.
Cell 4 in the array HowMany contains how many 5's are in the player's roll – there's none.
Cell 5 in the array HowMany contains how many 6's are in the player's roll – there's two.
Now you can loop to check each cell of HowMany and run rules for each type of dice combination in order to detect them.
Read through array HowMany by looping over the array. Checking each element will allow us to detect each type of
combination.
for loop…
{
if HowMany[i] contains 3, then add 3 to variable FullHouse
if HowMany[i] contains 2, then add 2 to variable FullHouse and increment variable TwoPair
0 1 2 3 4 5
1 0 1 1 0 2
if HowMany[i] contains 5, then increment variable CSlam.
if HowMany[i] contains 1, then increment variable LargeStraight
else if HowMany[i] contains 0 AND variable LargeStraight is greater than 0 and less than 5,
then set variable LargeStraight equal to 0
if HowMany[i] is greater than or equal to 1, then increment variable SmallStraight
else if HowMany[i] contains 0 AND variable SmallStraight is greater than 0 and less than 4,
then set variable SmallStraight equal to 0
if HowMany[i] contains 4, then set variable ofAKind to 4
if HowMany[i] contains 3, then set variable ofAKind to 3
}
After we finish looking through HowMany, we now look at the variables we set while looping over the
HowMany array and print out which combination our looping found.
if variable LargeStraight is 5, then print "Large Straight"
else if variable SmallStraight is greater than or equal to 4, then print "Small Straight"
else if variable FullHouse is 5, then print "Full House"
else if variable CSlam is 1, then print "CSlam!!"
else if variable ofAKind is 4, then print "Four of a kind"
else if variable ofAKind is 3, then print "Three of a kind"
else if variable TwoPair is 2, then print "Two Pair"
else print "You have nothing"