【发布时间】:2016-10-10 21:14:29
【问题描述】:
所以有人要求我在我的 C++ 课上制作游戏,我不久前上了 C++ 课,所以我有点生疏了。我记得你可以使用数组和列表?
游戏的重点是选择军队派系,选择军队类型,然后攻击某人。一旦我进入攻击部分,而不是从上一个菜单中检查所选派系的一堆 if 语句,我是否可以从程序的开头将派系实现到一个数组中,一旦我进入攻击阶段,运行通过数组并将用户选择的大陆与数组中的大陆进行比较,然后只保留匹配的输出?
如果有人可以发布一些代码或链接到提供此类示例的网站,将不胜感激,我想自己做,我并没有完全为我输入的东西。
#include <iostream>
using namespace std;
int main() {
// Declare variables
int menuInput = 0, continentInput = 0, armyInput = 0, actionInput = 0;
// Intro
cout << "WELCOME TO WAR" << endl;
// Display main menu
do {
cout << "1) Rules" << endl;
cout << "2) Play Game" << endl;
cout << "3) Quit" << endl;
cout << "Menu choice: ";
cin >> menuInput;
// if rules is selected
if (menuInput == 1) {
cout << endl;
cout << "RULES: " << endl;
cout << "1) Choose your player." << endl;
cout << "2) Choose your army type." << endl;
cout << "3) Choose to attack." << endl;
cout << endl;
}
// if game is selected
else if (menuInput == 2) {
cout << endl;
cout << "START" << endl;
// first do while loop, continent choice
do {
cout << "1) North America" << endl;
cout << "2) South America" << endl;
cout << "3) Europe" << endl;
cout << "4) Africa" << endl;
cout << "5) Asia" << endl;
cout << "6) Australia" << endl;
cout << "7) Antartica" << endl;
cout << "Choose your player from the list: ";
cin >> continentInput;
cout << endl;
// invalid display if selection not in range
if (continentInput <= 0 ||continentInput > 7) {
cout << "INVALID" << endl;
cout << endl;
}
} while (continentInput <= 0 || continentInput > 8);
// second do while loop, army type choice
do {
cout << "1) Army (Ground type forces)" << endl;
cout << "2) Navy (Sea type forces)" << endl;
cout << "3) Air Force (Air type forces)" << endl;
cout << "Choose your army type from the list: ";
cin >> armyInput;
cout << endl;
if (armyInput <= 0 || armyInput > 3) {
cout << "INVALID" << endl;
}
} while (armyInput <= 0 || armyInput > 3);
// third do while loop, who to attack
}
else if (menuInput == 3) {
cout << endl;
cout << "GAME OVER" << endl;
}
else {
// display invlaid input if number choice is not in given range
cout << "INAVLID INPUT" << endl;
}
} while (menuInput != 3);
return 0;
}
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
你学过课程了吗?如果您有一个包含玩家和军队类型成员的类对象,您可以设置、获取并执行操作,这会更好。 tutorialspoint.com/cplusplus/cpp_classes_objects.htm
-
@DigitalNinja 类还没有达到那个点,第一个“版本”应该以更基本的方式编码,但这就是我在对象方面的计划定向的。