MechAvia
MechAvia

Reputation: 343

Automatically adjusting display types

I am working on the "2048" game project.

When displaying the matrix, there are zeros as shown in the next figure: enter image description here

I want to get rid of the zeros in the table and just display the non-zero ints. So I added the following code to my existing "PrintMat" function:

void PrintMat(int a[4][4])
{
    int b[4][4];
    int i, j;

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            if (a[i][j] == 0)
            {
                b[i][j] = ' ';
            }
            else
            {
                b[i][j] = a[i][j];
            }
        }
    }
        printf("+--------------+--------------+--------------+--------------+\n");
printf("|              |              |              |              |\n");
printf("|   %5d      |   %5d      |   %5d      |   %5d      |\n", b[0][0], b[0][1], b[0][2], b[0][3]);
printf("|              |              |              |              |\n");
printf("+--------------+--------------+--------------+--------------+\n");
printf("|              |              |              |              |\n");
printf("|   %5d      |   %5d      |   %5d      |   %5d      |\n", b[1][0], b[1][1], b[1][2], b[1][3]);
printf("|              |              |              |              |\n");
printf("+--------------+--------------+--------------+--------------+\n");
printf("|              |              |              |              |\n");
printf("|   %5d      |   %5d      |   %5d      |   %5d      |\n", b[2][0], b[2][1], b[2][2], b[2][3]);
printf("|              |              |              |              |\n");
printf("+--------------+--------------+--------------+--------------+\n");
printf("|              |              |              |              |\n");
printf("|   %5d      |   %5d      |   %5d      |   %5d      |\n", b[3][0], b[3][1], b[3][2], b[3][3]);
printf("|              |              |              |              |\n");
printf("+--------------+--------------+--------------+--------------+\n");
}

And the output become as shown in the following figure: enter image description here

I know what is wrong with it (I am using int type to display the characters). The problem is: How can I fix it? How can I get rid of the zeros?

Thank you for your time!

Upvotes: 3

Views: 60

Answers (2)

Alex Trebek
Alex Trebek

Reputation: 917

Since you will only need to handle a limited set of possible numbers, I'd cheat and use a lookup table.

#include <stdio.h>

const char *Fmt(short a)
{
    static const char *powers[] = {
        "     ",
        "  2  ",
        "  4  ",
        "  8  ",
        " 16  ",
        " 32  ",
        " 64  ",
        " 128 ",
        " 256 ",
        " 512 ",
        "1024 ",
        "2048 ",
        "4068 ",
        "8192 ",
        "16384",
        "32768",
    };
    return powers[a];
}

void PrintMat(short a[4][4])
{
    for (int i = 0; i < 4; i++) {
        printf("+--------------+--------------+--------------+--------------+\n");
        printf("|              |              |              |              |\n");
        printf("|    %5s     |    %5s     |    %5s     |    %5s     |\n", Fmt(a[i][0]), Fmt(a[i][1]), Fmt(a[i][2]), Fmt(a[i][3]));
        printf("|              |              |              |              |\n");
    }
    printf(    "+--------------+--------------+--------------+--------------+\n");
}

int main() {
    static short a[][4] = {
        { 0, 1, 2, 3 },
        { 4, 5, 6, 7 },
        { 8, 9,10,11 },
        {12,13,14,15 }
    };
    PrintMat(a);
}

Upvotes: 1

this
this

Reputation: 5290

Using %d specifier you can't print a space. You will have to use %c specifier and an if statement in the case the array hold an integer with the value for a space.

Your printf statements will have to be split into shorter ones. One for each array element, and put them into a for loop.

Upvotes: 2

Related Questions