Ejemplo n.º 1
0
    def __play_one_round(self):
        """
         The function runs one round of the game:
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits
             and terminated ships)
        :return: none.
        """

        hit_list = list()
        curr_round_hits = list()
        not_hit_list = list()

        user_coord = gh.get_target(self.__board_size)
        self.__bombs_dict[user_coord] = 4

        curr_round_hits, hit_list, not_hit_list = self.move_ship(
            self.__ships, curr_round_hits, hit_list, not_hit_list, user_coord)
        self.update_bombs()
        print(
            gh.board_to_string(self.__board_size, curr_round_hits,
                               self.__bombs_dict, hit_list, not_hit_list))
        terminated_count = self.remove_terminated_ships()
        gh.report_turn(len(curr_round_hits), terminated_count)
Ejemplo n.º 2
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        # Report game instructions
        gh.report_legend()

        # Initialize ship coordinate for display!
        list_of_ships_coordinates = list()

        for ship in self.__ships:
            list_of_ships_coordinates.extend(ship.coordinates())

        # print board to screen
        print(
            gh.board_to_string(self.__board_size, self.__hits, self.__bombs,
                               self.__hit_ships, list_of_ships_coordinates))

        # print the game board
        while self.__ships:
            self.__play_one_round()

        # When all ship were destroyed
        gh.report_gameover()
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        Te function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits
            and terminated ships)
        :return:
            (some constant you may want implement which represents) Game
            status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """

        # Get user coordinates
        self.update_bombs()

        new_bomb = gh.get_target(self.board_size)
        self.bombs_dict[new_bomb] = BOMBS_LIFE
        current_turn_hits, current_turn_terminations = self.update_ships()

        print(
            gh.board_to_string(self.board_size, current_turn_hits,
                               self.bombs_dict, self.total_hit_coord,
                               self.total_ships_coord))
        gh.report_turn(len(current_turn_hits), len(current_turn_terminations))
Ejemplo n.º 4
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        target_bomb = gh.get_target(Game.get_board_size(self))
        for ship in self.__ships:
            if not ship.damaged_cells():
                ship.move()
                for coordinate in ship.coordinates():
                    if coordinate in self.__bombs.keys():
                        self.__current_turn_hits.append(coordinate)

            elif ship.terminated():
                self.__ships.remove(ship)

            elif not ship.hit(target_bomb):
                self.__bombs[target_bomb] = LONGEVITY
            else:
                self.__current_turn_hits.append(target_bomb)

        for bomb in self.__bombs:
            if self.__bombs[bomb] == DETONATION:
                del self.__bombs[bomb]
            else:
                self.__bombs[bomb] -= 1

        gh.board_to_string(self.__board_size, self.__current_turn_hits,
                           self.__bombs, self.__hit_coordinates,
                           self.__not_hit_coordinates)
        self.__current_turn_hits.clear()
        gh.report_turn(self.get_hit_ships(), self.get_terminateted_ships())
Ejemplo n.º 5
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        for ship in self.__ships:
            self.__not_hit_coordinates.extend(ship.coordinates())

        gh.report_legend()

        gh.board_to_string(self.get_board_size(), [], {}, [],
                           deepcopy(self.get_not_hit_coordinates()))

        ships_quantity = len(self.__ships)

        while self.__terminated_ships != ships_quantity:
            self.__play_one_round()
        return None
Ejemplo n.º 6
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        hits = []
        ships_terminated = 0
        still_alive_ships = []
        new_intact_cells = []
        damaged_cells = []
        new_bombs = {}
        target = gh.get_target(self.__board_size)
        self.bombs[target] = self.NEW_BOMB

        for ship in self.__ships:
            ship.move()
            for bomb in self.bombs:
                if ship.hit(bomb):
                    hits.append(bomb)
                damaged_cells.extend(ship.damaged_cells())
            new_intact_cells.extend(ship.intact_cells())
            self.intact_cells = new_intact_cells

        for ship in self.__ships:
            if ship.terminated():
                ships_terminated += 1
            else:
                still_alive_ships.append(ship)
        self.__ships = still_alive_ships

        for bomb in self.bombs:
            if bomb != target:
                self.bombs[bomb] -= 1
            if self.bombs[bomb] != 0 and bomb not in hits:
                new_bombs[bomb] = self.bombs[bomb]
        self.bombs = new_bombs
        print(
            gh.board_to_string(self.__board_size, hits, self.bombs,
                               damaged_cells, self.intact_cells))

        if len(self.__ships) == 0:
            self.__game_status = self.GAME_STATUS_OVER
        gh.report_turn(len(hits), ships_terminated)
        return self.__game_status
Ejemplo n.º 7
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        Te function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        intact_ships = []
        round_hits = 0
        round_terminations = 0

        usr_inpt = gh.get_target(self.board_size)

        self.bombs[usr_inpt] = 4

        for ship in self.ships:
            ship.move()

            for bomb in self.bombs.keys():
                if bomb in ship.coordinates():
                    round_hits += 1
                    ship.hit(bomb)
                    self.hits.append(bomb)
                    for coordinate in ship.coordinates():
                        if coordinate not in self.hit_ships:
                            self.hit_ships.append(coordinate)

                    if ship.terminated():
                        round_terminations += 1
                        self.ships.remove(ship)

        for ship in self.ships:
            if ship not in self.hit_ships:
                for coordinate in ship.coordinates():
                    intact_ships.append(coordinate)

        self.bombs = {
            bomb: (self.bombs[bomb] - 1)
            for bomb in self.bombs.keys()
            if self.bombs[bomb] > 0 and self.bombs[bomb] not in self.hits
        }

        print(
            gh.board_to_string(self.board_size, self.hits, self.bombs,
                               self.hit_ships, intact_ships))
        gh.report_turn(round_hits, round_terminations)
Ejemplo n.º 8
0
 def play(self):
     """
     The main driver of the Game. Manages the game until completion.
     :return: None
     """
     gh.report_legend()
     print(
         gh.board_to_string(self.__board_size, [], self.bombs, [],
                            self.intact_cells))
     while self.__game_status != self.GAME_STATUS_OVER:
         self.__play_one_round()
     gh.report_gameover()
Ejemplo n.º 9
0
 def play(self):
     """
     The main driver of the Game. Manages the game until completion.
     :return: None
     """
     gh.report_legend()
     print(
         gh.board_to_string(self.__board_size, [], self.__bombs, [],
                            self.not_hitted_coordinates()))
     # if there is no ships on the game then stop playing!
     while len(self.__ships) > 0:
         self.__play_one_round()
     gh.report_gameover()
Ejemplo n.º 10
0
 def play(self):
     """
     The main driver of the Game. Manages the game until completion.
     completion.
     :return: None
     """
     gh.report_legend()
     print(
         gh.board_to_string(self.__board_size, [], self.__bombs,
                            self.__hits,
                            self.get_coordinates_from_ship_list()))
     while len(self.__ships) > 0:
         self.__play_one_round()
     gh.report_gameover()
Ejemplo n.º 11
0
 def play(self):
     """
     The main driver of the Game. Manages the game until completion.
     completion.
     :return: None
     """
     gh.report_legend()
     all_ships_coordinates = []
     for ship in self.__ships:
         all_ships_coordinates += ship.coordinates()
     print(gh.board_to_string(self.__board_size, [], self.__bomb_on_board,
                              [], all_ships_coordinates))
     while len(self.__ships) != 0:
         self.__play_one_round()
     gh.report_gameover()
Ejemplo n.º 12
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        number_of_terminated = 0
        # getting the coordinates of the new bomb
        target = gh.get_target(self.__board_size)
        self.__bombs[target] = NEW_BOMB
        self.move_all_ship()
        this_turn_hits = self.update_ship_on_hit()

        if self.__ships:
            for i in self.__bombs:
                self.__bombs[i] -= 1
            for key in list(self.__bombs):  # remove hits that are 3 turns
                if self.__bombs[key] <= 0:
                    del self.__bombs[key]

        not_hited_coordinate = self.not_hitted_coordinates()
        hited_coordinate = self.coordinates_of_hited_ships()
        print(
            gh.board_to_string(self.__board_size, this_turn_hits, self.__bombs,
                               hited_coordinate, not_hited_coordinate))

        for ship in self.__ships:  # remove ship if terminated
            if ship.terminated():
                self.__ships.remove(ship)
                number_of_terminated += 1
        gh.report_turn(len(this_turn_hits), number_of_terminated)
Ejemplo n.º 13
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        bomb_pos = gh.get_target(self.__board_size)
        self.__bombs.append(Bomb(bomb_pos))

        hits = []
        for ship in self.__ships:
            ship.move()
            for bomb in self.__bombs:
                if ship.hit(bomb.get_pos()):
                    hits.append(bomb.get_pos())
                    self.__hit_ships_pos.add(ship.get_pos())

        not_hit_ships_pos = [
            ship.get_pos() for ship in self.__ships if not ship.is_hit()
        ]
        bombs_dict = {
            bomb.get_pos(): bomb.get_turns_left()
            for bomb in self.__bombs
        }
        print(
            gh.board_to_string(self.__board_size, hits, bombs_dict,
                               list(self.__hit_ships_pos), not_hit_ships_pos))

        self._update_bombs_one_round()
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        completion.
        :return: None
        """

        gh.report_legend()
        print(
            gh.board_to_string(self.board_size, [], {}, [],
                               self.get_total_ships_coords()))
        while len(self.terminated_ships) < self.initial_ship_amount:
            self.__play_one_round()
        clear_last_bombs = []
        for bomb in self.bombs_dict:
            for ship in self.ships_list:
                if bomb in ship.coordinates():
                    clear_last_bombs.append(bomb)
        for bomb in clear_last_bombs:
            del self.bombs_dict[bomb]
        self.ships_list = []
        gh.report_gameover()
Ejemplo n.º 15
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        completion.
        Initializes ship's orientation and coordinates.
        Plays rounds until all ships have been terminated.
        :return: None
        """

        gh.report_legend()

        for ship in self.__ships:
            ship.set_orientation()

        ship_coords = [ship.coordinates() for ship in self.__ships]
        ship_coords = [i for lst in ship_coords for i in lst]

        print(gh.board_to_string(self.__board_size, [], {}, [], ship_coords))

        while self.__ships:
            self.__play_one_round()

        gh.report_gameover()
Ejemplo n.º 16
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        Te function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        new_bomb = gh.get_target(self.__board_si



        ze)
        self.__bomb_on_board[new_bomb] = 4
        self.ship_mover()
        self.damaged_ships()
        self.bombs_life_time()
        hits = self.last_hit()
        self.remove_items(hits[0])
        self.not_damaged_pos()
        self.damaged_pos()
        terminations = self.terminated_ships()
        gh.report(gh.board_to_string(self.__board_size, hits[0],
                                     self.__bomb_on_board,
                                     self.__hit_ship_cells,
                                     self.__healthy_ships))
        gh.report_turn(hits[1], terminations)
Ejemplo n.º 17
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round
            (number of hits and terminated ships)
        :return:
            (some constant you may want implement which represents)
            Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """

        # Get target
        target = gh.get_target(self.__board_size)

        # Put the target on board
        self.put_target(target)

        # Move all ship that were not hit in last turn
        for ship in self.__ships:
            ship.move()

        # Check if ship was hit by an active bomb(also from last turns) need
        # to mention that this checking is made only after moving all ships!

        hit_count = 0

        # list of bombs that we should keep for future rounds
        bombs_to_delete = list()

        # Check if its a hit! and append a new hit to hit list. and also
        # count it.
        bombs_list_copy1 = deepcopy(self.__bombs)
        for ship in self.__ships:
            for bomb in bombs_list_copy1:
                # Check if there was a direct hit
                if ship.hit(bomb):
                    # Counter that count hits!
                    hit_count += 1
                    if bomb not in self.__hits:
                        self.__hits.append(bomb)
                    # add bomb to delete soon
                    bombs_to_delete.append(bomb)

        # For each ship distribute tuples accordingly if they are hit or not
        # each for the related list
        self.__hit_ships = []
        self.__not_hit_ships = []

        for ship in self.__ships:
            self.__hit_ships.extend(ship.damaged_cells())
            self.__not_hit_ships.extend(ship.not_damaged_cells())

        # lessen the span life of each bomb and delete the bombs with 1 span
        bombs_list_copy = deepcopy(self.__bombs)

        if self.__bombs:
            for bomb in bombs_list_copy:
                if self.__bombs[bomb] == 1:
                    del self.__bombs[bomb]
                else:
                    self.__bombs[bomb] -= 1

        # prints board to screen
        print(
            gh.board_to_string(self.__board_size, self.__hits, self.__bombs,
                               self.__hit_ships, self.__not_hit_ships))

        # delete the bombs that hit a target or some of them.
        set(bombs_to_delete)
        for bomb in bombs_to_delete:
            if bomb in self.__bombs:
                del self.__bombs[bomb]

        bombs_to_delete = []

        # remove the terminated ships from game
        terminated = 0
        ship_list = list()

        # Remove last turn hit from hit list
        self.__hits = []

        for ship in self.__ships:
            if not ship.terminated():
                ship_list.append(ship)
            else:
                terminated += 1

        # Update the ship list
        self.__ships = ship_list

        # Report turn result to screen
        gh.report_turn(hit_count, terminated)
Ejemplo n.º 18
0
 def __play_one_round(self):
     """
     Te function runs one round of the game :
         1. Get user coordinate choice for bombing.
         2. Move all game's ships.
         3. Update all ships and bombs.
         4. Report to the user the result of current round (number of hits and
          terminated ships)
     """
     target = gh.get_target(self.__board_size)
     # Put the bomb on board
     self.__bombs[target] = TURNS
     current_hits_list = []
     number_of_terminated_in_current_turn = INITIALIZE_VALUE
     number_of_hit_ships_in_current_turn = INITIALIZE_VALUE
     # 1. Move the ships if it was not hit
     # 2. Each ship that moved, check if a bomb (including the new target)
     #       hit in ship in the new location
     # 3. For all not moved ships check if the new target hits
     for ship in self.__ships:
         # Step 1:
         if ship not in self.__hit_ships:
             ship.move()
             # Step 2:
             previous_damaged_cells = tuple(ship.damaged_cells())
             for bomb in self.__bombs:
                 ship.hit(bomb)
             if len(ship.damaged_cells()) != len(previous_damaged_cells):
                 self.__hit_ships.append(ship)
                 for cell in ship.damaged_cells():
                     self.__hits.append(cell)
                     number_of_hit_ships_in_current_turn += 1
                     current_hits_list.append(cell)
         # Step 3:
         else:
             if ship.hit(target):
                 current_hits_list.append(target)
                 self.__hits.append(target)
                 number_of_hit_ships_in_current_turn += 1
     # Decrease bombs turns
     ezer_dict_bombs = copy.deepcopy(self.__bombs)
     for index_bomb in ezer_dict_bombs:
         self.__bombs[index_bomb] -= 1
         if  self.__bombs[index_bomb] < MIN_NUMBER_TURNES or \
                         index_bomb in current_hits_list:
             del self.__bombs[index_bomb]
     # Print board
     print(
         gh.board_to_string(self.__board_size, current_hits_list,
                            self.__bombs, self.__hits,
                            self.get_coordinates_from_ship_list()))
     # Check if ship terminated
     for ship in self.__ships:
         if ship.terminated():
             number_of_terminated_in_current_turn += 1
             self.__hit_ships.remove(ship)
             self.__ships.remove(ship)
             for cell in ship.coordinates():
                 self.__hits.remove(cell)
     # Report
     gh.report_turn(number_of_hit_ships_in_current_turn,
                    number_of_terminated_in_current_turn)
Ejemplo n.º 19
0
 def print_board(self):
     """prints current board, using board_to_string"""
     board_len, hits, bombs, hit_ships, ships = self.board_str_prep()
     brd = gh.board_to_string(board_len, hits, bombs, hit_ships, ships)
     print(brd)
     return None