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)
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))
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)
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
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())
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)
def __play_one_round(self): """ This 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 number of hits and terminated ships :return: Game status : GAME_STATUS_ONGOING if there are still ships on the board or GAME_STATUS_ENDED otherwise. """ self.update_bombs() self.place_bomb() self.move_ships() self._last_turn_hits = [] self._last_turn_kills = 0 self.do_hits() self.cleanup_bombs() self.print_board() gh.report_turn(len(self._last_turn_hits), self._last_turn_kills) return self._game_status
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)
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)
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)