May-15-2019, 07:28 AM
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.
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()