我是一个C++的初学者,正在构建一个基于控制台的C++游戏。 有谁能帮助我理解枚举函数的作用,它们在这里的确切用途以及它们的用途吗? 考虑到我的初学者水平,任何帮助都将不胜感激。
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 40;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
enum edirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
edirecton dir;
很简单。 枚举
是从0开始的数字的占位符/单词。 就是这样。 您可以键入stop
-而不是0
,这使代码更具可读性。 您也可以将它与字符串和其他东西进行比较,但对于一开始来说,它只是这样。 我给你写了一个输入数字的简单代码:
#include <iostream>
#include <limits> //numeric_limits, streamsize...
using std::cin, std::cout, std::endl; //Don't specify the whole namespace, just the few you need
enum gameOver{STOP/* or 0*/, LEFT/* or 1*/, RIGHT/* or 2*/, UP/*or 3*/, DOWN/*or 4*/, EXIT/*or 5*/};
int main()
{
int input{}; //c++17 declaration - equals input = 0;
while(true){ //running forever until return 0
cout << "Type a number:\n" << endl;
while(!(cin >> input) || cin.bad()){ //check the Input format for integer the right way
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
switch (input) {
case STOP: //equals number 0
cout << "stop" << STOP << endl;
break;
case LEFT: //equals number 1
cout << "left" << LEFT << endl;
break;
case RIGHT: //equals number 2
cout << "right" << RIGHT << endl;
break;
case 3: //or just a number - equals UP
cout << "up" << UP << endl;
break;
case DOWN: //equals number 4
cout << "down" << DOWN <<endl;
break;
case EXIT: //equals number 5
cout << "exit" << EXIT << endl;
return 0;
default:
cout << "wrong input" << endl;
break;
}
}
}
如果您编写了几行以上的示例代码,只需使用using std::cin
或what指定您出于各种原因正在使用的代码。
还要始终检查输入是否有效。 我为您添加了一些简单的验证循环。
我用第二个枚举展开了它。 通常,您在include指令下声明它,但在函数之前声明它,以使em对整个程序计算,编译器可以将em放入。
#include <iostream>
#include <limits> //numeric_limits, streamsize...
using std::cin, std::cout, std::endl; //Don't specify the whole namespace, just the few you need
enum gameOver{STOP/* or 0*/, LEFT/* or 1*/, RIGHT/* or 2*/, UP/*or 3*/, DOWN/*or 4*/, EXIT/*or 5*/};
//you can also specify different enums. They still equals always the same numbers
enum selection{ZERO/* or 0*/, ONE/* or 1*/, TWO/* or 2*/};
int main()
{
int input{};
while(true){
cout << "Type a number:\n" << endl;
while(!(cin >> input) || cin.bad()){ //check the Input format for integer the right way
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
switch (input) {
//you can call em like a class
case gameOver::STOP: //equals number 0
cout << "stop" << STOP << endl;
break;
case selection::ONE: //equals number 1
cout << "left" << LEFT << endl;
break;
//or like this for simple programs
case TWO: //equals number 2
cout << "right" << RIGHT << endl;
break;
case 3: //or just a number - equals UP
cout << "up" << UP << endl;
break;
case DOWN: //equals number 4
cout << "down" << DOWN <<endl;
break;
case EXIT: //equals number 5
cout << "exit" << EXIT << endl;
return 0;
default:
cout << "wrong input" << endl;
break;
}
}
}