I want to create a function that prints out a tic-tac-toe board with a given list that position its index value on the board in a reverse order like on a number keyboard that is from 9 to 1, with the 1st row having the index value of 7, 8, 9: 2nd row 4, 5, 6: 3rd row 1, 2, 3.
For example: a given list
test_board = ['O','O','X','X','X','O','X','O','X']
That show print out like this:
X | O | X
---|---|---
X | X | O
---|---|---
O | O | X
I wrote this function:
def display_board(board):
h_sep = '-' * 3
for i in range(3):
for j in reversed(board[1:]):
print(f"{j:^3}|{j:^3}|{j:^3}")
if i != 2:
print(f"{h_sep:^3}|{h_sep:^3}|{h_sep:^3}")
But i get this print out when i call the function with the given list:
test_board = ['O','O','X','X','X','O','X','O','X']
display_board(test_board)
Output:
X | X | X
O | O | O
X | X | X
O | O | O
X | X | X
X | X | X
X | X | X
O | O | O
---|---|---
X | X | X
O | O | O
X | X | X
O | O | O
X | X | X
X | X | X
X | X | X
O | O | O
---|---|---
X | X | X
O | O | O
X | X | X
O | O | O
X | X | X
X | X | X
X | X | X
O | O | O
Edited:
Can i use for loop to achieve this without writing multiple print statement like this:
print(f"{board[6]:^3}|{board[7]:^3}|{board[8]:^3}")
print(f"{h_sep:^3}|{h_sep:^3}|{h_sep:^3}")
print(f"{board[3]:^3}|{board[4]:^3}|{board[5]:^3}")
print(f"{h_sep:^3}|{h_sep:^3}|{h_sep:^3}")
print(f"{board[0]:^3}|{board[1]:^3}|{board[2]:^3}")