用 house 尝试编写在正确的窗口中写入“#”的函数来关闭游戏

问题描述 投票:0回答:1
char house =    
      "                                             ______________          "
      "                                            |______________|         "
      "      _______________________________________|            |_____     "
      "     '                                       |____________|     `    "
      "    |                                                           |    "
      "    '-----------------------------------------------------------'    "
      "    |           1                 2                 3           |    "
      "    |     +-----------+     +-----------+     +-----------+     |    "
      "    |     |           |     |           |     |           |     |    "
      "    |     |           |     |           |     |           |     |    "
      "    |     |           |     |           |     |           |     |    "
      "    |     +-----------+     +-----------+     +-----------+     |    "
      "    |           4                 5                 6           |    "
      "    |     +-----------+     +-----------+     +-----------+     |    "
      "    |     |           |     |           |     |           |     |    "
      "    |     |           |     |           |     |           |     |    "
      "  _ |     |           |     |           |     |           |     |    "
      " |#||     +-----------+     +-----------+     +-----------+     |    "
      " |_||           7                 8                 9           |    "
      "  `-|     +-----------+     +-----------+     +-----------+     |    "
      "    -     |           |     |           |     |           |     |    "
      "    '     |           |     |           |     |           |     |    "
      "    '     |           |     |           |     |           |     |    "
      "   o'     +-----------+     +-----------+     +-----------+     |    "
      "    '                                                           |    "
      "____'___________________________________________________________'____";

  int state[] = {1, 1, 0,
                 1, 1, 0,
                 1, 0, 0}; 

/* Updates the graphics for the window @ coordinates (x, y) to match the
 * `state` array.
 *
 *   This function modifies the `house` array by updating the characters
 *   inside the window located at the zero indexed coordinates (x, y) to
 *   match the window's state in the `state` array.  If the window's
 *   state is 1, then the window is filled with the '#' character.
 *   Likewise, if the window's state is 0 in the `state` array, the
 *   window is filled with the ' ' character.
 *
 * Parameters:
 *   house -- pointer to characters representing the house
 *
 *   state -- pointer to the game state array
 *
 *       x -- the horizontal coordinate of the window for which the
 *            state will be updated (zero indexed, with 0 being
 *            the left column and 2 being the right column)
 *
 *       y -- the vertical coordinate of the window for which the
 *            state will be updated (zero indexed, with 0 being
 *            the top row and 2 being the bottom row)
 */

void window_update_graphics(char *house, const int *state, int x, int y)

我已经为此工作了几天,甚至无法完成第一步,请有人帮助我。我只是不知道此时该做什么,我无法让它填充所有正确的窗口。下面的函数调用上面的函数“window_update_graphics”,该函数填充与状态数组中的 1 相对应的窗口。我希望我已经提供了所有必要的信息来提供帮助。这是在 C 中。

谢谢您!

void house_init(char *house, const int *state)
{
  int x, y;

  for (y = 0; y < 3; y++)
    for (x = 0; x < 3; x++)
      window_update_graphics(house, state, x, y);
}

这就是我现在的位置:

int idx;
  int i;
  int j;
  idx = HOUSE_WIDTH * 8 + 11;
  x = idx % HOUSE_WIDTH;
  y = idx / HOUSE_WIDTH;

  int windowStartIndex = HOUSE_WIDTH * y + x;
  // Fill the window with "#" if the corresponding state is 1, otherwise leave it unchanged
  for (i = 0; i < WINDOW_HEIGHT; i++)
  {
    for (j = 0; j < WINDOW_WIDTH; j++)
    {
      int currentIdx = windowStartIndex + i * HOUSE_WIDTH + j;
      if (state[(3 * i) + j] == 1)
      {
        house[currentIdx] = '#';
      }
      // You can add an else statement here if you want to handle other cases
    }
  }
}
c for-loop char 2d-games puzzle
1个回答
0
投票

我会在房子里添加一些东西。

  • ""
    (空字符串)表示下一个字符串是 s 行窗口。
  • NULL 表示房子的尽头

它将是通用且美好的。

extern const char *house[];
extern const char *house1[];

  int state[][3] = {{1, 1, 0},
                    {1, 1, 0},
                    {1, 0, 0}}; 

  int state1[][5] ={{1, 1, 0, 1, 0},
                    {1, 1, 1, 0, 1},
                    {1, 0, 1, 0, 1},
                    {0, 1, 0, 1, 0},
                    {1, 0, 0, 1, 1}}; 



void printHouse(FILE *fo, size_t windows, int (*state)[windows], const char **house)
{
    while(*house)
    {
        const char *line = *house;
        if(!*line)
        {
            size_t window = 0;
            house++;
            line = *house;
            while(*line)
            {
                if(*line == '@') fputc(state[0][window++] ? 'X' : ' ', fo);
                else fputc(*line, fo);
                line++;
            }
            state++;
            fputc('\n', fo);
        }
        else {fputs(line, fo); fputc('\n', fo);}
        house++;
    }
}

int main(void)
{
    printHouse(stdout, 3, state, house);
    printHouse(stdout, 5, state1, house1);
}


const char *house[] =    
     {"                                             ______________          ",
      "                                            |______________|         ",
      "      _______________________________________|            |_____     ",
      "     '                                       |____________|     `    ",
      "    |                                                           |    ",
      "    '-----------------------------------------------------------'    ",
      "    |           1                 2                 3           |    ",
      "    |     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |    ",
      "",
      "    |     |     @     |     |     @     |     |     @     |     |    ",
      "    |     |           |     |           |     |           |     |    ",
      "    |     +-----------+     +-----------+     +-----------+     |    ",
      "    |           4                 5                 6           |    ",
      "    |     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |    ",
      "",
      "    |     |     @     |     |     @     |     |     @     |     |    ",
      "  _ |     |           |     |           |     |           |     |    ",
      " |#||     +-----------+     +-----------+     +-----------+     |    ",
      " |_||           7                 8                 9           |    ",
      "  `-|     +-----------+     +-----------+     +-----------+     |    ",
      "    -     |           |     |           |     |           |     |    ",
      "",
      "    |     |     @     |     |     @     |     |     @     |     |    ",
      "    '     |           |     |           |     |           |     |    ",
      "   o'     +-----------+     +-----------+     +-----------+     |    ",
      "    '                                                           |    ",
      "____'___________________________________________________________'____", NULL};

const char *house1[] =    
     {"                                                                                 ______________          ",
      "                                                                                |______________|         ",
      "      ___________________________________________________________________________|            |_____     ",
      "     '                                                                           |____________|     `    ",
      "    |                                                                                               |    ",
      "    '-----------------------------------------------------------------------------------------------'    ",
      "    |           1                 1                 1                 2                 3           |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "",                                                              
      "    |     |     @     |     |     @     |     |     @     |     |     @     |     |     @     |     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |           1                 1                 1                 2                 3           |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "",                                                              
      "    |     |     @     |     |     @     |     |     @     |     |     @     |     |     @     |     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |           1                 1                 1                 2                 3           |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "",                                                              
      "    |     |     @     |     |     @     |     |     @     |     |     @     |     |     @     |     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |           4                 4                 4                 5                 6           |    ",
      "    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    |     |           |     |           |     |           |     |           |     |           |     |    ",
      "",                                                              
      "    |     |     @     |     |     @     |     |     @     |     |     @     |     |     @     |     |    ",
      "  _ |     |           |     |           |     |           |     |           |     |           |     |    ",
      " |#||     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      " |_||           7                 7                 7                 8                 9           |    ",
      "  `-|     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    -     |           |     |           |     |           |     |           |     |           |     |    ",
      "",                                                              
      "    |     |     @     |     |     @     |     |     @     |     |     @     |     |     @     |     |    ",
      "    '     |           |     |           |     |           |     |           |     |           |     |    ",
      "   o'     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    ",
      "    '                                                                                               |    ",
      "____'_______________________________________________________________________________________________'____", NULL};      

https://godbolt.org/z/PE9bMY3Ee

结果:

                                             ______________          
                                            |______________|         
      _______________________________________|            |_____     
     '                                       |____________|     `    
    |                                                           |    
    '-----------------------------------------------------------'    
    |           1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |    
    |     |     X     |     |     X     |     |           |     |    
    |     |           |     |           |     |           |     |    
    |     +-----------+     +-----------+     +-----------+     |    
    |           4                 5                 6           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |    
    |     |     X     |     |     X     |     |           |     |    
  _ |     |           |     |           |     |           |     |    
 |#||     +-----------+     +-----------+     +-----------+     |    
 |_||           7                 8                 9           |    
  `-|     +-----------+     +-----------+     +-----------+     |    
    -     |           |     |           |     |           |     |    
    |     |     X     |     |           |     |           |     |    
    '     |           |     |           |     |           |     |    
   o'     +-----------+     +-----------+     +-----------+     |    
    '                                                           |    
____'___________________________________________________________'____
                                                                                 ______________          
                                                                                |______________|         
      ___________________________________________________________________________|            |_____     
     '                                                                           |____________|     `    
    |                                                                                               |    
    '-----------------------------------------------------------------------------------------------'    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     |     X     |     |     X     |     |           |     |     X     |     |           |     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     |     X     |     |     X     |     |     X     |     |           |     |     X     |     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     |     X     |     |           |     |     X     |     |           |     |     X     |     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           4                 4                 4                 5                 6           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |     |           |     |           |     |           |     |           |     |           |     |    
    |     |           |     |     X     |     |           |     |     X     |     |           |     |    
  _ |     |           |     |           |     |           |     |           |     |           |     |    
 |#||     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
 |_||           7                 7                 7                 8                 9           |    
  `-|     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |           |     |           |     |           |     |           |     |           |     |    
    |     |     X     |     |           |     |           |     |     X     |     |     X     |     |    
    '     |           |     |           |     |           |     |           |     |           |     |    
   o'     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    '                                                                                               |    
____'_______________________________________________________________________________________________'____
© www.soinside.com 2019 - 2024. All rights reserved.