Extra credit help?


#1


Battleship Extra Credit Help

I've finished everything in code academy, so I'm back to battleship to try and make a more complex game
No error messages, but I currently have created 3 ships of 3 different sizes. I have kept them from going off the board, but I'm having some issues trying to get them to not overlap eachother. I have a single function checking to see if the row of my scouting ship interferes with my battleship, but no matter what it returns true.
am i missing something fundamentally wrong?
thanks


from random import randint

#setting up a 10x10 board
board = []
for tiles in range(10):
    board.append(["O"]*10)


#making playing board look nicer. combine list into string
def print_board(board):
    for row in board:
        print("  ".join(row))

#print("Starting a game of battleship!")
#print_board(board)

#make ships and place them
class scouting_ship(object):
    def __init__(self, name, length):
        self.name = "Scouting Ship"
        self.length = 1
    def hide_ship_row(self):
        random_row = randint(0,len(board)-1)
        return random_row
    def hide_ship_col(self):
        random_col = randint(0,len(board)-1)
        return random_col

class battleship(scouting_ship):
    def __init__(self, name, length):
        self.name = "Battleship"
        self.length = 4
    def hide_ship_row(self):
        battleship_loc = []
        #the battleship is 2D so we are associating some length with it. I have arbitrarily chosen the length to be in the row, for a future application I can have a random number of 1 or 0 generated
        # which could correspond if the length goes to a row of column.
        random_row = randint(0,len(board)-1)
        random_row2 = range(random_row,random_row+4)
        for x in random_row2:
            battleship_loc.append(x)
        #make sure our ship does not go off the board, we repeat the previous step until the ship is within bounds
        while battleship_loc[3] > 9:
            battleship_loc = []
            random_row = randint(0,len(board)-1)
            random_row2 = range(random_row,random_row+4)
            for x in random_row2:
                battleship_loc.append(x)
        return(random_row2)
    def hide_ship_col(self):
        random_col = randint(0,len(board)-1)
        return random_col

class submarine(scouting_ship):
    def __init__(self, name, length):
        self.name = "Submarine"
        self.length = 2
    def hide_ship_row(self):
        random_row = randint(0,len(board)-1)
        return(random_row)
    def hide_ship_col(self):
        submarine_loc = []
        random_col = randint(0,len(board)-1)
        random_col2 = range(random_col,random_col+2)
        for x in random_col2:
            submarine_loc.append(x)
        while submarine_loc[1] > 9:
            submarine_loc = []
            random_col = randint(0,len(board)-1)
            random_col2 = range(random_col,random_col+2)
            for x in random_col2:
                submarine_loc.append(x)
        return (random_col2)

def overlap(hide_ship_row, hide_ship_col):
    if scouting_ship.hide_ship_row(board) == battleship.hide_ship_row(board):
        return False
     else:
        return True
    

print(scouting_ship.hide_ship_row(board))
print(battleship.hide_ship_row(board))

print(overlap(scouting_ship, battleship))

#print("Coordinates of the scouting ship (row, col)")
#print(scouting_ship.hide_ship_row(board))
#print(scouting_ship.hide_ship_col(board))
#print("Coordinates of the battleship (row, col)")
#print(battleship.hide_ship_row(board))
#print(battleship.hide_ship_col(board))
#print("Coordinates of the submarine (row, col")
#print(submarine.hide_ship_row(board))
#print(submarine.hide_ship_col(board))


#2

There is a topic in the Corner Bar that explores multiple ships. If I recall, the overlap problem is dealt with using a checksum. I'll see if I can locate the thread.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.