我正在尝试使用while循环重新启动游戏,但是在较大的while循环内的第一个while循环之后,游戏将无限期重复。我假设有一种更简单的方法来创建循环,以便在游戏结束时响应Y / N答案。我也在尝试使其保持
int newpoints
在下一个循环的开始。
int main() {
//Declarations for user input valued
char ynend = 'y';
int usernum;
int newpoints;
int userpoints = 0;
char userlet;
char ynstart = 'n';
std::string usercol;
//Game loop
while (ynend == 'y'){
//User input interface
while (ynstart == 'n') {
std::cout << "---------------------- Probability ----------------------\n" ;
std::cout << "---------------------- Roulette! ----------------------\n" ;
std::cout << "\nHow to play:\nPlace a bet, then pick a letter, color, and number. Type all answers in lowercase. The house will pick a random set too. We have to match for you to win. Winning rules are as follows:\n If you we don't match at all, you lose your bet.\n If we only match once, you keep your bet and it stays the same.\n If we match twice, your bet doubles.\n If we match all three times, your bet quadruples!\n";
std::cout << "\nAre you ready to play? Y/N \n" ;
std::cin >> ynstart ;
} ;
std::cout << "\n\nHow much are you betting?\n" ;
std::cout << "You already have $" << newpoints ;
std::cin >> userpoints;
userpoints + newpoints;
std::cout << "Pick a number between 1 & 4: " ;
std::cin >> usernum ;
std::cout << "Pick a letter between A & D: " ;
std::cin >> userlet ;
std::cout << "Pick a color: " ;
std::cout << "\n Black \n Red \n Green \n Blue \nYour color choice: " ;
std::cin >> usercol ;
//Roulette with random number generators, two numbers between 1 and 20. Int to char conversion further down.
srand (time(NULL));
int number = rand() % 4 + 1;
int letter = rand() % 4 + 1;
int color = rand() % 4 + 1;
/*Switch for determining if usernum value is between 1 & 4
switch (usernum){
case 1:
return true;
break;
case 2:
return true;
break;
case 3:
return true;
break;
case 4:
return true;
break;
default:
std::cout << "There was an error, please restart by pressing 'run'. \n" ;
return false;
break;
} ;
*/
//Roulette number display
std::cout << "\n\nThe house's number is: " << number << ". You picked: " << usernum << "\n";
//Roulette letter display, uses switch to convert int-letter integer to word
switch (letter){
case 1:
std::cout << "The house's letter is A. You picked: " << userlet << "\n";
break;
case 2:
std::cout << "The house's letter is B. You picked: " << userlet << "\n";
break;
case 3:
std::cout << "The house's letter is C. You picked: " << userlet << "\n";
break;
case 4:
std::cout << "The house's letter is D. You picked: " << userlet << "\n";
break;
}
//Roulette color display, uses switch to convert int-color integer to word
switch (color){
case 1:
std::cout << "The house's color is black. You picked: " << usercol << "\n\n";
break;
case 2:
std::cout << "The house's color is red. You picked: " << usercol << "\n\n";
break;
case 3:
std::cout << "The house's color is green. You picked: " << usercol << "\n\n";
break;
case 4:
std::cout << "The house's color is blue. You picked: " << usercol << "\n\n";
break;
}
//Variables for text conversion and calculation
int calclet = 0;
int calccol = 0;
//Converting user-inputter letter to integer variable
switch (userlet){
case 'a':
calclet = calclet + 1;
break;
case 'b':
calclet = calclet + 2;
break;
case 'c':
calclet = calclet + 3;
break;
case 'd':
calclet = calclet + 4;
break;
default :
//std::cout << "There was an error, please restart by pressing 'run'. \n" ;
//return false;
break;
} ;
//Converting user-inputted color to integer-variable
if (usercol == "black") {
calccol = calccol + 1;
} else if (usercol == "red") {
calccol = calccol + 2;
} else if (usercol == "green"){
calccol = calccol + 3;
} else if (usercol == "blue"){
calccol = calccol + 4;
} else {
//std::cout << "There was an error, please restart by pressing 'run'. \n" ;
//return false;
};
//If statement for determining win amount
if (calccol == color && calclet == letter && usernum == number) {
newpoints = userpoints * 4 ;
std::cout << "You got all three guesses correct! You won $" << newpoints << "!\n" ;
//Below is 2/3 correct elseif statements
//
} else if (calccol == color && calclet == letter && usernum != number) {
newpoints = userpoints * 2 ;
std::cout << "You only got two out of three guesses correct! You won $" << newpoints << "!\n" ;
} else if (calccol == color && calclet != letter && usernum == number) {
newpoints = userpoints * 2 ;
std::cout << "You only got two out of three guesses correct! You won $" << newpoints << "!\n" ;
} else if (calccol != color && calclet == letter && usernum == number) {
newpoints = userpoints * 2 ;
std::cout << "You only got two out of three guesses correct! You won $" << newpoints << "!\n" ;
//Below is the 1/3 correct elseif statements
//
} else if (calccol == color && calclet != letter && usernum != number) {
newpoints = userpoints * 1 ;
std::cout << "You only got one out of three guesses correct, your bet stays the same!\n" ;
} else if (calccol != color && calclet != letter && usernum == number) {
newpoints = userpoints * 1 ;
std::cout << "You only got one out of three guesses correct, your bet stays the same!\n" ;
} else if (calccol != color && calclet == letter && usernum != number) {
newpoints = userpoints * 1 ;
std::cout << "You only got one out of three guesses correct, your bet stays the same!\n" ;
//Below is all incorrect elseif
//
} else if (calccol != color && calclet != letter && usernum != number) {
newpoints = userpoints * 0 ;
std::cout << "You didn't guess anything correct, you've lost your bet!\n" ;
//Below is incase of incorrect input
//
} else {
std::cout << "There was an error, please restart.\n" ;
} ;
//Final statements before restart option
std::cout << "\n\nStarting bet amount: $" << userpoints;
std::cout << "\nCurrent bet amount: $" << newpoints;
std::cout << "\n\nThanks for playing! Would you like to play again? Y/N \n" ;
std::cin >> ynend ;
};
int totpoints = 0 ;
std::cout << "\n\nYou walk away with $" << totpoints ;
return 0;
}
答案 0 :(得分:0)
根据发布的代码,您没有为外部循环设置条件。
直截了当:由于您的初始标志设置,第一次运行时,您可以进入第一循环和第二循环。
进行某些操作后进入内部循环时,您可以更改ynstart的值,因此可以退出内部循环;否则,请执行以下操作。但是您应该通过外部循环来实现目标:您应该放置一个if语句,该语句从用户那里读取一个假设的y / n,以便if语句将知道要在ynend上进行的设置并最终重新启动游戏或停止运行。
我仍然对代码的第二次执行有疑问,因为ynstart仍设置为“ y”,因此第二次循环可能不会运行。万一放一个
ynstart = 'n'
在第二个循环开始之前。