首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环数组: While循环和2D数组

循环数组: While循环和2D数组
EN

Stack Overflow用户
提问于 2020-07-08 11:08:13
回答 1查看 1.4K关注 0票数 0

一些背景:这个程序读取石头的线条,并将它们放到地图上。

程序应该首先要求以整数的形式列出石头的行数。然后,程序将以下列格式将行的位置扫描为一个由四个整数组成的组:

行列长度值

行和列表示到地图上放置的块的水平线中最左边的块。长度告诉你在这条水平线上应该有多少块石头。在这个例子中,假设第四个整数总是1代表一个石头。

示例: CommandMeaning

0 0 5 1放置一行石头,从开始到结束。这条线上的所有5个方块将被设置为1(石头)。

我的问题是试图在长度方面进行编码,但我找不出模式:

  • ,我们只允许使用we循环,对于循环不允许使用

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-08 11:25:05

将您的第二个while循环替换为下面一个:

代码语言:javascript
复制
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]的所有映射块

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62793423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档