I want to print a 2048 board with proper spacing.
I was able to reach till here.
╔═══╤═══╤═══╤═══╗
║ 1 │ 2 │ 3 │ 4 ║
╟───┼───┼───┼───╢
║ 3 │ 2 │ 4 │ 5 ║
╟───┼───┼───┼───╢
║ 1 │ 1 │ 4 │ 2 ║
╟───┼───┼───┼───╢
║ 6 │ 5 │ 4 │ 3 ║
╚═══╧═══╧═══╧═══╝
But when I give larger numbers, it does not look good.
╔═══╤═══╤═══╤═══╗
║ 1 │ 1256 │ 3 │ 4 ║
╟───┼───┼───┼───╢
║ 3 │ 2 │ 4 │ 123 ║
╟───┼───┼───┼───╢
║ 1 │ 1 │ 4 │ 2 ║
╟───┼───┼───┼───╢
║ 6 │ 2048 │ 4 │ 3 ║
╚═══╧═══╧═══╧═══╝
How can I achieve this with only standard libraries?
Thanks!
Edit
I know that we can use prettytable
but I also want to print it colorful. So I cant use prettytable
.
Edit 2
My code -
print('\n'.join([
'╔═══╤═══╤═══╤═══╗',
'║ {} │ {} │ {} │ {} ║'.format(*board[0]),
'╟───┼───┼───┼───╢',
'║ {} │ {} │ {} │ {} ║'.format(*board[1]),
'╟───┼───┼───┼───╢',
'║ {} │ {} │ {} │ {} ║'.format(*board[2]),
'╟───┼───┼───┼───╢',
'║ {} │ {} │ {} │ {} ║'.format(*board[3]),
'╚═══╧═══╧═══╧═══╝'
]))
CodePudding user response:
I don't think you can achieve this in such a simple way. My guess would be to :
- Find out the length of the biggest number in your table
- Fill out every other number with enough space to reach the same length (you can look at https://docs.python.org/3/library/stdtypes.html#str.ljust for that)
- Generate your table with the right size