Question

I'm trying to create a grid that's n_rows by n_columns, which will be changeable. This is my code; it takes a list of lists and the two dimension integers:

def _print_board(game_state: list, n_rows: int, n_columns: int)-> None:
    for i in range(n_rows):
        if i+1 < 10:
            print(i+1, '', end=' ')
        else:
            print(i+1, end=' ')
        for j in range(n_columns):
            if game_state[j][i] == NONE:
                print('.', end=' ')
            elif game_state[j][i] == WHITE:
                print(WHITE, end=' ')
            elif game_state[j][i] == BLACK:
                print(BLACK, end=' ')
        else:
            print('\n',end='')

The output I get is:

1  . . . . . . . . . . . . . . . . 
2  . . . . . . . . . . . . . . . . 
3  . . . . . . . . . . . . . . . . 
4  . . . . . . . . . . . . . . . . 
5  . . . . . . . . . . . . . . . . 
6  . . . . . . . . . . . . . . . . 
7  . . . . . . . . . . . . . . . . 
8  . . . . . . . B W . . . . . . . 
9  . . . . . . . W B . . . . . . . 
10 . . . . . . . . . . . . . . . . 
11 . . . . . . . . . . . . . . . . 
12 . . . . . . . . . . . . . . . . 
13 . . . . . . . . . . . . . . . . 
14 . . . . . . . . . . . . . . . . 
15 . . . . . . . . . . . . . . . . 
16 . . . . . . . . . . . . . . . .

So I get numbered rows but I can't figure out how to format numbered columns properly, meaning that each dot aligns up with the row and column number. I would like to get something like this, but with each dot also aligned with the column number:

   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1  . . . . . . . . . . . . . . . . 
2  . . . . . . . . . . . . . . . . 
3  . . . . . . . . . . . . . . . . 
4  . . . . . . . . . . . . . . . . 
5  . . . . . . . . . . . . . . . . 
6  . . . . . . . . . . . . . . . . 
7  . . . . . . . . . . . . . . . . 
8  . . . . . . . B W . . . . . . . 
9  . . . . . . . W B . . . . . . . 
10 . . . . . . . . . . . . . . . . 
11 . . . . . . . . . . . . . . . . 
12 . . . . . . . . . . . . . . . . 
13 . . . . . . . . . . . . . . . . 
14 . . . . . . . . . . . . . . . . 
15 . . . . . . . . . . . . . . . . 
16 . . . . . . . . . . . . . . . .

The column numbers can be on top or bottom, but I prefer them on top. How do I make it so that the dots align with the column numbers and there won't be those two numbers hanging off the edge? 16 is the max dimension for the grid, so this is the largest grid I would print.

Was it helpful?

Solution

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top