def _set_combo_box_values(self):
        self._grid_sizes = [
            GridSize.small,
            GridSize.medium,
            GridSize.large
        ]

        # fill the grid size combo box
        for grid_size in self._grid_sizes:
            grid_size_numerical = grid_size.value
            grid_size_textual = \
                EnumConverters.grid_size_to_string_converter(grid_size)
            grid_size_caption = "{0} ({1}x{1})".format(grid_size_textual,
                                                       grid_size_numerical)
            self.ui.comboBoxGridSize.addItem(grid_size_caption,
                                             QVariant(grid_size))

        self._player_types[PlayerType.human] = Human.default()
        self._player_types[PlayerType.cpu] = CPU.default()

        player_one_textual = EnumConverters.player_type_to_string_converter(
            PlayerType.human
        )
        player_two_textual = EnumConverters.player_type_to_string_converter(
            PlayerType.cpu
        )

        # fill the player combo boxes
        self.ui.comboBoxPlayerOne.addItem(
            player_one_textual,
            QVariant(self._player_types[PlayerType.human]))
        self.ui.comboBoxPlayerTwo.addItem(
            player_two_textual,
            QVariant(self._player_types[PlayerType.cpu]))
    def _update_status_bar(self):
        session_phase = self.session.session_phase
        message = EnumConverters.session_phase_to_string_converter(
            session_phase
        )

        if session_phase == SessionPhase.fleet_layout:
            fleet = self.session.player_one.fleet
            current_ship = fleet.ships[self._current_ship_index]
            message += self.tr(", Current ship: {0} (size - {1})".format(
                EnumConverters.ship_type_to_string_converter(
                    current_ship.ship_type
                ),
                current_ship.ship_type.value)
            )

        self.statusLabel.setText(self.tr("Phase: " + message))
    def session_phase_output(session):
        """Outputs the current phase of the session.

        :param session: a member of the SessionPhase enum

        """
        session_phase_textual = EnumConverters.session_phase_to_string_converter(session.session_phase)

        print("\n" + Fore.GREEN + "Phase: " + session_phase_textual + "\n", end="")

        print(Style.RESET_ALL)
    def ship_output(ship):
        """Outputs the current ship and its size during the fleet layout phase.

        :param ship: an instance of type Ship

        """
        ship_type_textual = EnumConverters.ship_type_to_string_converter(ship.ship_type)
        ship_dimensions = ship.ship_type.value

        print(Fore.GREEN + "Current ship: {0} (size - {1})".format(ship_type_textual, ship_dimensions))

        print(Style.RESET_ALL)
    def pushButtonStartClick(self):
        """Slot used when the "Start" button is clicked.

        """
        # exctract selected values
        selected_grid_size = self.ui.comboBoxGridSize.itemData(
            self.ui.comboBoxGridSize.currentIndex()
        )
        selected_player_one = self.ui.comboBoxPlayerOne.itemData(
            self.ui.comboBoxPlayerOne.currentIndex())
        selected_player_two = self.ui.comboBoxPlayerTwo.itemData(
            self.ui.comboBoxPlayerTwo.currentIndex())

        # initialize grids and fleets
        selected_player_one.grid = Grid(GridPosition.left, selected_grid_size)
        selected_player_two.grid = Grid(GridPosition.right, selected_grid_size)

        fleet_size_multiplier = \
            EnumConverters.grid_size_to_fleet_size_multiplier_converter(
                selected_grid_size
            )
        selected_player_one.fleet = Fleet(fleet_size_multiplier)
        selected_player_two.fleet = Fleet(fleet_size_multiplier)

        # create a new session
        new_session = Session(SessionType.offline,
                              selected_player_one,
                              selected_player_two)

        # fill the main window's data
        self._parent.session = new_session
        self._parent.grid_size = selected_grid_size
        self._parent.is_new_game = True
        self._parent.current_orientation = ShipOrientation.horizontal
        self._parent.current_ship_index = 0

        player_one_label_text = self._parent.ui.playerOneLabel.text()
        player_one_text = self.ui.comboBoxPlayerOne.currentText()
        player_two_label_text = self._parent.ui.playerTwoLabel.text()
        player_two_text = self.ui.comboBoxPlayerTwo.currentText()

        if not self._parent.has_game_been_started:
            # append players' textual representations
            player_one_label_text += " - " + player_one_text
            self._parent.ui.playerOneLabel.setText(player_one_label_text)
            player_two_label_text += " - " + player_two_text
            self._parent.ui.playerTwoLabel.setText(player_two_label_text)

        self._parent.initialize_grids()

        self.close()
    def _grid_header_output(player_type, header_size):
        # outputs the grids header row
        print(Fore.RED + EnumConverters.player_type_to_string_converter(player_type) + "'s grid")

        print(" " * OutputHelpers._row_space_count, end="")

        for i in xrange(0, header_size):
            # padding the column numbers
            column = i + 1
            column_digits = len(str(column))

            print(Fore.YELLOW + str(column), end=" " * (OutputHelpers._grid_header_space_count - column_digits))

        print(Style.RESET_ALL)
Exemple #7
0
            OutputHelpers.clear_output()
            print(Fore.RED + "Already hit", end="")
            clear = False
            continue

        player_two.shoot(player_one.grid)

        print(Style.RESET_ALL, end="")

        # a fleet has been sunk
        if player_one.fleet.is_sunk():
            winner = player_two
            break
        elif player_two.fleet.is_sunk():
            winner = player_one
            break

    session.session_phase = SessionPhase.game_over
    OutputHelpers.clear_output()
    OutputHelpers.session_phase_output(session)

    print(Fore.GREEN + EnumConverters.player_type_to_string_converter(
        winner.player_type
    ) + " wins")

    OutputHelpers.grid_output(player_one)
    print()
    OutputHelpers.grid_output(player_two, True)

    print(Style.RESET_ALL, end="")