试图学习如何制作地图移动系统,这是我已经走了多远:
这是我打算遍历的地图,每个位置打开一个唯一的区域,数字值对应输入,如N,W,E,S
int Coordinates[3][3]{
{0, 1, 2} ,
{7, 9, 3} ,
{6, 5, 0}
};
map<string,int> Move_Table;
Move_Table["n"]=1;
Move_Table["ne"]=2;
Move_Table["e"]=3;
Move_Table["se"]=4;
Move_Table["s"]=5;
Move_Table["sw"]=6;
Move_Table["w"]=7;
Move_Table["nw"]=8;
//Starting position updates after value is entered
int position = Coordinates[1][1];
string command;
cout <<"Move: \n";
cin >> command;
//toLower()
transform(command.begin(), command.end(), command.begin(), ::tolower);
//Check if command is validly within command map (will update this later... focusing on the array bit)
if (Move_Table.find(command) == Move_Table.end()){
cout << "Invalid move command\n";
}
else {
int Result = Move_Table.find(command)->second;
cout << Result;
//This should allow me to iterate through a one dimensional array, but I need to get the indices of a two dimensional one
auto it = std::find( std::begin( Coordinates ), std::end( Coordinates ), Result );
if ( it != std::end( Coordinates ) )
{
std::cout << "The index of the element with value 7 is "
<< std::distance( std::begin( Coordinates ), it )
<< std::endl;
}
}
之后,我用我找到的新x和y坐标更新玩家位置。因此,这些坐标暂时是特定于房间的,我还没想过要绘制这张地图的更宏大的方法,但这应该作为测试。 (建议使用图表,而不是那个想法)
基本上,如果结果想要搜索多个1(北),它应该返回一个(x = 0,y = 1)
很抱歉没有缩进,我在repl.it上编码。
答案 0 :(得分:1)
只需使用简单的for
循环:
int x, y;
int found = 0;
for (x = 0; !found && x < 3; x++) {
for (y = 0; y < 3; y++) {
if (Coordinates[x][y] == search_value) {
found = 1;
break;
}
}
}
if (found) {
printf("x = %d y = %d\n", x, y);
} else {
printf("Not found\n");
}