这是我在c ++中的冒险游戏。我一直在尝试新事物以提高效率。我想知道是否有更有效的方法来设置关卡?到目前为止,我只是将级别定义为字符串,它似乎比它需要的效率更低。特别是对于if-statments,那些是聚集的。
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
string name;
short choice;
string choice2;
int health = 100;
// handling errors
string nope = "Are you H@CK3R, that command does not exist";
// handling directions
string west = "You went West. ";
string east = "You went East. ";
string south = "You went South. ";
string north = "Your went North. ";
// Handles the areas of the map
// Level 1
string north1 = "You are now standing in an empty clearing. There is a path going North, East, and West.";
string south1 = "You are now back where you started.";
string east1 = "There is nothing of significance this way.";
string west1 = "You see smoke off farther west. There is also a path to your North.";
// level 2
string north2 = "You have continued North, the trees are getting more thick. There is no longer a trail to the East and West.";
string south2 = "You are now back in the empty clearing.";
string east2 = "You cannot go this way.";
string west2 = "You cannot go this way.";
// level 3
// level 4
// level 5
// level 6
// level 7
// level 8
// level 9
// level 10
system("clear");
std::cout << "Please enter your name.\n";
cin >> name;
cout << "Welcome " << name << " to the [place holder]" << endl;
cout << "Type 1 to start, press 2 for credits, press 3 to quit." << endl;
std::cin >> choice;
// main game block
switch (choice) {
system("clear");
case 1:
system("clear");
while(1) {
cout << "You wake up on a beach, there are trees to your North, and open beach to your East and West." << endl;
cin >> choice2;
if (choice2 == "north") {
system("clear");
while(2) {
cout << north << north1 << endl;
cin >> choice2;
if(choice2 == "north"){
system("clear");
cout << north << north2 << endl;
cin >> choice2;
} else if("south") {
cout << south << south2 << endl;
break;
} else if("east") {
cout << east << east2 << endl;
break;
} else if("west") {
cout << west << west2 << endl;
break;
} else {
system("clear");
cout << nope << endl;
}
}
break;
}else if (choice2 == "south") {
system("clear");
cout << south << south1 << endl;
cin >> choice2;
break;
}else if (choice2 == "east") {
system("clear");
cout << east << east1 << endl;
cin >> choice2;
break;
}else if (choice2 == "west") {
system("clear");
cout << west << west1 << endl;
cin >> choice2;
break;
} else if (choice2 == "hacker") {
system("clear");
cout << "nope";
break;
}else {
system("clear");
cout << nope << endl;
}
}
break;
case 2:
system("clear");
cout << "The Village Part 2" << endl;
cout << "Coded ----- Kyle Sherman" << endl;
cout << "Coded ----- Phillip Noble" << endl;
cout << "Story ----- Pillip Noble" << endl;
cout << "Story ----- Kyle Sherman" << endl;
break;
case 3:
break;
}
cout << endl;
/**
// Handles displaying the stats and help
if(choice2 == "help") {
cout << "List of commands" << endl;
cout << "stats ----- Display the stats" << endl;
cout << "clear ----- Clears the screen" << endl;
cout << "north ----- Goes North" << endl;
cout << "south ----- Goes South" << endl;
cout << "east ------ Goes East" << endl;
cout << "west ------ Goes West" << endl;
}else if(choice2 == "stats") {
system("clear");
cout << name << "'s health: " << health << endl;
}else if(choice2 == "clear") {
system("clear");
}
**/
return 0;
}
答案 0 :(得分:2)
您可以使用责任链设计模式来实现这一目标。 去了解它:http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern
通过将链的第一个元素设置为链的最后一个元素的下一个元素,您将能够以循环方式进行条件检查。