如何在c++骰子游戏中存储第一个值,并在下一步进行比较

How do I store the first value in a c++ dice game and compare it in the next move?

本文关键字:步进 比较 存储 c++ 游戏 第一个      更新时间:2023-10-16

我目前正在开发一款骰子游戏。用户先掷一对骰子,假设他掷了骰子,骰子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;
}