0

I just started the game of fifteen ,so i thought of drawing the board in this way

char space=' ';
for(int i=15;i>=1;i--)
{
    for(int j=i;j>=i-3;j--)
    {
        printf("%2d%c",j,space);
    }
    i=i-3;        
    printf("\n");
}
}

to get the idea .I am getting output as

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

and i am not able to replace 0 by '-'which is the requirement of the game .

i tried implementing this

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    char space=' ';
    char hash='-';
    for(int i=15;i>=1;i--)
    {
        for(int j=i;j>=i-3;j--)
        {
            printf("%2d%c",j,space);
            while (j==1)
            {
                printf("%c",hash)
            }
        }
        i=i-3;
        printf("\n");
    }
}

but this outputs infinite bunch of numbers,seems like when implementing j==1 condition, loop gets repeated, it doesn't end.

1

The reason that your second loop never ends is that it is an infinite loop. The loop starts when j == 1, and continues as long as that is true. Since there's nothing inside the loop that modifies j, it will never change, so once the loop starts, it will never stop. If you only wanted to do it once, then an if statement would be a better choice.

In fact, an if statement is exactly what you need, just not exactly what you had in mind there. You want to print a dash instead of 0, so use the if to detect 0, and print the dash instead.

There may be other issues, but since you're still working all this stuff out, I'll let you have the fun of discovering what works and what doesn't. Enjoy!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .