[英]Number generator is not functioning properly in my C++ dice game program
我是 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";
}
}
有人可以帮我找到问题吗? 谢谢你。
我假设随机数生成器无法正常工作。
我可以向你保证,随机发生器不是你的问题。
编写一个选择谁先走的 function。 注意 - 您的代码给玩家两次机会,即使他们抛出相同的数字 - 您的代码将继续进入游戏。
请注意,每个玩家检查输赢组合的算法是相同的。 写一次,作为 function,然后调用第一个玩家,然后,如果需要,再调用第二个。
不要预先掷骰子; 在需要时获取价值。 也可以在单独的单行 function 中完成。
我完全赞成命名常量,但是您的numOne
.. numSix
是一种矫枉过正。
想评论你的变量名,但大多数都不需要……
提示:如果您使用已知值(如 0、1 等)为随机生成器播种,您将获得相同的伪随机序列。 非常适合调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.