C++ Small Straight (like yahtzee or poker). 5 Dice

2019-06-12 03:13发布

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;

}

标签: c++ c poker
3条回答
该账号已被封号
2楼-- · 2019-06-12 03:44

I combine all the rolls in a single variable.

A 32 bits long can easily hold all the counts:

//Number of ones     V
long result = 0x000000;
// sixes        ^

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 or 0x011111. A full house 555-33 is 0x030200. A short straight is a bit more complex, it looks like 0x101111 or 0x001121. 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.

查看更多
女痞
3楼-- · 2019-06-12 03:46
int bits = (1 << Roll1) | (1 << Roll2) | (1 << Roll3) | (1 << Roll4) | (1 << Roll5);
if((bits & 0x1E) == 0x1E || (bits & 0x3C) == 0x3C || (bits & 0x78) == 0x78)
    cout << "Yes Small Straight!" << endl;
查看更多
神经病院院长
4楼-- · 2019-06-12 03:48

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 necessarily c++11.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    int straight_checker = 0;
    int rolls[5] = {0}, num_count[6] = {0};

    // use char * die_names[6] if c
    string die_names[6] = {"ones", "twos", "threes", "fours", "fives", "sixes"};
    bool two_kind = false, three_kind = false, four_kind = false, five_kind = false;

    cout << "Roll 5 dice. Enter them below." << endl;
    for(unsigned int i = 0; i < sizeof(rolls) / sizeof(int); ++i)
    {
        cout << "Roll " << i + 1 << ": ";
        cin >> rolls[i];

        if(rolls[i] < 1 || rolls[i] > 6)
        {
            exit(1);
        }
        else
        {
            num_count[rolls[i] - 1]++;
            // This will set a binary "1" at the location rolls[i] - 1, easy way to check straights
            straight_checker ^= (-1 ^ straight_checker) & (1 << (rolls[i] - 1));
        }
    }

    for(unsigned int i = 0; i < sizeof(num_count) / sizeof(int); ++i)
    {
        cout << endl << "There are " << num_count[i] << " " <<die_names[i];

        // If you are using c++11, then use std::find outside loop
        switch(num_count[i])
        {
            case 2:
                two_kind = true;
                break;
            case 3:
                three_kind = true;
                break;
            case 4:
                four_kind = true;
                break;
            case 5:
                five_kind = true;
                break;
        }
    }

    cout << endl;

    if(four_kind) cout << "Yes four of a kind!" << endl;
    if(five_kind) cout << "Yes five of a kind!" << endl;
    if(two_kind && three_kind) cout << "Yes Full House!" << endl;
    else
    {
        if(three_kind) cout << "Yes three kind" << endl;
        else if(two_kind) cout << "Yes two kind" << endl;
    }

    // 31 in binary is 011111, and 62 is 111110 (ie 5 straight 1s)
    if(straight_checker == 31 || straight_checker == 62)
    {
        cout << "Yes long straight!" << endl;
    }
    // 47 in binary is 101111, 30 is 011110, 61 is 111101, etc (ie 4 straight 1s)
    if(straight_checker == 15 || straight_checker == 47 || straight_checker == 30 || straight_checker == 60 || straight_checker == 61)
    {
        cout << "Yes short straight" << endl;
    }

    return 0;
}
查看更多
登录 后发表回答