Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming printing after looping

Tags:

c

I am trying to make hexagons tiles. I have the shape but I am doing something wrong while printing them.

Here is my code and what I tried:

    #include <stdio.h>

  int main() {

    int i, x;

    for (i = 1; i <= 2; i++) {

      for (x = 1; x <= 2; x++) {
        printf("  %d,%d\t  \n", i, x);
        printf(" / \t \\\n");
        printf("| \t  |\n");
        printf("| \t  |\n");
        printf(" \\ \t /\n");
      }

      printf("\n\n");

    }
  }

I need them horizontal and not vertical.

I know what I did wrong, which is printing the hex-shape "m,n+1" below the hex-shape "m,n", instead of on its right side.
But I don't know how to fix it. Thanks!

Fixed!

Update: Results now are

#include <stdio.h>
#define COLS 4
#define ROWS 3

void subrow(char* pcWhat, char* pcEndWhat)
{  int col;
   for (col = 0; col < COLS; col++) {
     printf("%s", pcWhat);
   }
   printf("%s\n", pcEndWhat);
}
void indexedSubrow(char* pcWhat, char* pcEndWhat, int row, int offset)
{  int col;
   for (col = 0; col < COLS; col++) {
         printf(pcWhat, row, col+offset);
       }

   printf("%s\n", pcEndWhat);
}

int main(void) {
    int i, x;


    subrow("  ___     ","");


    for (i = 0; i < ROWS; i++) {
           if(i==0){
               subrow(" /   \\    ","  ");
      indexedSubrow("/ %d,%d \\___"," ", i, 0);
             subrow("\\     /   ","\\");
      indexedSubrow(" \\___/ %d,%d"," \\", i, 1);  


      }

   //else if(i==ROWS-1){}


      else{
      subrow(" /   \\    "," /");
      indexedSubrow("/ %d,%d \\___","/", i, 0);
      subrow("\\     /   ","\\");
      indexedSubrow(" \\___/ %d,%d"," \\", i, 1);
           }
    }
    return 0;
}

More explanation and art of what I need:

The last subrow is:
\     /   \     /   \     /   \     /   \
 \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \

Which should be
\     /   \     /   \     /   \     /   \
 \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \
     \     /   \     /   \     /   \     /
      \___/     \___/     \___/     \___/

So the missing part is:
     \     /   \     /   \     /   \     /
      \___/     \___/     \___/     \___/

Also I am curious how to add numbers ("Let's say 1 as a second digit in all of the hexagon grids") Example:

Current art:

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

Expecting: ( Overall as the final result )

  ___       ___       ___       ___     
 /   \     /   \     /   \     /   \      
/ 0,0 \___/ 0,2 \___/ 0,4 \___/ 0,6 \___ 
\  1  /   \  1  /   \  1  /   \  1  /   \
 \___/ 0,1 \___/ 0,3 \___/ 0,5 \___/ 0,7 \
 /   \  1  /   \  1  /   \  1  /   \  1  /
/ 1,0 \___/ 1,2 \___/ 1,4 \___/ 1,6 \___/
\  1  /   \  1  /   \  1  /   \  1  /   \
 \___/ 1,1 \___/ 1,3 \___/ 1,5 \___/ 1,7 \
 /   \  1  /   \  1  /   \  1  /   \  1  /
/ 2,0 \___/ 2,2 \___/ 2,4 \___/ 2,6 \___/
\  1  /   \  1  /   \  1  /   \  1  /   \
 \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \
     \  1  /   \  1  /   \  1  /   \  1  /
      \___/     \___/     \___/     \___/

Thanks!

like image 930
hobaa Avatar asked Dec 17 '17 20:12

hobaa


People also ask

Why is my C code printing twice?

"prints out "Input a character" twice each time" is because user typed 2 keys.

How do you print a loop letter in Python?

In the below program, for loop is used to print the alphabets from A to Z. A loop variable is taken to do this of type 'char'. The loop variable 'i' is initialized with the first alphabet 'A' and incremented by 1 on every iteration. In the loop, this character 'i' is printed as the alphabet.

Can we give printf in for loop?

It keeps the for loop running until y==0 . The printf in the afterthought is run at the end of each iteration but doesn't change the value of y . The loop's body does change y however. Most textbooks will describe this.


1 Answers

You want to make rows of two (probably more) hex shapes each.
For that you must not print newlines after a sub-row of one hex shape.
Instead print the sub-rows of all (two) hex shapes, wiht appropriate whitespace to align them. For that, going away from tabs is probably helpful.
So basically you should change from

  for (x = 1; x <= 2; x++) {
    printf("  %d,%d\t  \n", i, x);
    printf(" / \t \\\n");
    printf("| \t  |\n");
    printf("| \t  |\n");
    printf(" \\ \t /\n");
  }

to

    for (i = 1; i <= 2; i++) {

      for (x = 1; x <= 2; x++) {
        printf("  %d,%d  ", i, x);
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf(" /   \\ ");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf("|     |");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf("|     |");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf(" \\   / ");
      }
      printf("\n");
    }

Note 1, I did not fine-tune the whitespace between the hex shapes in one row. You will have to do that yourself. So the output below might not satisfy you directly, but it should be visible that the core problem is solved.

Note 2, even this way you will get a square grid of hex-shapes.
If you actually want a hex-grid of hex-shapes, you have to change the position of the | in every second hex row.

Output:

  1,1    1,2
 /   \  /   \
|     ||     |
|     ||     |
 \   /  \   /
  2,1    2,2
 /   \  /   \
|     ||     |
|     ||     |
 \   /  \   /

I played around, attempting to make something of a hex grid, according to Note2. I also provide an attempt at ascii art of your pixel picture,
in the second hex-grid version,
based on Jonathan Lefflers proposal to turn the hexes by 90 degrees.

#include <stdio.h>
#define COLS 4
#define ROWS 3

void subrow(char* pcWhat, char* pcEndWhat)
{  int col;
   for (col = 0; col < COLS; col++) {
     printf("%s", pcWhat);
   }
   printf("%s\n", pcEndWhat);
}
void indexedSubrow(char* pcWhat, char* pcEndWhat, int row, int offset)
{  int col;
   for (col = 0; col < COLS; col++) {
     printf(pcWhat, row, col*2+offset);
   }
   printf("%s\n", pcEndWhat);
}

int main(void) {
    int i, x;

    for (i = 1; i <= 2; i++) {

      for (x = 1; x <= 2; x++) {
        printf("  %d,%d  ", i, x);
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf(" /   \\ ");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf("|     |");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf("|     |");
      }
      printf("\n");
      for (x = 1; x <= 2; x++) {
        printf(" \\   / ");
      }
      printf("\n");
    }
    printf("\n");
    printf("\n");
    printf("\n");

    for (i = 0; i < ROWS; i++) {
      subrow("   / \\  ","");
      subrow("  /   \\ ","");
      subrow(" /     \\","");
      subrow("|       ","|");
      subrow("|       ","|");
      subrow(" \\     /","");
      subrow("  \\   / ","");
      subrow("   \\ /  ","");
      subrow("    |   ","");
      subrow("    |   ","");
     }
      subrow("   / \\  ","");
      subrow("  /   \\ ","");
      subrow(" /     \\","");
      subrow("|       ","|");
      subrow("|       ","|");
      subrow(" \\     /","");
      subrow("  \\   / ","");
      subrow("   \\ /  ","");
      printf("\n");
      printf("\n");

    subrow("  ___     ","");
    for (i = 0; i < ROWS; i++) {
             subrow(" /   \\    "," /");
      indexedSubrow("/ %d,%d \\___","/", i, 0);
             subrow("\\     /   ","\\");
      indexedSubrow(" \\___/ %d,%d"," \\", i, 1);
    }
    return 0;
}

Output vertical hexes:

   / \     / \     / \     / \
  /   \   /   \   /   \   /   \
 /     \ /     \ /     \ /     \
|       |       |       |       |
|       |       |       |       |
 \     / \     / \     / \     /
  \   /   \   /   \   /   \   /
   \ /     \ /     \ /     \ /
    |       |       |       |
    |       |       |       |
   / \     / \     / \     / \
  /   \   /   \   /   \   /   \
 /     \ /     \ /     \ /     \
|       |       |       |       |
|       |       |       |       |
 \     / \     / \     / \     /
  \   /   \   /   \   /   \   /
   \ /     \ /     \ /     \ /

Output horizontal hexes:

  ___       ___       ___       ___
 /   \     /   \     /   \     /   \     /
/ 0,0 \___/ 0,2 \___/ 0,4 \___/ 0,6 \___/
\     /   \     /   \     /   \     /   \
 \___/ 0,1 \___/ 0,3 \___/ 0,5 \___/ 0,7 \
 /   \     /   \     /   \     /   \     /
/ 1,0 \___/ 1,2 \___/ 1,4 \___/ 1,6 \___/
\     /   \     /   \     /   \     /   \
 \___/ 1,1 \___/ 1,3 \___/ 1,5 \___/ 1,7 \
 /   \     /   \     /   \     /   \     /
/ 2,0 \___/ 2,2 \___/ 2,4 \___/ 2,6 \___/
\     /   \     /   \     /   \     /   \
 \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \

I am sure that you will not like the parts on the edges and the position of the digits,
but that is for yourself to tune to your taste.

like image 191
Yunnosch Avatar answered Oct 07 '22 00:10

Yunnosch