iMAN
iMAN

Reputation: 107

Print two boards in one line on Python

I am new to Python programming, so need help from experienced programmers. I got a code for which I would like to print two boards as one raw.

class Board:
    def __init__(self, hide=False, size=6):
        self.hide = hide
        self.size = size
        self.count = 0
        self.field = [['O'] * size for i in range(size)]
        self.full = []
        self.ships = []

    def __str__(self):
        cell = ''
        cell += '  | 1 | 2 | 3 | 4 | 5 | 6 |'
        for i, row in enumerate(self.field):
            cell += f'\n{i + 1} | ' + ' | '.join(row) + ' | '
        if self.hide:
            cell = cell.replace('■', 'O')
        return cell

b = Board
print(b())

And, based on the above class and methods there is another method that prints two boards as a column as below

`def print_boards(self):
    print('*' * 27)
    print('Board1!')
    print(self.human.board)
    print('*' * 27)
    print('Board2!')
    print(self.viki.board)`

Is there a straightforward way to achieve this?

I tried to use end parameter in a print() function, but I get weird output.

Upvotes: 0

Views: 53

Answers (1)

elldur
elldur

Reputation: 2103

There are libraries that let you write to the console in more advanced ways e.g. with color or moving the cursor. You could use one of these but it won't work in IDLE. So the simplest way would be to just print one row at a time from each board:

for l, r in zip(str(board1).split("\n"), str(board2).split("\n")):
    print(f"{l}   {r}")

Upvotes: 2

Related Questions