Reputation: 47
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?
Upvotes: 1
Views: 81
Reputation: 34839
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]);
}
Upvotes: 1