Search code examples
pythonpython-3.xprinting2048

Printing 2048 board in python with spacing with standard libraries


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]),
            '╚═══╧═══╧═══╧═══╝'
]))

Solution

  • I don't think you can achieve this in such a simple way. My guess would be to :