def perform_turn(self, opponent):
     """
     The player's turn is performed. He is shown his opponent's board
     with all that he has currently acchieved.
     He is also shown his own board with what the opponent acchieved.
     He is then promted to chose a point on the grid to attack.
     The result of the attack is evaluated and presented to him.
     If he wins, the game ends. Otherwise,
     he is asked to hand over to his opponent.
     """
     print("{}'s' board:".format(opponent.name))
     opponent.board.print(hide_ships=True)
     print("Your board:")
     self.board.print()
     attack_point = self.get_attack_point(opponent)
     clear_screen()
     opponent.point_take_hit(attack_point, self)
     opponent.board.print(hide_ships=True)
     if opponent.defeated:
         print("Congratulations, {}! You won the game!".format(self))
         print("That is what {} has guessed so far from you:".format(
             opponent))
         self.board.print()
         sys.exit()
     else:
         self.handover_control()
Esempio n. 2
0
 def setup_player(self, player):
     """
     This is the single player setup of their battleships.
     """
     clear_screen()
     input("{}! It is your turn to set up your battleships.\n"
           "Press Enter when you are ready to do that."
           .format(player))
     player.setup_ships()
 def __init__(self, player, board):
     """
     The ship is set up on the board.
     """
     clear_screen()
     board.print()
     print('{}, how do you want to set up your {} of length {}?'
           .format(player, self, self.length))
     self.get_direction()
     self.get_location(board)
Esempio n. 4
0
 def get_turn(self, player):
     """
     This prompts a player to take his turn. When he is ready,
     he hits any key, then the screen is cleared an his turn starts.
     """
     clear_screen()
     input("It is {}'s turn!\n{}, "
           "press Enter when you are ready to start you turn."
           .format(player, player))
     clear_screen()
     opponent_player = self.get_opponent_in_game(player)
     player.perform_turn(opponent_player)
Esempio n. 5
0
 def get_players(self):
     """
     This gets the names of the players and prompts them to continue into
     the single player setup for both players.
     """
     clear_screen()
     print('Welcome to the Battleship game. You need two players. '
           '\nPlease give the name of the first player:')
     self.player1 = Player()
     print("Thank you. So {} is playing.\n"
           "Now please provide the second player's name!"
           .format(self.player1))
     self.player2 = Player(opponent=self.player1)
     input('Great {} and {}! '
           'Press Enter when you are ready to start the game.'
           .format(self.player1, self.player2))
 def setup_ships(self):
     """
     The player's ships are set up here. Afterward the player's board
     is printed and he is asked to hand over to his opponent.
     """
     print('Hello {}, now it is time for you to set up your battleships!'.
           format(self))
     self.board.print()
     self.ships_afloat = []
     self.ships_afloat.append(BattleShip(self, self.board))
     self.ships_afloat.append(Submarine(self, self.board))
     self.ships_afloat.append(Cruiser(self, self.board))
     self.ships_afloat.append(AircraftCarrier(self, self.board))
     self.ships_afloat.append(PatrolBoat(self, self.board))
     clear_screen()
     print("Bravo {}, you are done with your setup. It looks fantastic:".
           format(self))
     self.board.print()
     self.handover_control()