因此,我们受到老师的挑战,要求将这个简单的游戏制作成c++程序。
英语不是我的主要语言,但我会尽力解释。我是一个初学者,所以这些方法效率不高,但是我仍然为迄今为止的工作感到自豪。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int secret[3], i = 0, x, y, z, a, b, c, guess = 1, tries = 9;
bool correct = false;
srand(time(0));
do
{
secret[i] = rand() % 10;
i++;
} while (i <= 2);
x = secret[0];
y = secret[1];
z = secret[2];
//cout << x << y << z << endl; <--- for debugging purposes
cout << "=================================================\n\n";
cout << " I HAVE THREE SINGLE DIGIT NUMBERS\n\n";
cout << " YOU HAVE TEN TRIES TO GUESS ALL THREE DIGITS\n\n";
cout << "=================================================\n\n";
do
{
cout << "\n\nGuess the first digit.\n";
while (!(cin >> a))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please try again: \n";
}
cout << "\n\nGuess the second digit.\n";
cout << a;
while (!(cin >> b))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please try again: \n" << a;
}
cout << "\n\nGuess the third digit.\n";
cout << a << b;
while (!(cin >> c))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please try again: \n" << a << b;
}
cout << "\n\n====================================================\n\n";
if (tries == 0)
{
cout << "YOU RAN OUT OF GUESSES! \n";
cout << "The secret number is " << x << y << z << "!\n";
cout << "PLEASE RESTART THE PROGRAM TO TRY AGAIN!\n";
correct = true;
}
else if ((a == x) && (b == y) && (c == z))
{
cout << "YOU GUESSED THE SECRET NUMBER IN " << guess << " TRY / TRIES!\n";
cout << "CONGRATULATIONS!\n\n";
correct = true;
}
else if ((a == x) && (b == y) && (c != z))
{
cout << "You guessed TWO of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else if ((a == x) && (b != y) && (c != z))
{
cout << "You guessed ONE of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else if ((a == x) && (b != y) && (c == z))
{
cout << "You guessed TWO of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else if ((a != x) && (b == y) && (c == z))
{
cout << "You guessed TWO of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else if ((a != x) && (b == y) && (c != z))
{
cout << "You guessed ONE of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else if ((a != x) && (b != y) && (c == z))
{
cout << "You guessed ONE of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
else
{
cout << "You guessed NONE of the numbers correctly!\n";
cout << "You have " << tries << " tries left.\n";
correct = false;
}
cout << "\n====================================================\n\n";
guess++;
tries--;
} while (correct == false);
}
不过,在程序的这一部分我有一个小问题,cout << "\n\nGuess the first digit.\n";
while (!(cin >> a))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please try again: \n";
}
我可以输入任何无效的内容,它将正确地将其识别为无效输入。但是,如果我输入2位数字(例如22),则程序仍输入22,它只是接受它。我不确切知道程序的那部分是如何工作的,我只是复制粘贴了它。是否可以修改它,或者我的程序只接受一个数字,即0-9 ,并在输入两个数字时将输入标识为无效?
我知道这只是一个小麻烦,并没有真正破坏程序,但是如果我可以做得更好,那就太好了。如果可能的话,我只是希望它是完美的。
我猜是否有像_getche这样的整数,那会更好吗?
提前致谢。
最佳答案
while (!(cin >> a))
表示保持循环,而无法将输入转换为a
中的整数值-您可以添加其他条件:
while (!(cin >> a) || a < 0 || a > 9)
在后一种情况下,无需.clear()
和.ignore(...)
,但不会造成任何危害。如果您不熟悉它,
||
表示逻辑或,即英语的等效逻辑,例如:“a小于0或a大于9”。