Ligths out game with house trying to write function that writes "#" in correct windows

88 Views Asked by At
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)

I have been working on this for a few days now and cannot get it to fill all correct windows. The function below calls the function above, window_update_graphics, and this is the function that fills the windows corresponding to the 1's in the state array.

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

This is where I am at:

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

There are 2 best solutions below

2
0___________ On

I would add something to the house.

  • first line defines size of the window
  • "" (empty string) indicates that the next string is s line with lights.
  • NULL indicates the end of the house
  • '@' - indicates position of the light

It will be universal and nice.

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)
{
    size_t wWidth, wHeight;

    if(sscanf(*house, "%zu, %zu", &wWidth, &wHeight) != 2) return; //wrong format
    house++;
    while(*house)
    {
        const char *line = *house;
        if(!*line)
        {
            for(size_t col = 0; col < wHeight; col++)
            {
                size_t window = 0;
                house++;
                line = *house;
                while(*line)
                {
                    if(*line == '@') 
                    {
                        for(size_t row = 0; row < wWidth; row++) 
                        {
                            fputc(state[0][window] ? 'X' : ' ', fo);
                            line++;
                        }
                        window++;
                        continue;
                    }
                    else fputc(*line, fo);
                    line++;
                }
                fputc('\n', fo);
            }
            state++;

        }
        else {fputs(*house, fo); fputc('\n', fo);}
        house++;
    }
}

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

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

const char *house1[] =    
     {"11,3", 
      "                                                                                 ______________          ",
      "                                                                                |______________|         ",
      "      ___________________________________________________________________________|            |_____     ",
      "     '                                                                           |____________|     `    ",
      "    |                                                                                               |    ",
      "    '-----------------------------------------------------------------------------------------------'    ",
      "    |           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/E9xbnTKM9

                                             ______________          
                                            |______________|         
      _______________________________________|            |_____     
     '                                       |____________|     `    
    |                                                           |    
    '-----------------------------------------------------------'    
    |           1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
    |     +-----------+     +-----------+     +-----------+     |    
    |           4                 5                 6           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |    
 |#||     +-----------+     +-----------+     +-----------+     |    
 |_||           7                 8                 9           |    
  `-|     +-----------+     +-----------+     +-----------+     |    
    -     |XXXXXXXXXXX|     |           |     |           |     |    
    |     |XXXXXXXXXXX|     |           |     |           |     |    
    |     |XXXXXXXXXXX|     |           |     |           |     |    
   o'     +-----------+     +-----------+     +-----------+     |    
    '                                                           |    
____'___________________________________________________________'____
                                                                                 ______________          
                                                                                |______________|         
      ___________________________________________________________________________|            |_____     
     '                                                                           |____________|     `    
    |                                                                                               |    
    '-----------------------------------------------------------------------------------------------'    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           1                 1                 1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    |           4                 4                 4                 5                 6           |    
    |     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
    -     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
    -     |           |     |XXXXXXXXXXX|     |           |     |XXXXXXXXXXX|     |           |     |    
 |#||     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
 |_||           7                 7                 7                 8                 9           |    
  `-|     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    -     |XXXXXXXXXXX|     |           |     |           |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |           |     |           |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |    
    -     |XXXXXXXXXXX|     |           |     |           |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |    
   o'     +-----------+     +-----------+     +-----------+     +-----------+     +-----------+     |    
    '                                                                                               |    
____'_______________________________________________________________________________________________'____

It will also work with non regular window placements. Example (this time second row is 1,0,1):

                                             ______________          
                                            |______________|                              
      _______________________________________|            |__________________________     
     '                                       |____________|                          `    
    |                                                                                |    
    '--------------------------------------------------------------------------------'    
    |           1                 2                 3                                |    
    |     +-----------+     +-----------+     +-----------+                          |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |                          |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |                          |    
    |     |XXXXXXXXXXX|     |XXXXXXXXXXX|     |           |                          |    
    |     +-----------+     +-----------+     +-----------+                          |    
    |           4                 5                 6                                |    
    |     +-----------+     +-----------+                         +-----------+      |    
    |     |XXXXXXXXXXX|     |           |                         |XXXXXXXXXXX|      |    
    |     |XXXXXXXXXXX|     |           |                         |XXXXXXXXXXX|      |    
    |     |XXXXXXXXXXX|     |           |                         |XXXXXXXXXXX|      |    
 |#||     +-----------+     +-----------+                         +-----------+      |    
 |_||           7                 8                 9                                |    
  `-|     +-----------+     +-----------+     +-----------+                          |    
    -     |XXXXXXXXXXX|     |           |     |           |                          |    
    |     |XXXXXXXXXXX|     |           |     |           |                          |    
    |     |XXXXXXXXXXX|     |           |     |           |                          |    
   o'     +-----------+     +-----------+     +-----------+                          |    
    '                                                                                |    
____'________________________________________________________________________________'____

https://godbolt.org/z/TxoWqfenW

0
Allan Wind On

Convert each entry of the state array to a window number y * STATE_COLUMNS + x + 1. Then find the top left position of the inside of this window by searching for the window number (string) followed by and some relative navigation. Finally, fill the window by mapping the state entry value to a fill character ch.

#include <stdio.h>
#include <string.h>

#define HOUSE_WIDTH 69
#define STATE_COLUMNS 3
#define WINDOW_HEIGTH 5
#define WINDOW_WIDTH 13

char *window_find(const char *house, int n) {
    // number is padded in case n > 9
    int len = snprintf(NULL, 0, " %d ", n);
    char s[len+1];
    sprintf(s, " %d ", n);
    // convert window number `n` to text `s` then search
    // for `s`.  Then avigate two lines to be inside 
    // the window frame.  Then find the right frame '|'
    // and use known width of window to find the top left
    // inside of the window.  Add some error handling so
    // invalid input doesn't result in a segfault?
    return strchr(strstr(house, s) + 2 * HOUSE_WIDTH, '|') - WINDOW_WIDTH + 2;
}

void window_fill(char *window, char ch) {
    for(int r = 0; r < WINDOW_HEIGTH - 2; r++)
        memset(window + r * HOUSE_WIDTH, ch, WINDOW_WIDTH - 2);
}

void window_update_graphics(char *house, const int *state, int x, int y) {
    window_fill(
        window_find(house, y * STATE_COLUMNS + x + 1),
        " #"[state[y * STATE_COLUMNS + x]]
    );
}

void house_print(const char *house) {
    for(; *house; house += HOUSE_WIDTH)
        printf("%.*s\n", HOUSE_WIDTH, house);
}

int main(void) {
    char house[] =
        "                                             ______________          "
        "                                            |______________|         "
        "      _______________________________________|            |_____     "
        "     '                                       |____________|     `    "
        "    |                                                           |    "
        "    '-----------------------------------------------------------'    "
        "    |           1                 2                 3           |    "
        "    |     +-----------+     +-----------+     +-----------+     |    "
        "    |     |           |     |           |     |           |     |    "
        "    |     |           |     |           |     |           |     |    "
        "    |     |           |     |           |     |           |     |    "
        "    |     +-----------+     +-----------+     +-----------+     |    "
        "    |           4                 5                 6           |    "
        "    |     +-----------+     +-----------+     +-----------+     |    "
        "    |     |           |     |           |     |           |     |    "
        "    |     |           |     |           |     |           |     |    "
        "  _ |     |           |     |           |     |           |     |    "
        " |#||     +-----------+     +-----------+     +-----------+     |    "
        " |_||           7                 8                 9           |    "
        "  `-|     +-----------+     +-----------+     +-----------+     |    "
        "    -     |           |     |           |     |           |     |    "
        "    '     |           |     |           |     |           |     |    "
        "    '     |           |     |           |     |           |     |    "
        "   o'     +-----------+     +-----------+     +-----------+     |    "
        "    '                                                           |    "
        "____'___________________________________________________________'____";

    const int state[] = {
        1, 1, 0,
        1, 1, 0,
        1, 0, 0
    };
    for(unsigned i = 0; i < sizeof state / sizeof *state; i++)
        window_update_graphics(house, state, i % STATE_COLUMNS, i / STATE_COLUMNS);
    house_print(house);
}

Example run:

                                             ______________          
                                            |______________|         
      _______________________________________|            |_____     
     '                                       |____________|     `    
    |                                                           |    
    '-----------------------------------------------------------'    
    |           1                 2                 3           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |###########|     |###########|     |           |     |    
    |     |###########|     |###########|     |           |     |    
    |     |###########|     |###########|     |           |     |    
    |     +-----------+     +-----------+     +-----------+     |    
    |           4                 5                 6           |    
    |     +-----------+     +-----------+     +-----------+     |    
    |     |###########|     |###########|     |           |     |    
    |     |###########|     |###########|     |           |     |    
  _ |     |###########|     |###########|     |           |     |    
 |#||     +-----------+     +-----------+     +-----------+     |    
 |_||           7                 8                 9           |    
  `-|     +-----------+     +-----------+     +-----------+     |    
    -     |###########|     |           |     |           |     |    
    '     |###########|     |           |     |           |     |    
    '     |###########|     |           |     |           |     |    
   o'     +-----------+     +-----------+     +-----------+     |    
    '                                                           |    
____'___________________________________________________________'____