我想創建一個ASCII世界,但是我無法在函數之間傳遞二維數組。這是一個20×20的陣列,我想隨意放置房屋。這個數組不會像我想要的那樣通過,我的教程告訴我全局變量是邪惡的,所以沒有這些的解決方案會很好。將二維數組傳遞到函數
using namespace std;
void place_house(const int width, const int height, string world[width][length])
{
int max_house = (width * height)/10; //One tenth of the map is filled with houses
int xcoords = (0 + (rand() % 20));
int ycoords = (0 + (rand() % 20));
world[xcoords][ycoords] = "@";
}
int main(int argc, const char * argv[])
{
srand((unsigned)time(NULL));
const int width = 20;
const int height = 20;
string world[width][height];
string grass = ".";
string house = "@";
string mountain = "^";
string person = "Å";
string treasure = "$";
//Fill entire world with grass
for (int iii = 0; iii < 20; ++iii) {
for (int jjj = 0; jjj < 20; ++jjj) {
world[iii][jjj] = ".";
}
}
place_house(width, height, world);
for (int iii = 0; iii < 20; ++iii) {
for (int jjj = 0; jjj < 20; ++jjj) {
cout << world[iii][jjj] << " ";
}
cout << endl;
}
}
如果數組中的每個字符串只包含一個字符,則應該使用'char'的二維數組,而不是'string'的二維數組。 – 2013-05-01 14:44:30