Extra credit 19/19 multiple ships


#1

Hello all,

Please bear with me. I am a code newbie. I'm trying to do multiple ships, but the results i got is not what i expected. Can someone help me,please? Thank you.


#2

Well to generate multiple ships you have to have several checks and a few while loops/try statements.

You have to have a good data structure for storing values, and a dict just so happens to be perfect for this task.

board = {(x, y): None for x in range(1, len(board) + 1) for y in range(1, len(board) + 1)}

Now that we have a good board let's move on.

Place a ship and check it it's occupied or out of bounds. I am going to use a ternary function for this. look it up it is really useful for this.

def place_ship_section(cord, ship):
    board[cord] = ship.name if board[cord] == None or 'miss' else ValueError

Now we need to place whole ships and call on our ship section function.

def place_ship(cord, ship):
    hold_board = board
    for length in range(ship()):
        try:
            place_ship_section(cord, ship)
        except ValueError:
            print("Failed To place ship, reverting board")
            board = hold_board
            place_ship()
            break
    else:
        print("%s placed" % ship)

Ok, now that we can place whole ships let's make a class that contains the information of the ship we need to place. Doing this will make it easier in the long run so we don't have 400 different variables for our ships.

class ShipBlueprint(object):
    def __init__(self, ship_length, ship_name):
        self.shipLength = ship_length
        self.shipName = ship_name

    def get_name(self):
        return self.__dict__['shipName']

    def get_length(self):
        return self.__dict__['shipLength']
    def __call__(self):
        return self.length

Ok let's make it so we have a fleet of ships!

fleet = [
    ShipBlueprint(5, 'Aircraft Carrier',),
    ShipBlueprint(4, 'Battleship'),
    ShipBlueprint(3, 'Submarine'),
    ShipBlueprint(3, 'Destroyer'),
    ShipBlueprint(2, 'Patrol Boat')
]

Now that we have all that let's create our fleet!

for number, ship in enumerate(fleet):
    place_ship(coord, ship)

There might be a few more things you need but you should get the gist of what's going on from this.

Any questions?

While it may look like a lot it really is not and it is all nessiary for ship placement.

Here are the steps you need to create your own code:

  1. A list of the items to be placed, with pertinent data for placement.
  2. A way to place your ships(whole)
  3. A way to verify ship placement, section by section or at once
  4. A way to loop through your 'fleet' while placing ships!

Here is a link to my thread on here with links to my code that I ahve done you may take a gander at it and please post your efforts in my post once you get done. I would love to see what you come up with.

THE BATTLESHIP EXTRA CREDIT MEGA POST!

As always best of luck!


#3

I forgot to mention that,

This will look JUST like this,

{(1, 1): None, (1, 2): "Battleship", ...}

So as you can see it is way better than a list because we can store at positions and have data at that position.