一些背景:这个程序读取石头的线条,并将它们放到地图上。
程序应该首先要求以整数的形式列出石头的行数。然后,程序将以下列格式将行的位置扫描为一个由四个整数组成的组:
行列长度值
行和列表示到地图上放置的块的水平线中最左边的块。长度告诉你在这条水平线上应该有多少块石头。在这个例子中,假设第四个整数总是1代表一个石头。
示例: CommandMeaning
0 0 5 1放置一行石头,从开始到结束。这条线上的所有5个方块将被设置为1(石头)。
我的问题是试图在长度方面进行编码,但我找不出模式:
。
#define SIZE 15
#define EMPTY 0
#define STONE 1
void printMap(int map[SIZE][SIZE], int playerX);
int main (void) {
// This line creates our 2D array called "map" and sets all
// of the blocks in the map to EMPTY.
int map[SIZE][SIZE] = {EMPTY};
// This line creates out playerX variable.
int playerX = SIZE / 2;
printf("How many lines of stone? ");
int linesOfStone;
scanf("%d", &linesOfStone);
printf("Enter lines of stone:\n");
int rowPos;
int columnPos;
int stoneLength;
int stoneValue;
int i = 0;
while (i < linesOfStone) {
scanf("%d %d %d %d", &rowPos, &columnPos, &stoneLength, &stoneValue);
map[rowPos][columnPos]++; //pos for position
//ERROR: This was my attempt to incorporate the length aspect, i think my logic got lost along the way...
int j = 0;
while (j < stoneLength) {
rowPos++;
j++;
}
i++;
}
printMap(map, playerX);
return 0;
}
// Print out the contents of the map array. Then print out the player line
// which will depends on the playerX variable.
void printMap(int map[SIZE][SIZE], int playerX) {
// Print values from the map array.
int i = 0;
while (i < SIZE) {
int j = 0;
while (j < SIZE) {
printf("%d ", map[i][j]);
j++;
}
printf("\n");
i++;
}
// Print the player line.
i = 0;
while (i < player发布于 2020-07-08 11:25:05
将您的第二个while循环替换为下面一个:
for (int j=0; j < stoneLength; j++) // Iterate over all affected map tiles
map[rowPos + j][columnPos] = 1; // Make this map tile STONE这个集合将从[rowPos][columnPos]到[rowPos + stoneLength - 1][columnPos]的所有映射块
https://stackoverflow.com/questions/62793423
复制相似问题