The easiest way to solve this is to format all numbers to two characters:
def _print_board(game_state):
print(" ".join("{0:2d}".format(i) if i else " " # or 0:02d to pad with zero
for i in range(len(game_state[0]) + 1)))
for i, row in enumerate(game_state, 1):
print("{0:2d}".format(i), end=" ")
print("".join(" {0} ".format(col if col != NONE else ".") for col in row))
Note that you don't need to pass the sizes if you can get them from game_state
. For a 12x4 grid, this gives me:
1 2 3 4 5 6 7 8 9 10 11 12
1 . . . . . . . . . . . .
2 . . . . . . . . . . . .
3 . . . . . . . . . . . .
4 . . . . . . . . . . . .
Expanding the above function a bit:
def _print_board(game_state):
# headers
for i in range(len(game_state[0]) + 1):
if i == 0:
# column for row numbers
print(" ", end="")
else:
# column headers
print("{0:2d} ".format(i), end="")
print()
for i, row in enumerate(game_state, 1):
# row number
print("{0:2d} ".format(i), end="")
for col in row:
# row data
if col == NONE:
print(" {0} ".format("."), end="")
else:
print(" {0} ".format(col), end="")
print()