1

Hey I have a character array here that is suppose to print the different parts of a hangman game

char HANG_STATES[7][10 * 9] = 
{
    "             +         +----     +----     +----     +----     +----     +----     +----  ",
    "             |         |         |   O     |   O     |   O     |   O     |   O     |   O  ",
    "             |         |         |         |   +     | --+     | --+--   | --+--   | --+--",
    "             |         |         |         |   |     |   |     |   |     |   |     |   |  ",
    "             |         |         |         |         |         |         |  /      |  / \\ ",
    "             |         |         |         |         |         |         |         |      ",
    "/*****\\   /*****\\   /*****\\   /*****\\   /*****\\   /*****\\   /*****\\   /*****\\   /*****\\   "
};

I thought it'd be like looping through rows and columns, but I've had no success with it so far. My question is, how can I print the 5th instance in this character array?

3
  • 1
    You didn't allow space for the trailing nulls, it needs to be [10 * 9 + 1] Commented Dec 28, 2019 at 1:57
  • 5
    This seems like the wrong way to do it. You should have a separate array for each step in the game, not one big array for everything. Commented Dec 28, 2019 at 1:59
  • Each segment has fixed width, so you can calculate where each row of each segment starts. Still, as @Barmar suggested, it would be easier to specify them separately to begin with. Commented Dec 28, 2019 at 2:05

1 Answer 1

1

You've got 9 different states, so you need an array of 9 drawings. One approach to the problem is to represent each drawing as a single string. Embedding newline characters '\n' in the string allows the string to cover multiple lines on the display.

The key concept that allows this to work: string concatenation. In C, when two double quoted strings are separated by zero or more whitespace characters, they are treated as one string. For example:
char *str = "Hello, " "world" "!"; is the same as char *str = "Hello, world!";

In the code below, each drawing is on multiple lines, but is a single string. The comma on the bottom line of a drawing marks the end of the string. So each drawing can be output with a single printf as shown in main at the end of the code.

#include <stdio.h>

char *HANG_STATES[9] =
{
    "           \n"
    "           \n"
    "           \n"
    "           \n"
    "           \n"
    "           \n"
    "/*****\\   \n",   // <-- comma marks the end of the string

    "   +       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   |   +   \n"
    "   |   |   \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   | --+   \n"
    "   |   |   \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   | --+-- \n"
    "   |   |   \n"
    "   |       \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   | --+-- \n"
    "   |   |   \n"
    "   |  /    \n"
    "   |       \n"
    "/*****\\   \n",

    "   +----   \n"
    "   |   O   \n"
    "   | --+-- \n"
    "   |   |   \n"
    "   |  / \\ \n"
    "   |       \n"
    "/*****\\   \n",
};

int main(void)
{
    for (int state = 0; state < 9; state++)
        printf("%s\n\n", HANG_STATES[state]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.