是什么阻止随机数生成器在玩家每次开始新游戏时随机化?
what is stopping the random num generator from randomizing every time a player starts a new game?
{
int i,game = 0,guess,count = 0;
int rando;
srand (time(0));
rando = rand() % 50 + 1;
cout << "****Welcome To The Game****\n";
cout << "1: Start the game\n";
cout << "2: End the game\n";
cin >> game;
while (game != 2 && count != 11)
{
cout << "Enter a number between 1 and 50: ";
cin >> guess;
count++;
if(guess == rando)
{
cout << "you got it!\n";
cout << "would you like to play again?\n";
cin >> game;
}
else if(guess < rando)
{
cout << "too low, try again " << endl;
}
else if(guess > rando)
{
cout << "Too high, try again" << endl;
}
if (count == 11)
{
cout << "too many guesses. the number was: " << rando << "." << endl;
cout << "would you like to play again?\n";
cin >> game;
}
if (game == 2)
{
cout << "@@@ Thank you. See you next time.@@@\n";
}
}
return 0;
}
当我开始游戏时,每次我 运行 程序都会生成一个新数字,但当我要求再次玩时却不会。我试着把它放在一边,但这弄乱了代码。第一句话中的第二个选项如何实现?
int randomGame()
{
int gameMenuChoice = 0;
srand(time(0));
std::cout << "****Welcome To The Game****\n";
std::cout << "1: Start the gameMenuChoice\n";
std::cout << "2: End the gameMenuChoice\n";
std::cin >> gameMenuChoice;
while (gameMenuChoice != 2)
{
const int rando = rand() % 50 + 1;
int guess = 0;
for (int count = 0; count < 11 && guess != rando; ++count)
{
std::cout << "Enter a number between 1 and 50: ";
std::cin >> guess;
if (guess < rando)
{
std::cout << "too low, try again \n";
}
else if (guess > rando)
{
std::cout << "Too high, try again\n";
}
}
if (guess == rando)
{
std::cout << "you got it!\n";
}
else
{
std::cout << "too many guesses. the number was: " << rando << ".\n";
}
std::cout << "would you like to play again?\n"; // I think people will enter Y for yes here.
std::cin >> gameMenuChoice;
}
std::cout << "@@@ Thank you. See you next time.@@@\n";
return 0;
}
我已经拆分了你的菜单和游戏例程循环并简化了逻辑。
{
int i,game = 0,guess,count = 0;
int rando;
srand (time(0));
rando = rand() % 50 + 1;
cout << "****Welcome To The Game****\n";
cout << "1: Start the game\n";
cout << "2: End the game\n";
cin >> game;
while (game != 2 && count != 11)
{
cout << "Enter a number between 1 and 50: ";
cin >> guess;
count++;
if(guess == rando)
{
cout << "you got it!\n";
cout << "would you like to play again?\n";
cin >> game;
}
else if(guess < rando)
{
cout << "too low, try again " << endl;
}
else if(guess > rando)
{
cout << "Too high, try again" << endl;
}
if (count == 11)
{
cout << "too many guesses. the number was: " << rando << "." << endl;
cout << "would you like to play again?\n";
cin >> game;
}
if (game == 2)
{
cout << "@@@ Thank you. See you next time.@@@\n";
}
}
return 0;
}
当我开始游戏时,每次我 运行 程序都会生成一个新数字,但当我要求再次玩时却不会。我试着把它放在一边,但这弄乱了代码。第一句话中的第二个选项如何实现?
int randomGame()
{
int gameMenuChoice = 0;
srand(time(0));
std::cout << "****Welcome To The Game****\n";
std::cout << "1: Start the gameMenuChoice\n";
std::cout << "2: End the gameMenuChoice\n";
std::cin >> gameMenuChoice;
while (gameMenuChoice != 2)
{
const int rando = rand() % 50 + 1;
int guess = 0;
for (int count = 0; count < 11 && guess != rando; ++count)
{
std::cout << "Enter a number between 1 and 50: ";
std::cin >> guess;
if (guess < rando)
{
std::cout << "too low, try again \n";
}
else if (guess > rando)
{
std::cout << "Too high, try again\n";
}
}
if (guess == rando)
{
std::cout << "you got it!\n";
}
else
{
std::cout << "too many guesses. the number was: " << rando << ".\n";
}
std::cout << "would you like to play again?\n"; // I think people will enter Y for yes here.
std::cin >> gameMenuChoice;
}
std::cout << "@@@ Thank you. See you next time.@@@\n";
return 0;
}
我已经拆分了你的菜单和游戏例程循环并简化了逻辑。