def place_ship(self, board): """ Changes correct square's *is_element_of_ship* attribute to True Parameters ------- board: :obj: Returns: ------- None """ row = self.start_row column = self.start_column is_close = self.check_enviroment(board) if is_close is False: for i in range(self.size): board[row][column].is_element_of_ship = True self.squares.append(board[row][column]) if self.direction == "right": column += 1 elif self.direction == "left": column -= 1 elif self.direction == "up": row -= 1 elif self.direction == "down": row += 1 else: Ui.print_message("Can\'t place a ship here!\n")
def turn(self): while not self.is_over: Ui.print_message(self.player_in_round.name) Ui.print_message(self.player_in_round.ocean) if self.player_in_round == self.player1: shot_done = False while not shot_done: shot_done = self.player_in_round.shot(self.player2.ocean) else: shot_done = False while not shot_done: shot_done = self.player_in_round.shot(self.player1.ocean) sleep(0.00) self.is_over = self.is_game_over() self.player_switch() if self.is_over: print(self.player1) print(self.player2) if isinstance(self.player1, Player) or isinstance( self.player2, Player): self.add_player_to_highscore()
def start_simulation(): ''' Makes two *Computer* objects and starts the simulation mode. Returns ------- None ''' for level in Computer.allowed_levels: Ui.print_message("Option: {} ".format(level)) proper_level = False while not proper_level: levels = Ui.get_inputs(["Level", "Level"], "Choose computer\'s level") comp1_level = levels[0] comp2_level = levels[1] if level in Computer.allowed_levels: proper_level = True computer1 = Computer(comp1_level) computer2 = Computer(comp2_level) computer2.name = "Computer2" gameplay = Game(computer1, computer2) gameplay.start_game()
def set_ships(self): """ - Asks player for coordinates to place ships on his ocean, checks if it's possible. If yes, places ship on the provided coordinates. If no, asks for coordinates again. Returns: -------- None """ alphanumeric_dict = dict( [[item for item in pair[::-1]] for pair in enumerate(self.ocean.alphabet_list[:])]) for name, lenght in Ship.sizes.items(): is_close = True while is_close: ship_specification = Ui.get_inputs( ["Row", "Column", "Direction"], "\nPlease place {} which length is {}".format( name, lenght)) row = int(ship_specification[0]) - 1 column = alphanumeric_dict[ship_specification[1].upper()] ship = Ship(name, row, column, ship_specification[2]) is_close = ship.check_enviroment(self.ocean.board) ship.place_ship(self.ocean.board) Ui.print_message(self.ocean) self.ocean.add_ship_to_ocean(ship)
def main(): ''' Function start the program. ''' option = float("inf") while not (option == "0"): Ui.display_screen("screens/menu.txt") option = choose()
def set_size(self): """ Sets *size* attribute based on name of ship. Returns ------- size: :obj: """ try: size = Ship.sizes[self.name] except KeyError: Ui.print_message("Provided ship name is wrong.") return size
def start_multiplayer(): ''' Makes two *Player* objects and starts the multiplayer mode. Returns ------- None ''' players = Ui.get_inputs(["First player's name", "Second player's name"], "Please provide your names") player1 = Player(players[0]) player2 = Player(players[1]) gameplay = Game(player1, player2) gameplay.start_game()
def start_singleplayer(): ''' Makes *Player* and *Computer* objects and starts the singleplayer Returns ------- None ''' data = Ui.get_inputs( ["Name", "Level"], "Please provide your name and choose difficulty level") player1 = Player(data[0]) level = data[1] computer = Computer(level) gameplay = Game(player1, computer) gameplay.start_game()
def start_game(self): """ Calls set_ships function for both players, and draws which one will shot first. Returns: ------- None """ first_player_number = randint(1, 2) if first_player_number == 1: self.player_in_round = self.player1 else: self.player_in_round = self.player2 self.player1.set_ships() self.player2.set_ships() self.turn() exit = Ui.get_inputs(["Press any number to exit"], "")
def choose(): """ Calls proper functions based on user's choice. Returns: -------- option: string It is the number which user choose from main menu. """ option = Ui.get_inputs(["Please choose an option"], "") option = option[0] if option == "1": start_singleplayer() elif option == "2": start_multiplayer() elif option == "3": start_simulation() elif option == "4": Ui.display_screen("screens/credits.txt", True) elif option == "5": Ui.display_screen("screens/highscore.txt") HallOfFame.print_highscore("Interface/hall_of_fame.csv") exit = "1" while not exit == "0": exit = Ui.get_inputs(["Press 0 to exit"], "") exit = exit[0] elif option == "0": Ui.print_message("Good bye!") return option
def shot(self, enemy_ocean): """ Takes input with coordinates to shot from player, then checks if player can shot there. - If yes, changes *is_hit* attribute of correct square to True. - If no, asks for coordinates again. Parameters ---------- enemy_ocean: :obj: Object of Ocean class. Returns ------- board_to_display: boolean """ proper_coordinates = False alphanumeric_dict = dict( [[item for item in pair[::-1]] for pair in enumerate(self.ocean.alphabet_list[:])]) coordinates = Ui.get_inputs(["Row", "Column"], "Where do you want to shot?") row = int(coordinates[0]) - 1 column = alphanumeric_dict[coordinates[1].upper()] square = enemy_ocean.board[row][column] enemy_square = self.ocean.enemy_board[row][column] if not square.is_hit: square.hit() if square.is_element_of_ship: enemy_square.is_element_of_ship = True enemy_square.hit() self.counted_shots += 1 return True else: return False