0

I have python (3.7) code that works, but it's ugly, and I'm sure there's a more elegant way of doing what I want to do. I have 9 3x3 grids that I want to print out (plus some line separators) for a tic-tac-toe game.

I basically want the output to look like this (sample of the first row)

█ █│   │███
 █ │ 2 │█ █
█ █│   │███

The code below does this, but it seems like there's a better way, and I'm not sure how to do this in an elegant for loop.

Thanks in advance!

choice_x = [
    ['█', ' ', '█'],
    [' ', '█', ' '],
    ['█', ' ', '█']
]

choice_o = [
    ['█', '█', '█'],
    ['█', ' ', '█'],
    ['█', '█', '█']
]

v_grid = [
    ['│'],
    ['│'],
    ['│']
]

empty_grid = [
    [' ',' ',' '],
    [' ','2',' '],
    [' ',' ',' ']
]

row1 = [
    choice_x, v_grid, empty_grid, v_grid, choice_o
]

#for n in range(0,4):
slice1 = [sublist[0][:3] for sublist in row1]
result = ''.join([''.join(item) for item in slice1])
print(result)

slice2 = [sublist[1][:3] for sublist in row1]
result = ''.join([''.join(item) for item in slice2])
print(result)

slice3 = [sublist[2][:3] for sublist in row1]
result = ''.join([''.join(item) for item in slice3])
print(result)
#end for loop

2 Answers 2

1

Without using list of lists, but knowing the size of the board:

GRID_SIZE = 3
SEP = '|'
choice_x = ['█', ' ', '█',
            ' ', '█', ' ',
            '█', ' ', '█']
empty_grid = [' ', ' ', ' ',
              ' ', '2', ' ',
              ' ', ' ', ' ']
choice_o = ['█', '█', '█',
            '█', ' ', '█',
            '█', '█', '█']

for i in range(0, GRID_SIZE**2, GRID_SIZE):
    s1 = ''.join(choice_x[i:i+GRID_SIZE])
    s2 = ''.join(empty_grid[i:i+GRID_SIZE])
    s3 = ''.join(choice_o[i:i+GRID_SIZE])
    print(SEP.join([s1, s2, s3])
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

class Printer:
    def __init__(self):
        self.grid = [[[[" " for y in range(3)] for x in range(3)] for i in range(3)] for j in range(3)]

    def render(self):
        print("-" * 13)
        for y in range(3):
            for j in range(3):
                print("|",end="")
                for x in range(3):
                    print("".join(self.grid[y][x][j]), end="|")
                print()
            print("-" * 13)

    def set_char(self, x, y, char):
        self.grid[y][x] = char

choice_x = [
    ['█', ' ', '█'],
    [' ', '█', ' '],
    ['█', ' ', '█']
]

choice_o = [
    ['█', '█', '█'],
    ['█', ' ', '█'],
    ['█', '█', '█']
]

empty_grid = [
    [' ',' ',' '],
    [' ','2',' '],
    [' ',' ',' ']
]

board = Printer()
board.set_char(0,1,choice_x)
board.set_char(1,1,empty_grid)
board.set_char(2,1,choice_o)
board.render()

print("\n" * 2)

board.set_char(2, 0, choice_o)
board.render()

Output:

-------------
|   |   |   |
|   |   |   |
|   |   |   |
-------------
|█ █|   |███|
| █ | 2 |█ █|
|█ █|   |███|
-------------
|   |   |   |
|   |   |   |
|   |   |   |
-------------



-------------
|   |   |███|
|   |   |█ █|
|   |   |███|
-------------
|█ █|   |███|
| █ | 2 |█ █|
|█ █|   |███|
-------------
|   |   |   |
|   |   |   |
|   |   |   |
-------------

Board x/y explaination:

   0 1 2
  -------
0 | | | |
  -------
1 | | | |
  -------
2 | | | |
  -------

As you can see, both start at 0, and top-left.


Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.