Python Forum
Publishing two boards side by side
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Publishing two boards side by side
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
Show me the example layout that you want. Put it in output tags, to get a monospace font.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
One of these modules might do what your looking for
https://pypi.org/project/tabulate/
https://pypi.org/project/PrettyTable/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to horizontally align and display images side-by-side in an email using Python? shantanu97 0 968 Feb-22-2023, 11:41 PM
Last Post: shantanu97
  Get the Client-Side TLS ? JohnnyCoffee 1 1,329 Nov-30-2021, 10:49 PM
Last Post: Larz60+
  ModuleNotFoundError after publishing project mattshu 2 1,456 Sep-07-2021, 11:43 PM
Last Post: jefsummers
  4D array with only elements on one side of the diagonal schniefen 0 1,656 Dec-24-2020, 11:32 AM
Last Post: schniefen
  NumPy Side Effects - HELP UNDERSTANDING lasek723 3 3,416 Sep-17-2020, 06:56 AM
Last Post: lasek723
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  putting 2 lists items side by side Jiwan 1 2,172 Apr-30-2020, 01:04 AM
Last Post: Larz60+
  Save a file uploaded from client-side without having to read into memory andym118 3 4,995 Nov-21-2019, 07:34 AM
Last Post: DeaD_EyE
  How to print data in bracket in list in one side. phob0s 2 2,145 Jul-23-2019, 08:00 AM
Last Post: phob0s

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020