Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a 2 dimensional map in C

Tags:

c

loops

printf

I have a game board to be printed with ASCII which I hope by the end looks something like this

            ___
3       ___/ 1 \___  ___
    ___/ 2 \___/ 2 \/ 3 \
2  / 1 \___/ 3 \___/\___/___
   \___/ 2 \___/        / 2 \
1  / 1 \___/ 1 \___     \___/
   \___/ 2 \___/ 2 \
0      \___/   \___/

     1   2   3   4

The map is distributed along x and y axis, size of the map is fixed too. The numbers to be printed are all stored in a 2d array. Some of them can be empty too as shown above. Here's something that I tried to do. map->sizeX and map->sizeY gives the size of the X and y axis of the map. If the number in that block is 0, that means the block doesn't exists and we don't print it. Any help would be much appreciated.

void printMap(struct Map *map) {
    for (int i = 0; i < map->sizeX; ++i) {
        for (int k = 0; k < map->sizeX && i == 0; ++k)
            printf(" ___ ");
        printf("\n");
        for (int j = 0; j < map->sizeY; ++j) {
            if(i == 0) {
                printf("/ %d \\", giveFloe(map, i, j)->numbOfFish);
            }
            else {
                printf("/ %d \\", giveFloe(map, i, j)->numbOfFish);
            }
        }
        printf("\n");
        for (int l = 0; l < map->sizeX; ++l)
            printf("\\___/");
    }
    printf("\n");
}

This is what I got as output, since I couldn't print the first row correctly, I didn't proceed. The map doesn't exceed 15 x 15 so I'm sure it can be printed on a terminal window.

 ___  ___  ___  ___  ___  ___  ___  ___  ___  ___ 
/ 3 \/ 0 \/ 2 \/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 1 \/ 0 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 0 \/ 0 \/ 0 \/ 2 \/ 0 \/ 1 \/ 0 \/ 3 \/ 0 \/ 3 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 2 \/ 0 \/ 3 \/ 0 \/ 3 \/ 0 \/ 3 \/ 0 \/ 1 \/ 0 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 0 \/ 1 \/ 0 \/ 2 \/ 0 \/ 2 \/ 0 \/ 2 \/ 0 \/ 3 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 3 \/ 0 \/ 0 \/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 1 \/ 0 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 0 \/ 3 \/ 0 \/ 2 \/ 0 \/ 1 \/ 0 \/ 3 \/ 0 \/ 1 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 3 \/ 0 \/ 2 \/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 2 \/ 0 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 3 \/ 0 \/ 2 \/ 0 \/ 2 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 3 \/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 3 \/ 0 \/ 2 \/ 0 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/
/ 0 \/ 2 \/ 0 \/ 3 \/ 0 \/ 1 \/ 0 \/ 3 \/ 0 \/ 3 \
\___/\___/\___/\___/\___/\___/\___/\___/\___/\___/

This is as far as I could get, it is assumed that the map won't exceed a terminal window. I am required for the map to look like the hexagons drawn above.

struct Map {
    struct Floe *mapPointer;
    int sizeX;
    int sizeY;
    struct Box *changelog;
    int changeCount;
    int maxChanges;
    struct Player *players;
    int playerCount;
};

giveFloe() takes in a the structure map and returns the floe on that coordinate.

struct Floe {
    int numbOfFish; 
    int whosPenguin; 
};
like image 949
Ritwick Malhotra Avatar asked Nov 09 '22 02:11

Ritwick Malhotra


1 Answers

I'm thinking your first step may be to re-think how the grid is laid out.

First of all how are you supposed to know which row each hexagon is supposed to be printed on (notice how my image treats each line as a row).

Second, is it legal to have two hexagons on the same row right next to each other? See hexagon 3 and 2 on the right side of the grid.

            ___
5       ___/ 1 \___  ___
4   ___/ 2 \___/ 2 \/ 3 \
3  / 1 \___/ 3 \___/\___/___
2  \___/ 2 \___/        / 2 \
1  / 1 \___/ 1 \___     \___/
0  \___/ 2 \___/ 2 \
       \___/   \___/

     0---1---2---3----4---5   <-- columns 4 and 5 don't fit normally
like image 150
Josh Lubawy Avatar answered Nov 15 '22 05:11

Josh Lubawy