Effective way to draw 3! (6 times) three forms in C

I want to draw all the combinations (3! = 6) from 3 figures in one line: an empty cell, X or a rectangle.

Current code:

For an empty cell:

 void drawEmptyCell() { printf("||||||||||||||||||||||||||\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("| |\n"); printf("||||||||||||||||||||||||||\n"); } 

For a cell with a rectangle:

 void drawCellWithRectangle() { printf("||||||||||||||||||||||||||\n"); printf("| |\n"); printf("| |\n"); printf("| ************** |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| ************** |\n"); printf("| |\n"); printf("| |\n"); printf("||||||||||||||||||||||||||\n"); } 

And for a cell with X :

 void drawCellWithX() { printf("||||||||||||||||||||||||||\n"); printf("| |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| * * |\n"); printf("| |\n"); printf("||||||||||||||||||||||||||\n"); } 

I can use brute force and take all 6 parameters, for example:

 void drawOption1() { // empty , rectangle , x printf("||||||||||||||||||||||||||");printf("||||||||||||||||||||||||||");printf("||||||||||||||||||||||||||\n"); printf("| |");printf("| |");printf("| |\n"); printf("| |");printf("| |");printf("| * * |\n"); printf("| |");printf("| ************** |");printf("| * * |\n"); printf("| |");printf("| * * |");printf("| * * |\n"); printf("| |");printf("| * * |");printf("| * * |\n"); printf("| |");printf("| * * |");printf("| * |\n"); printf("| |");printf("| * * |");printf("| * * |\n"); printf("| |");printf("| * * |");printf("| * * |\n"); printf("| |");printf("| ************** |");printf("| * * |\n"); printf("| |");printf("| |");printf("| * * |\n"); printf("| |");printf("| |");printf("| |\n"); printf("||||||||||||||||||||||||||");printf("||||||||||||||||||||||||||");printf("||||||||||||||||||||||||||\n"); } 

But I'm looking for something else, without brute force all the way.

Any suggestions would be greatly appreciated.

+4
source share
2 answers

A good approach would be to include the shapes in a string array. Then the corresponding algorithm will draw them

Something like the following will do the job. I hope you get an idea

 char* x[] = { "||||||||||||||||||||||||||", "| |", "| * * |", "| * * |", "| * * |", "| * * |", "| * |", "| * * |", "| * * |", "| * * |", "| * * |", "| |", "||||||||||||||||||||||||||"}; char* o[] .. char* empty[] .. .. output (" XO"); .. void output (const char* pOut) { // assert (sizeof(x) == sizeof(o)); // assert (sizeof(x) == sizeof(empty)); int i, j; for (i = 0; i < sizeof(o) / sizeof(o[0]); i ++) { const char* c = pOut; while (*c != 0) { switch (*c ++) { case 'X': printf (x[i]); break; case 'O': printf (o[i]); break; default: printf (empty[i]); break; } } printf ("\n"); } } 

Obviously, x, o, empty should contain the same number of lines.

now in C, not compiled yet, so it may have errors

+3
source

You can use gotoxy (x, y); and printf(); in for-loop .

+3
source

Source: https://habr.com/ru/post/1480859/


All Articles