Python Forum
Publishing two boards side by side - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Publishing two boards side by side (/thread-18385.html)



Publishing two boards side by side - DJ_Qu - May-15-2019

Hello,
We are implementing a simple console game of battleship. It has two boards (one human, one AI).
I want to have numbers 1 to 10 in a column next to the boards, and 1 to 10 over the boards to avoid counting positions before bombing. But I can't do it without attentive calibrating. And in addition, depending on the terminal, it prints out differently!

How can we print those two boards with numbers on the side and above?

This is the actual code:
There are imports from ship positions but the issue is print_board method.
from Shiptype import Shiptypes

class TerminalGUI:

    def __init__(self):
        print('Terminal GUI called from Game engine')
        self.board1 = [['~' for x in range(10)] for y in range(10)]
        self.board2 = [['~' for x in range(10)] for y in range(10)]

    def insert_ships_in_terminal_gui(self, board_human, board_ai):
        print('\nFilling boards with ships')

        for i in range(0, 5):
            print(str(i + 1) + ' ' + str(Shiptypes.all[i].get_name()))
        # i is incremented to see which boat is which

        self.filling(board_human, self.board1)
        self.filling(board_ai, self.board2)
        self.fill_bombed_coordinates(board_human, self.board1)
        self.fill_bombed_coordinates(board_ai, self.board2)


    def filling(self, board_with_ships, board_to_fill):
        i = 1
        for ship in board_with_ships.get_ships():
            xy_coordinates = ship.get_coordinates()
            for xy_coordinate in xy_coordinates:
                # graphical representation y is rows and x are columns
                # that's why coordinates are swapped
                board_to_fill[xy_coordinate[1]-1][xy_coordinate[0]-1] = i
            i = i + 1

    def fill_bombed_coordinates(self, board_with_ships, board_to_fill):
        for xy_coordinate in board_with_ships.get_hits_on_board():
            board_to_fill[xy_coordinate[1]-1][xy_coordinate[0]-1] = 'X'

    def print_board(self):
        print('\nPrinting boards (human left, AI right)\n')

        print('\tHuman', sep=' ', end='\n')
        print('   ', end='')
        for j in range(1, 11):
            print(str(j), sep= ' ', end=' ')
        print('\t\t\t\t', end='')

        print('AI', sep=' ')
        i = 1
        for minilist1, minilist2 in zip(self.board1, self.board2):
            print(str(i), '\t', *minilist1, sep = ' ', end='')
            print('\t\t\t\t', end='')
            print(str(i), '\t', *minilist2, sep=' ')
            i = i+1
        print()



RE: Publishing two boards side by side - ichabod801 - May-15-2019

Maybe I don't understand the problem correctly, but if you are just printing this out on a terminal, it should be easy. Print a header row with the numbers 1 to 10 twice, appropriately spaced. Then print the number 1, the first row of the user's board, and the first row of the AI's board, all on the same line. Then do the same for rows 2, 3, 4, and so on.


RE: Publishing two boards side by side - DJ_Qu - May-15-2019

(May-15-2019, 01:28 PM)ichabod801 Wrote: Maybe I don't understand the problem correctly, but if you are just printing this out on a terminal, it should be easy. Print a header row with the numbers 1 to 10 twice, appropriately spaced. Then print the number 1, the first row of the user's board, and the first row of the AI's board, all on the same line. Then do the same for rows 2, 3, 4, and so on.

Hello, that's the *appropriately spaced* which is the issue. I want to have those 1-10 in the loops, so that they land precisely where they should.


RE: Publishing two boards side by side - ichabod801 - May-15-2019

Show me the example layout that you want. Put it in output tags, to get a monospace font.


RE: Publishing two boards side by side - Yoriz - May-15-2019

One of these modules might do what your looking for
https://pypi.org/project/tabulate/
https://pypi.org/project/PrettyTable/