Как мне сохранить первое значение в игре в кости c ++ и сравнить его в следующем ходу?

В настоящее время я работаю над игрой в кости. Где пользователь сначала бросает пару костей, скажем, он бросил, и кости 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;
}

-1

Решение

После нашего чата / недоразумений с комментариями я позволил себе скопировать ваш код и изменить его (как можно меньше, чтобы сохранить ваш стиль кодирования — я бы не рекомендовал этот стиль для любых будущих проектов) для получения желаемых результатов. Дайте мне знать, если это работает (простое тестирование показало, что это работает, возможно, пропустили некоторые другие причуды)

#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;
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector