C++: как получить повторный бросок для отображения нового набора случайных чисел в игре в кости?
Я почти закончил с этой программой, кроме того, что я не могу понять, как получить новый набор случайных чисел, когда пользователь выбирает повторный бросок (rolling R при первом запросе). Ниже приведен мой код-может ли кто-нибудь помочь мне выяснить, где я ошибся с синтаксисом, семенем и т. д.?
Что я уже пробовал:
/* Program Name: P30DiceIfElseLoopRandom.cpp Name: Amy Kaskowitz Date Started: 5/24/2018 ; Date completed: Program Description: Write a Dice Game program that would result in output like this: Beat the computer! You rolled a 2 and a 1 (total = 3) Do you want to keep those? (y or n) n Rolling again.... You rolled a 4 and a 1 (total = 5) Do you want to keep those? (y or n) n Rolling again.... You rolled a 1 and a 1 (total = 2) Do you want to keep those? (y or n) y The computer rolled 6 and 3 (total = 9) So sorry. You lose. === Hint: use random numbers b/w 1-6, make 4 random numbers */ #include <stdlib.h> /// need this for srand() -- for random numbers #include <time.h> /// need this for time() -- time #include <iostream> /// need this for cout<< and cin>> using namespace std; /// need this for cout<< and cin>> int main() { int iseed = (int)time(0); srand(iseed); cout << "Beat the computer! \n"; int roll1 = 1+rand()%6; /// make a random number for die # 1 for user int roll2 = 1+rand()%6; /// make a random number for die # 2 for user int UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user char keep; do { cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n"; cout << "\n"; do { cout << "Would you like to keep this total, or roll again? \n"; cout << "\n"; cout << "Enter \"K\" for keep and \"R\" for roll again: \n"; cin >> keep; if (keep != 'K' && keep != 'R') { cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl; cout << "\n"; } } while(keep != 'K' && keep != 'R'); if (keep == 'R') { cout << "You chose R--let's roll again. \n"; } else { cout << "Great! Your total is " << UserRoll << "\n"; } } while (keep == 'R'); int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n"; cout << "\n"; if (ComputerRoll < UserRoll) { cout << "Congratulations! You won! \n"; } if (ComputerRoll > UserRoll) { cout << "Sorry. You lose. \n"; } if (ComputerRoll == UserRoll) { cout << "It's a tie. \n"; } return 0; } /* SAMPLE RUNS: ------------ Beat the computer! You rolled a 4 and a 6 for a total of: 10 Would you like to keep this total, or roll again? Enter "K" for keep and "R" for roll again: R You chose R--let's roll again. You rolled a 4 and a 6 for a total of: 10 Would you like to keep this total, or roll again? Enter "K" for keep and "R" for roll again: K Great! Your total is 10 The computer rolled a 4 and a 6 for a total of: 10 It's a tie. Process returned 0 (0x0) execution time : 8.763 s Press any key to continue. */