Im a beginner with C++ and this is for homework but Im stuck. I have one remaining problem and then im finished. I can'T think of an algorithm that will tell if the user entered a small straight (1234) or (2345) or (3456). I know how to do it using a loop but is it possible to not use a loop? When this was assigned, we did not learned about loops yet. I think we are only suppose to use if statements. I started coding for it (I wrote what the dice can be, but cant figure out how to let only 4 dice go straight without an array and loop). Im stuck! Please help! Can you also check at my code and see if there is anything wrong so far.
Assigned Problem:
Playing poker with dice. (Kinda like Yahtzee)
• Have the user enter 5 dice rolls (1-6) in any order. Have the user type in 5 numbers. If any are outside of the 1 to 6 inclusive range, just end the program.
• Count and print the number of ones entered.
• Count and print the number of twos entered.
• Count and print the number of threes entered.
• Count and print the number of fours entered.
• Count and print the number of fives entered.
• Count and print the number of sixes entered.
• Tell if there are three or more dice the same.
• Tell if there are four or more dice the same.
• Tell if there are five dice the same.
• Tell if there is a full house, 3 the same and 2 the same.
• Tell if there is a long straight ... 1 2 3 4 5 or 2 3 4 5 6
• Tell if there is a small straight (1 2 3 4) or (2 3 4 5) or (3 4 5 6)
My Code:
int main(int argc, char** argv)
{
//
int Roll1, Roll2, Roll3, Roll4, Roll5;
int numberOf1s = 0, numberOf2s = 0, numberOf3s = 0, numberOf4s = 0, numberOf5s = 0, numberOf6s = 0;
bool TwoOfaKind = false;
bool ThreeOfaKind = false;
cout << "Roll 5 dice. Enter them below." << endl;
cout << "Roll 1: ";
cin >> Roll1;
cout << "Roll 2: ";
cin >> Roll2;
cout << "Roll 3: ";
cin >> Roll3;
cout << "Roll 4: ";
cin >> Roll4;
cout << "Roll 5: ";
cin >> Roll5;
if (Roll1 > 6 || Roll1 < 1 || Roll2 > 6 || Roll2 < 1 || Roll3 > 6 || Roll3 < 1 || Roll4 > 6 || Roll4 < 1 || Roll5 > 6 || Roll5 < 1 )
{
exit(1);
}
if (Roll1 == 1)
{
numberOf1s += 1;
}
if (Roll2 == 1)
{
numberOf1s += 1;
}
if (Roll3 == 1)
{
numberOf1s += 1;
}
if (Roll4 == 1)
{
numberOf1s += 1;
}
if (Roll5 == 1)
{
numberOf1s += 1;
}
cout << "There are " << numberOf1s << " ones." << endl;
if (Roll1 == 2)
{
numberOf2s += 1;
}
if (Roll2 == 2)
{
numberOf2s += 1;
}
if (Roll3 == 2)
{
numberOf2s += 1;
}
if (Roll4 == 2)
{
numberOf2s += 1;
}
if (Roll5 == 2)
{
numberOf2s += 1;
}
cout << "There are " << numberOf2s << " twos." << endl;
if (Roll1 == 3)
{
numberOf3s += 1;
}
if (Roll2 == 3)
{
numberOf3s += 1;
}
if (Roll3 == 3)
{
numberOf3s += 1;
}
if (Roll4 == 3)
{
numberOf3s += 1;
}
if (Roll5 == 3)
{
numberOf3s += 1;
}
cout << "There are " << numberOf3s << " threes." << endl;
if (Roll1 == 4)
{
numberOf4s += 1;
}
if (Roll2 == 4)
{
numberOf4s += 1;
}
if (Roll3 == 4)
{
numberOf4s += 1;
}
if (Roll4 == 4)
{
numberOf4s += 1;
}
if (Roll5 == 4)
{
numberOf4s += 1;
}
cout << "There are " << numberOf4s << " fours." << endl;
if (Roll1 == 5)
{
numberOf5s += 1;
}
if (Roll2 == 5)
{
numberOf5s += 1;
}
if (Roll3 == 5)
{
numberOf5s += 1;
}
if (Roll4 == 5)
{
numberOf5s += 1;
}
if (Roll5 == 5)
{
numberOf5s += 1;
}
cout << "There are " << numberOf5s << " fives." << endl;
if (Roll1 == 6)
{
numberOf6s += 1;
}
if (Roll2 == 6)
{
numberOf6s += 1;
}
if (Roll3 == 6)
{
numberOf6s += 1;
}
if (Roll4 == 6)
{
numberOf6s += 1;
}
if (Roll5 == 6)
{
numberOf6s += 1;
}
cout << "There are " << numberOf6s << " sixes." << endl << endl;
if (numberOf1s == 2 || numberOf2s == 2 || numberOf3s == 2 || numberOf4s == 2 || numberOf5s == 2 || numberOf6s == 2)
{
TwoOfaKind = true;
}
if (numberOf1s == 3 || numberOf2s == 3 || numberOf3s == 3 || numberOf4s == 3 || numberOf5s == 3 || numberOf6s == 3)
{
cout << "Yes three of a kind!" << endl;
ThreeOfaKind = true;
}
else
{
cout << "No three of a kind" << endl;
}
if (numberOf1s == 4 || numberOf2s == 4 || numberOf3s == 4 || numberOf4s == 4 || numberOf5s == 4 || numberOf6s == 4)
{
cout << "Yes four of a kind!" << endl;
}
else
{
cout << "No four of a kind" << endl;
}
if (numberOf1s == 5 || numberOf2s == 5 || numberOf3s == 5 || numberOf4s == 5 || numberOf5s == 5 || numberOf6s == 5)
{
cout << "Yes five of a kind!" << endl;
}
else
{
cout << "No five of a kind" << endl;
}
//
if (TwoOfaKind == true && ThreeOfaKind == true)
{
cout << "Yes Full House!" << endl;
}
else
{
cout << "No Full House" << endl;
}
//
if (Roll1 == Roll2 && Roll2 == Roll3 && Roll3 == Roll4 && Roll4 == Roll5)
{
cout << "No Long Straight" << endl;
}
else
{
if (((Roll1 >= 1 && Roll1 < 6) && (Roll2 >= 1 && Roll2 < 6) && (Roll3 >= 1 && Roll3 < 6) && (Roll4 >= 1 && Roll4 < 6) && (Roll5 >= 1 && Roll5 < 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Long Straight!" << endl;
}
else if (((Roll1 > 1 && Roll1 <= 6) && (Roll2 > 1 && Roll2 <= 6) && (Roll3 > 1 && Roll3 <= 6) && (Roll4 > 1 && Roll4 <= 6) && (Roll5 > 1 && Roll5 <= 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Long Straight!" << endl;
}
else
{
cout << "No Long Straight" << endl;
}
//***HELP ME HERE PLEASE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***
if (Roll1 == Roll2 && Roll2 == Roll3 && Roll3 == Roll4 && Roll4 == Roll5)
{
cout << "No Small Straight" << endl;
}
else
{
if (((Roll1 > 1 && Roll1 <= 4) && (Roll2 > 1 && Roll2 <= 4) && (Roll3 > 1 && Roll3 <= 4) && (Roll4 > 1 && Roll4 <= 4) && (Roll5 > 1 && Roll5 <= 4)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else if (((Roll1 > 1 && Roll1 <= 5) && (Roll2 > 1 && Roll2 <= 5) && (Roll3 > 1 && Roll3 <= 5) && (Roll4 > 1 && Roll4 <= 5) && (Roll5 > 1 && Roll5 <= 5)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else if (((Roll1 > 2 && Roll1 <= 6) && (Roll2 > 2 && Roll2 <= 6) && (Roll3 > 2 && Roll3 <= 6) && (Roll4 > 2 && Roll4 <= 6) && (Roll5 > 2 && Roll5 <= 6)) && (Roll1 != Roll2 && Roll2 != Roll3 && Roll3 != Roll4 && Roll4 != Roll5))
{
cout << "Yes Small Straight!" << endl;
}
else
{
cout << "No Small Straight" << endl;
}
}
return 0;
}
I combine all the rolls in a single variable.
A 32 bits
long
can easily hold all the counts:Each hexadecimal digit can hold a value between 0 and F (15) so that's sufficient for 15 rolls, and you only need 5.
Your long straight would be
0x111110
or0x011111
. A full house 555-33 is0x030200
. A short straight is a bit more complex, it looks like0x101111
or0x001121
. To get rid of that 2, I'd use a bit shift:long test = (result | (result>>1)) & 0x777777
and then it's a straightforward check:test & 0x001111 == 0x001111
etc.I decided to rewrite your program which refactors a lot of the code and also solves your problem. Do note I assumed you are using
c++
but not necessarilyc++11
.