1

I'm currently making a game of tic_tac_toe and I'm trying to make a function that will draw a grid with new information for every turn. Problem is that the "grid" board is moved when new information is entered.

Here's what I've tried:

row1 = ["", "", ""]
row2 = ["", "", ""]
row3 = ["", "", ""]
def gridDrawer (row1, row2, row3) :
    print(f"| {row1[0]} |", f"{row1[1]}", f"| {row1[2]} |")
    print(f"| {row2[0]} |", f"{row2[1]}", f"| {row2[2]} |")
    print(f"| {row3[0]} |", f"{row3[1]}", f"| {row3[2]} |")

gridDrawer(row1, row2, row3)
row1[0] = 5 # This part moves the "|" if i call the gridDrawer() again. 
White Owl
  • 21
  • 6
  • 2
    Did you mean: `row1 = [" ", " ", " "]` etc? – quamrana Jul 05 '21 at 12:57
  • You easily fix it by making empty cells the same width as occupied calls, i.e. one character wide as @quamrana suggested. The alternative is to specify a field with in your strings. e.g. f"|{row1[0]:1}` etc. – Tom Karzes Jul 05 '21 at 12:59

1 Answers1

6

You can use spaces instead of empty strings in your original empty grid. This way, when you change it to other characters, the size remains the same.

row1 = [" ", " ", " "]
row2 = [" ", " ", " "]
row3 = [" ", " ", " "]
def gridDrawer (row1, row2, row3) :
    print(f"|{row1[0]}|{row1[1]}|{row1[2]}|")
    print(f"|{row2[0]}|{row2[1]}|{row2[2]}|")
    print(f"|{row3[0]}|{row3[1]}|{row3[2]}|")

output:

>>> row1[0] = 5
>>> gridDrawer(row1, row2, row3)

|5| | |
| | | |
| | | |
mozway
  • 194,879
  • 13
  • 39
  • 75