I am trying to resolve the error as highlighted in the picture below. I am able to print a tic-tac-toe board but the trailing whitespace affects the test case. I am not better yet at using strings to represent a list of numbers. The desired output is embedded in the image below:
def printBoard(plane):#function to print out the current board
for w in range(len(plane)):
for e in range(len(plane)):
if plane[w][e]== 1:
print('{:^3}'.format('X'), end="")
elif plane[w][e] == -1:
print('{:^3}'.format('O'), end="")
elif plane[w][e] == 0:
print('{:^3d}'.format((len(plane)*w+e)), end="")
print()
This will do want you want. Changed end
to vary if it was the end of a line and used periods(.) to make the spaces visible for demonstration. Refactored a bit to remove redundant code:
def printBoard(plane):
i=0
for values in plane:
for col,value in enumerate(values):
text = ' X' if value==1 else ' O' if value==-1 else f'{i:2d}'
print(text, end='\n' if col==len(values)-1 else '.')
i += 1
plane = [[0]*6 for _ in range(6)]
printBoard(plane)
plane[1][1] = 1
plane[1][5] = 1
plane[2][2] = -1
plane[2][5] = -1
print()
printBoard(plane)
Output:
0. 1. 2. 3. 4. 5
6. 7. 8. 9.10.11
12.13.14.15.16.17
18.19.20.21.22.23
24.25.26.27.28.29
30.31.32.33.34.35
0. 1. 2. 3. 4. 5
6. X. 8. 9.10. X
12.13. O.15.16. O
18.19.20.21.22.23
24.25.26.27.28.29
30.31.32.33.34.35