diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-07 16:06:45 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-07 16:06:45 +0200 |
commit | d8da3f4fb0b3d5e7ea6f4c65b16b81166c34ca19 (patch) | |
tree | 3c8f3c51b867db98e0acb4ef7e91f8aa2d3e4d71 | |
parent | f3451cbbdd70e0dda15b01a1920f486169c14d0f (diff) | |
download | ttfe-d8da3f4fb0b3d5e7ea6f4c65b16b81166c34ca19.tar.xz |
Center numbers
-rw-r--r-- | main.c | 18 | ||||
-rw-r--r-- | main.h | 1 |
2 files changed, 18 insertions, 1 deletions
@@ -399,6 +399,22 @@ void merge_west(board *b) { } } +void center_print(uint n, int width) +{ + char s[20] = {'\0'}; + int len; + sprintf(s, "%u", n); + len = strlen(s); + if (len >= width) { + printf("%s", s); + } else { + int remaining = width - len; + int spaces_right = remaining / 2; + int spaces_left = remaining - spaces_right; + printf("%*s%s%*s", spaces_left, "", s, spaces_right, ""); + } +} + void print_sep(const char *left, const char *right, const char *cross, const char *line) { printf("%s", left); @@ -422,7 +438,7 @@ void print_board_line(board *b, int l) { if(n == 0) printf(" "); else - printf("%6u", n); + center_print(n, 6); if(i == 3) printf("\u2503"); @@ -8,6 +8,7 @@ #include <time.h> #include <unistd.h> #include <termios.h> +#include <string.h> typedef struct board board; typedef unsigned int uint; |