C ++ Small Straight(比如yahtzee或扑克)。 5骰子卷(1234)/(2345)/(3456)

时间:2015-03-06 06:18:54

标签: c++ c poker

我是C ++的初学者,这是作业,但我卡住了。我有一个剩下的问题然后我完成了。 我不能想出一个算法,它将告诉用户是否输入了一个小直(1234)或(2345)或(3456)。 我知道如何使用循环,但是可以不使用循环吗?分配时,我们还没有学习循环。我想我们只是假设使用if语句。 我开始为它编码(我写了骰子可以做什么,但是无法弄清楚如何在没有数组和循环的情况下只让4个骰子直接进行)。我卡住了!请帮忙! 您是否也可以查看我的代码,看看到目前为止是否有任何问题。

指定问题:

用骰子玩扑克。 (有点像Yahtzee)

•让用户以任何顺序输入5个骰子卷(1-6)。让用户输入5个数字。如果有任何超出1到6的范围,只需结束该程序。

•计算并打印输入的数量。

•计算并打印输入的两个数字。

•计算并打印输入的三分数。

•计算并打印输入的四分数。

•计算并打印输入的五个数字。

•计算并打印输入的六位数。

•判断是否有三个或更多骰子相同。

•判断是否有四个或更多骰子相同。

•判断是否有五个骰子相同。

•判断是否有满屋,3是相同的,2是相同的。

•判断是否有长直... 1 2 3 4 5或2 3 4 5 6

•判断是否有小直(1 2 3 4)或(2 3 4 5)或(3 4 5 6)

我的代码:

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;

}

3 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分:0)

我决定重写你的程序,它重构了很多代码并解决了你的问题。请注意我假设您使用的是c++,但不一定是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;
}

答案 2 :(得分:0)

我将所有卷组合在一个变量中。

32位long可轻松保存所有计数:

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

每个十六进制数字可以保存0到F(15)之间的值,这样就可以满足15卷,而且只需要5个。

你的长直道将是0x1111100x011111。满屋555-33是0x030200。短直线稍微复杂一点,看起来像0x1011110x001121。为了摆脱那个2,我使用了一个位移: long test = (result | (result>>1)) & 0x777777然后它是一个简单的检查:test & 0x001111 == 0x001111等。