我正在尝试编写一个版本的Picolo移动的饮酒游戏应用程序,供我和我的朋友们玩(它看起来很简单,我们厌倦了应用程序中的问题,所以我们希望能够编写自己的问题)。它的工作方式应该是提示玩家的数量,随机化问题的顺序,然后一个接一个地问。我试着使用cin.ignore()来等待下一个问题,直到用户按下回车键。除了第一次迭代之外,它对每次迭代都有效。下面是源代码:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<algorithm>
using std::random_shuffle;
#include<fstream>
using std::ifstream;
#include<cstdlib>
#include<string>
using std::string;
#include<time.h>
const int numQuestions = 19;
int vir6rounds;
bool activeVir6 = false;
int v6counter = 0;
void getQuestion(int questionNumber, string players[], int numPlayers);
int getRandom();
int main()
{
srand(time(0));
// Create array of player names based on entered size
cout << "Enter \"quit\" to end game" << endl;
cout << "How many players?" << endl;
int numPlayers;
cin >> numPlayers;
cout << "Enter players' names: " << endl;
string players[numPlayers];
for (int i = 0; i < numPlayers; i++)
cin >> players[i];
bool loop = true;
int qArray[numQuestions];
// Populate array
for (int i = 0; i < numQuestions; i++)
{
qArray[i] = i;
}
while (loop)
{
// Reshuffle array each time game is looped
random_shuffle(qArray, qArray + numQuestions);
for (int i = 0; i < numQuestions; i++)
{
// Choose 3 random player names from array
// Changes each time a new question is generated
random_shuffle(players, players + numPlayers);
getQuestion(qArray[i], players, numPlayers);
cin.ignore(); // Problem here
}
cout << "Game over; press enter to play again" << endl;
cin.ignore();
}
}
void getQuestion(int questionNumber, string players[], int numPlayers)
{
if (activeVir6 == true)
{
v6counter++;
if (v6counter == vir6rounds)
{
cout << "Pong loser can speak again." << endl;
cin.ignore();
activeVir6 = false;
v6counter = 0;
}
}
string player1 = players[0];
string player2 = players[1];
string player3 = players[2];
switch (questionNumber)
{
case 0:
cout << "Going around, Play rock paper scissors with the player to your left." << endl;
cout << "Losers take two drinks." << endl;
break;
case 1:
cout << player1 << ", who would win in a fight? " << player2 << " or " << player3 << "?";
cin.ignore();
cout << "Loser takes two drinks." << endl;;
break;
case 2:
cout << player1 << " chooses the next song" << endl;
break;
case 3:
cout << "Whoever took a shower the least recently has to take a drink" << endl;
break;
case 4:
cout << player1 << ", take as many penalties as you want.";
cin.ignore();
cout << "However many you took, " << player2 << " has to take double." << endl;
break;
case 5:
cout << "Everyone switch drinks to the left" << endl;
break;
case 6:
cout << "virus: " << player1 << " versus " << player2 << " in pong." << endl;
cout << "loser can't speak until otherwise" << endl;
activeVir6 = true;
vir6rounds = getRandom();
break;
case 7:
cout << player1 << " take as many penalties as the youngest players age.\nIf you're the youngest, everyone else take 2." << endl;
break;
case 8:
cout << player1 << ", buy " << player2 << " a red bull or take 15 penalties" << endl;
break;
case 9:
cout << player1 << ", let " << player2 << " look through your snap memories or take 8 penalties" << endl;
break;
case 10:
cout << player1 << " has to say yes to " << player2 << " for 5 minutes or take 8 penalties" << endl;
break;
case 11:
cout << player1 << ", show off ur best cartwheel" << endl;
break;
case 12:
cout << player1 << ", change clothes with " << player2 << " or they pick how many penalties you take" << endl;
break;
case 13:
cout << player1 << ", pick someone new to go on aux or they take 5 penalties" << endl;
break;
case 14:
cout << player1 << ", cook " << player2 << " a meal or take 9 penalties" << endl;
break;
case 15:
cout << player1 << ", pick two people to shotgun" << endl;
break;
case 16:
cout << player1 << ", let " << player2 << " paint ur nails or take 10 penalties" << endl;
break;
case 17:
cout << player1 << ", every time you talk to " << player2 << " take 3 penalties until otherwise" << endl;
break;
case 18:
cout << player1 << ", kill yourself or take a penalty." << endl;
break;
default:
cout << "question has not been defined yet" << endl;
}
return;
}
下面是一些示例输入。其他一切似乎都运行良好;我遇到的唯一问题是,在输入名字之后,程序在正确工作之前同时打印前两行(在本例中,“helen,buy john a red bull or take 15 penalty”和“dylan choose the next song”)。
/*
Enter "quit" to end game
How many players?
7
Enter players' names:
john
kevin
mark
stacy
helen
jenna
dylan
helen, buy john a red bull or take 15 penalties
dylan chooses the next song
mark, let stacy look through your snap memories or take 8 penalties
helen has to say yes to mark for 5 minutes or take 8 penalties
Everyone switch drinks to the left
*/
我并不太关心代码的效率或者兰德()函数的典型缺陷,因为这是一个低风险的游戏,我主要关心的是让它一次打印出一行代码,但是,作为一个新的、公认的蹩脚的程序员,所有的建议都是受欢迎的。
1条答案
按热度按时间sxpgvts31#
第一次迭代不会等待
cin.ignore()
(顺便说一句,这不是等待按下ENTER键的好方法),因为在读入最后一个球员的名字后,输入缓冲区中仍然存在一个换行符。尝试使用
std::getline()
代替operator>>
来读取每个玩家的名字,它会自动为你丢弃最后的换行符。Why does std::getline() skip input after a formatted extraction?
此外,
string players[numPlayers];
不是标准C++:Why aren't variable-length arrays part of the C++ standard?
请改用
std::vector
。