使用二维阵列在地图/栅格上显示坐标

Using a 2d array to display coordinates on a Map/Grid

本文关键字:地图 坐标 显示 阵列 二维      更新时间:2023-10-16

我正试图在地图/网格上放置裁剪。裁剪是需要在地图边界中显示的字符串。我需要使用一个2d数组来显示将在地图上形成裁剪形状的各个字符。这是我到目前为止的代码,但当它们显示时,它并没有给我正确的裁剪形状。

bool Crop::place(char map[MAPL][MAPW],int x_crop,int y_crop)const{
cout << 'n';
cout << setw(24) << right << "11111111112n";
cout << setw(24) << right << "12345678901234567890n";
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 << "11111111112n";
cout << setw(24) << right << "12345678901234567890n";
return true;
}

下面是输出应该是什么样子的示例。它有两个作物,"c"answers"p"在地图上,是20宽乘10高的

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

看起来是一个横向错误。循环中:

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;
}

您每次打印一列,而不是一次打印一行(列的主要顺序),所以它是横向打印字段。如果切换MAPW和MAPL(并切换x和y以使变量描述此更改),则执行

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

您将一次打印一行(行主要顺序),并打印出字段中的每个空格。