gpt4 book ai didi

C++ Small Straight(如 yahtzee 或 poker)。 5 掷骰子 (1234)/(2345)/(3456)

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:53 28 4
gpt4 key购买 nike

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

分配的问题:

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

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

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

• 计算并打印输入的二元数。

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

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

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

• 计算并打印输入的 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;

最佳答案

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;

关于C++ Small Straight(如 yahtzee 或 poker)。 5 掷骰子 (1234)/(2345)/(3456),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893319/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com