2014-10-11 42 views
0

我需要將2d數組顯示爲地圖。 g_length和w_width是從x和y點開始的多長時間。需要創建一個需要放置在map.x_crop上的裁剪的寬度和長度的循環,而y_crop是裁剪應該創建的x和y座標。 w_width是寬度,g_length是位於類中的作物的長度或高度。將2d數組顯示爲地圖

bool Crop::place(char map[MAPL][MAPW],int x_crop,int y_crop)const{ 

int y_total =y_crop; 
y_total = y_crop + g_length; 

for(int x=0; x < MAPW;x++){ 

    if(x==x_crop){ 
     for(int b=0; b < w_width;b++){ 
      if(y_total==y_crop){ 
       map[x][y_crop] = PlantType.symbol(); 
       x++; 
       y_crop++; 
      }else{ 
       b=w_width; 
      } 
     x++;   
     } 
    } 
} 


cout << '\n'; 
cout << setw(24) << right << "11111111112\n"; 
cout << setw(24) << right << "123456789\n"; 
cout << " " << setw(21) << setfill('-') << left << '+' << right << '+' << setfill (' ') << endl; 



for(int x=0; x < MAPW;x++){ 
    cout << setw(2) << right << x+1 << "|"; 
    for(int y=0; y < MAPL;y++){ 
     cout << map[x][y]; 

    } 
    cout << "|" << endl; 
} 

cout << " " << setw(21) << setfill('-') << left << '+' << right << '+' << setfill (' ') << endl; 
cout << setw(24) << right << "11111111112\n"; 
cout << setw(24) << right << "123456789\n"; 

return true;} 

OUT輸出繼承人應該如何看起來像:

  11111111112 
    123456789
    +--------------------+ 
1|     | 
2|     | 
3| cc    | 
4| cc    | 
5| cc    | 
6| cc    | 
7|   pppppppp | 
8|   pppppppp | 
9|   pppppppp | 
10|     | 
    +--------------------+ 
      11111111112 
    123456789

>

+0

這是什麼問題? – Jarod42 2014-10-11 02:00:10

+0

@ Jarod42 for循環應該如何獲得問題中提供的輸出? – Sobasofly 2014-10-11 02:07:46

回答

1

不要使循環過於複雜:

for (int x = x_crop; x < x_crop + w_width; ++x) { 
    for (int y = y_crop; y < y_crop + g_height; ++y) { 
     map[x][y] = PlantType.symbol(); 
    } 
} 

一些其他注意事項:

  • 你可能要考慮,以保持地圖的邊界內
  • 讓您的代碼中的地圖索引一致將檢查參數x_cropy_crop,在循環你被map[x][y]索引,但在參數聲明char map[MAPL][MAPW]暗示它應該被索引map[y][x]