【发布时间】:2022-01-31 19:35:33
【问题描述】:
我是 C++ 新手,因为我正在尝试从错误中吸取教训。我正在编写一个程序,该程序应该让每个玩家掷骰子,而数量最多的人可以通过掷骰子两次来开始游戏。如果两者的掷骰子值相等。然后,他们再次投掷,直到有人获得最大价值。如果开始的玩家获得 3-3、5-5、6-6、6-5,则该玩家获胜,如果该玩家获得 1-1、2-2、4-4、1-2,则该玩家获胜。玩家输。如果两者都不是,则开始的玩家轮到结束,然后第二个玩家开始。如果他不满足相同的条件;输或赢,则比赛为平局。我试图实现它,但它不起作用,我不知道为什么。我假设随机数生成器无法正常工作。这是我下面的代码:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
srand(time(0));
cout << " -- Barbooth dice-rolling game with 2 players –" << endl;
cout << endl;
int numOne = 1;
int numTwo = 2;
int numThree = 3;
int numFour = 4;
int numFive = 5;
int numSix = 6;
int firstThrow = (rand() % 6) + 1;
int secondThrow = (rand() % 6) + 1;
int p1 = (rand() % 6) + 1; //First throw for player one
int p2 = (rand() % 6) + 1; //First throw for player two
int p1s = (rand() % 6) + 1; //Second throw for player one
int p2s = (rand() % 6) + 1; //Second throw for player two
int thirdThrow = (rand() % 6) + 1;
int fourthThrow = (rand() % 6) + 1;
int thirdThrows = (rand() % 6) + 1;
int fourthThrows = (rand() % 6) + 1;
int fivthThrow = (rand() % 6) + 1;
int sixthThrow = (rand() % 6) + 1;
int fivthThrow1 = (rand() % 6) + 1;
int sixthThrow1 = (rand() % 6) + 1;
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << firstThrow << endl;
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << secondThrow << endl;
cout << endl;
if(firstThrow > secondThrow)
{
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << p1 << endl;
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << p1s << endl;
}
else if(firstThrow < secondThrow)
{
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << p2 << endl;
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << p2s << endl;
}
else if(firstThrow == secondThrow)
{
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << thirdThrow << endl;
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << fourthThrow << endl;
if(thirdThrow > fourthThrow)
{
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << fivthThrow1 << endl;
cout << "Player 1, please enter to roll." << endl;
cout << "Player 1: " << sixthThrow1 << endl;
}
else if(fourthThrow > thirdThrow)
{
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << fivthThrow << endl;
cout << "Player 2, please enter to roll." << endl;
cout << "Player 2: " << sixthThrow << endl;
}
}
else
{
cout << "Tie!" << endl;
}
// All game conditions
if(p1 == numThree && p1s == numThree)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout<< "Player 1 wins\n";
}
else if(p1 == numFive && p1s == numFive)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout<< "Player 1 wins\n";
}
else if(p1 == numSix && p1s == numSix)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout<< "Player 1 wins\n";
}
else if(p1 == numSix && p1s == numFive)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout<< "Player 1 wins\n";
}
else if(p1 == numOne && p1s == numOne)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout << "Player 2 wins\n";
}
else if(p1 == numTwo && p1s == numTwo)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout << "Player 2 wins\n";
}
else if(p1 == numFour && p1s == numFour)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout << "Player 2 wins\n";
}
else if(p1 == numOne && p1s == numTwo)
{
cout<< "Player 1: " << p1 << "-" << p1s << endl;
cout << "Player 2 wins\n";
}
else if(p2 == numThree && p2s == numThree)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 2 wins\n";
}
else if(p2 == numFive && p2s == numFive)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 2 wins\n";
}
else if(p2 == numSix && p2s == numSix)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 2 wins\n";
}
else if(p2 == numSix && p2s == numFive)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 2 wins\n";
}
else if(p2 == numOne && p2s == numOne)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 1 wins\n";
}
else if(p2 == numTwo && p2s == numTwo)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 1 wins\n";
}
else if(p2 == numFour && p2s == numFour)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 1 wins\n";
}
else if(p2 == numOne && p2s == numTwo)
{
cout<< "Player 2: " << p2 << "-" << p2s << endl;
cout << "Player 1 wins\n";
}
////////////////////////////////////
else if(fivthThrow1 == numThree && sixthThrow1 == numThree)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout<< "Player 1 wins\n";
}
else if(fivthThrow1 == numFive && sixthThrow1 == numFive)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout<< "Player 1 wins\n";
}
else if(fivthThrow1 == numSix && sixthThrow1 == numSix)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout<< "Player 1 wins\n";
}
else if(fivthThrow1 == numSix && sixthThrow1 == numFive)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout<< "Player 1 wins\n";
}
else if(fivthThrow1 == numOne && sixthThrow1 == numOne)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow1 == numTwo && sixthThrow1 == numTwo)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow1 == numFour && sixthThrow1 == numFour)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow1 == numOne && sixthThrow1 == numTwo)
{
cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow == numThree && sixthThrow == numThree)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow == numFive && sixthThrow == numFive)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow == numSix && sixthThrow == numSix)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow == numSix && sixthThrow == numFive)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 2 wins\n";
}
else if(fivthThrow == numOne && sixthThrow == numOne)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 1 wins\n";
}
else if(fivthThrow == numTwo && sixthThrow == numTwo)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 1 wins\n";
}
else if(fivthThrow == numFour && sixthThrow == numFour)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 1 wins\n";
}
else if(fivthThrow == numOne && sixthThrow == numTwo)
{
cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
cout << "Player 1 wins\n";
}
////////////////////////////////
else
{
cout<< "Tie! \n";
}
}
有人可以帮我找到问题吗?谢谢。
【问题讨论】:
-
“不起作用”是什么意思?
-
战术说明:当您按顺序命名或编号变量时,您可能会发现使用数组编写代码要容易得多。
-
注意there are better ways在C++中生成的随机数比
rand()。 -
程序员的秘密武器是调试器。使用调试器,您可以按照自己的速度执行程序,并观察程序的运行情况。典型的用法是逐行执行,直到您看到一些您没有预料到的东西,例如存储的错误值或采用的错误路径。意外是您的程序或您的期望中的错误。任何一个都需要修复才能继续。
-
有点生气,原来的 Rand 被认为是有害的演示文稿不再被托管。我确定有人在我还没有找到的地方发布了一份副本。
标签: c++