Center numbers

master
Elias Fleckenstein 2021-06-07 16:06:45 +02:00
parent f3451cbbdd
commit d8da3f4fb0
2 changed files with 18 additions and 1 deletions

18
main.c
View File

@ -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");

1
main.h
View File

@ -8,6 +8,7 @@
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
typedef struct board board;
typedef unsigned int uint;