如何在c++骰子游戏中存储第一个值,并在下一步进行比较
How do I store the first value in a c++ dice game and compare it in the next move?
我目前正在开发一款骰子游戏。用户先掷一对骰子,假设他掷了骰子,骰子1=2,骰子2=3。所以现在总数是5。现在,他需要再次获得5分(总得分)才能获胜,如果他在下一步没有获得5分,那么他就会再次投球,比赛就会继续。但是,如果在任何时候,他总共滚了两个球,他就会放松。
所以,请告诉我如何存储第一个滚动的值,并将其和下一个移动进行比较。我试过一些东西,但似乎不起作用。
#include<iostream>
#include<ctime> // for the time() function
#include<cstdlib> // for the srand() and rand() functions
using namespace std;
// Declare variables
//int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int dieTotal = 0;
int Dice ()
{
// roll the first die
die1 = (rand() % 6 ) + 1;
// roll the second die
die2 = (rand() % 6 ) + 1;
}
// iniating a second two pair dice function.
int compDice()
{
Dice();
dieTotal = die1 + die2;
return (dieTotal);
}
// User Rolling the dice and calucalting the total here
int userGame()
{
cout << "nUser turn --- Press 2 to roll" << endl;
cin >> userInput;
if ( userInput == 2 )
{
Dice ();
cout << "nThe user rolled Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
cout << "Total = " << die1 + die2 << endl;
}
else {
cout << "Wrong input.";
//userGame();
}
return (die1 + die2 );
}
int checkForWin ()
{
while (true)
{
int result1 = compDice();
int result = userGame();
// int finalResult = dieTotal;
if (result == result1 )
{
cout << "nUser won. Computer looses....m " << endl;
break;
}
else if (result == 2)
{
cout << "nUser looses. Computer won." <<endl;
break;
}
else
{
}
}
}
// Calling for the checkForWin() function in main and the srand.
int main ()
{
cout << "This is the Dice game. " << endl;
// set the seed
srand(time(0));
checkForWin(); // Initiating the game.
return 0;
}
在我们的评论聊天/误解之后,我自由地复制并修改了你的代码(尽可能少地维护你的编码风格-我不建议将来的任何项目使用这种风格),以产生你想要的结果。让我知道它是否有效(简单的测试表明它有效,可能错过了其他一些怪癖)
#include<iostream>
#include<ctime> // for the time() function
#include<cstdlib> // for the srand() and rand() functions
using namespace std;
// Declare variables
//int compInput;
int userInput;
int firstRoll = 1;
int die1 = 0;
int die2 = 0;
int dieTotalToMatch = 0;
void Dice ()
{
// roll the first die
die1 = (rand() % 6 ) + 1;
// roll the second die
die2 = (rand() % 6 ) + 1;
}
// iniating a second two pair dice function.
void compDice()
{
Dice();
dieTotalToMatch = die1 + die2;
}
// User Rolling the dice and calucalting the total here
int userGame()
{
cout << "nUser turn --- Press 2 to roll" << endl;
cin >> userInput;
if ( userInput == 2 )
{
Dice ();
cout << "nThe user rolled Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
cout << "Total = " << die1 + die2 << endl;
}
else {
cout << "Wrong input.";
//userGame();
}
return (die1 + die2 );
}
void checkForWin ()
{
while (true)
{
int result = userGame();
if (firstRoll)
{
dieTotalToMatch = result;
firstRoll = 0;
continue;
}
// int finalResult = dieTotal;
if (result == dieTotalToMatch )
{
cout << "nUser won. Computer looses....m " << endl;
break;
}
else if (result == 2)
{
cout << "nUser looses. Computer won." <<endl;
break;
}
else
{
}
}
}
// Calling for the checkForWin() function in main and the srand.
int main ()
{
cout << "This is the Dice game. " << endl;
// set the seed
srand(time(0));
checkForWin(); // Initiating the game.
cin.ignore();
return 0;
}
相关文章:
- 需要反转音频步进公式
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- 锁定步进pthread互斥
- 在 ODEINT 中通过 if 语句选择步进器
- boost odeint 中的受控误差步进器是否支持复杂的数据类型?
- 创建带有限幅器的步进器控件
- 班上的Arduino步进电动机指针不起作用
- 生成最多给定数字 N 的步进数字
- Boost ODEINT:具有自定义类和向量空间代数的受控步进器
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- 如何使步进电机运行,直到我按下 Arduino 上的特定键盘按钮
- Arduino Sonar和步进马达
- 这些步进回合是否等效
- 使用C++同步控制多个步进器
- 接口 C++ 以使用 Arduino 控制电机步进器
- 如何在c++骰子游戏中存储第一个值,并在下一步进行比较
- 步骤程序?开始、结束、步进整数
- 如何使用ODEINT的标签系统为各种步进器类型做特定工作
- 通过替换模板<类步进器来要求>
- GDB 继续而不是步进