:col: 0-based column :row: 0-based row :direction: "H" or "V" LEt's create a player and place a ship: > player = Player('Jane') > destroyer = Destroyer() > ace_ship(destroyer, 1, 2, "V") Did it place the destroyer on all the cells for it? > player._board is destroyer True > player._board is destroyer True Did it get the coordinates right? > ords Is the destroyer the only ship in their player's arsenal? > player._ships = True """ # Have ship determine it's own coordinates ship. _ships = def place_ship ( self, ship, col, row, direction ): """Place a ship on the board at col, row, going in direction. _board = None # will be 10x10 matrix (list-of-lists) for board _ships = None # will be list of living ships for user def _init_ ( self, name ): """Create player: - set up their board - set up their (empty initially) list of ships Let's create a player: > player = Player('Jane') They should have an empty board: > player._board # doctest: +NORMALIZE_WHITESPACE, ,, ,, ,, ,, ] THey should have no ships: > player._ships They should have no opponent (yet!): > player.opponent is None True """ self. In this case, this says clearly that # the other classes shouldn't mess with this stuff.
#Battleship make a list how to#
It's smart to note # which attributes/methods are private, so it's more clear for others # how to use your class. """ name = "" # will be name for player opponent = None # will be the opponent object for this player # These attributes are private - other classes shouldn't # need to inspect or change them directly. Players have a board with their ships/opponent's moves, their ships, their name, and a link to their opponent. ValueError: Illegal direction """ class AircraftCarrier ( Ship ): _length = 5 name = "Aircraft Carrier" class Destroyer ( Ship ): _length = 2 name = "Destroyer" class Submarine ( Ship ): _length = 3 name = "Submarine" class Battleship ( Ship ): _length = 4 name = "Battleship" # List of ship types, in the order player shoud place them SHIP_TYPES = class Player ( object ): """Object for a player.
![battleship make a list battleship make a list](https://hative.com/wp-content/uploads/2014/10/newspaper-craft-fashion-ideas/14-creative-newspaper-craft-fashion-ideas.jpg)
name = "Test Ship" Let's make a ship and place it: > ship = TestShip() > ace(1, 2, "H") > ords > ace(1, 2, "V") > ords Illegal directions raise an error: > ace(1, 2, "Z") Traceback (most recent call last).
![battleship make a list battleship make a list](https://i.ytimg.com/vi/4JAdXERo4Oc/maxresdefault.jpg)
This is meant to be an abstract class-you should subclass it for individual ship types. Raises an exception for an illegal direction.
#Battleship make a list update#
Given a row and column and direction, determine coordinates ship will occupy and update it's coordinates property. _length def place ( self, col, row, direction ): """Place ship. coords = def is_sunk ( self ): """Is this ship sunk?""" return self. name, "Must subclass with length and name!" self. These functions print out a message # in a console color if the console appears to be a live TTY (that is, not # being piped out/in a test/etc.) NO_COLOR = False # If the colors annoy you, set this to True WATER = lambda msg : ( ' \033 # list of (col, row) coordinates this ship occupies def _init_ ( self ): assert self.
![battleship make a list battleship make a list](https://c2.staticflickr.com/4/3044/2703842451_eaef4e0367_b.jpg)
Joel Burton """ import random import sys # If set to true, print board without so much spacing # On bigger screens, you can set this to False TIGHT = True # Colors # You can safely ignore this. The ships are all subclasses of the Ship class, which handles logic around knowing when a ship is sunk, how to place a ship on the board, etc. It also has a link to the other player ("opponent"), which makes it easy to find the other player. It holds information about their name, board, and ships. The Player class represents an individual player. The Game class represents a game and holds the state of the game (player1, player2, and the current player. It's a good demonstration of class-based game design. """Hackbright Battleship! This is an object-oriented version of Battleship.