So I found some code that formatted a 2048 Game Board so that it didn't look messy when there was more than 1 digit to a number:
nlist = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 128, 2, 2, 16, 16]
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
print('|{:^{width}}'.format(nlist[i], width=widths[i % 4]), end = '')
count += 1
if count == 4:
print("|\n" + '-' * width)
count = 0
print("")
Run this code to make things more clear and change values of nlist to see how the formatting works. So anyways, after I finished the game, I wanted to add colors to make the game easier to understand in terminal so I edited my code to look like this:
clist = nlist.copy()
for x in range(16):
if clist[x] == 2:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 4:
clist[x] = ' \033[1;37;105m' + str(clist[x]) + '\033[0m '
if clist[x] == 8:
clist[x] = ' \033[1;37;104m' + str(clist[x]) + '\033[0m '
if clist[x] == 16:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 32:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 64:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 128:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 256:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 512:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 1024:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 2048:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
if clist[x] == 4096:
clist[x] = ' \033[1;37;106m' + str(clist[x]) + '\033[0m '
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for
col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
print('|{:^{width}}'.format(clist[i], width=widths[i % 4]), end = '')
count += 1
if count == 4:
print("|\n" + '-' * width)
count = 0
But now the formatting got all messed up and my board looked ugly once again. Is there a possible way to change this code so that it looks like the first code except with colors(right now almost all the colors are the same for times sake. I will change it later). Also, is there any easier way to put colors in a conditional statement?
EDIT:
This is the link to the file with no colors that formats correctly: 2048 that works(no colors)
This is the link to the file with colors that does not format correctly: 2048 that does not work(colors)
Screen shot of me running code: Screen shot of messed up format