Expanding Battleship game


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/5?curriculum_id=4f89dab3d788890003000096


Trying to figure out how to add a second hit to the battleship game. I have the second hit registering right, but the "break" doesn't register when I guess both correctly and then cycles back to the first point. Also if I guess wrong with the second point, it cycles back to the first as well. It also only adds to turn after I've gone through both points; meaning I'm getting twice as many guesses as I put out.


from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

def random_row1(board):
    return randint(0, len(board) - 1)

def random_col1(board):
    return randint(0, len(board[0]) - 1)

def random_row2(board):
    return randint(0, len(board[0]) - 1)
    
def random_col2(baord):
    return randint(0, len(board[0]) - 1)
    
battleship_row1 = random_row1(board)
battleship_col1 = random_col1(board)
battleship_row2 = random_row2(board)
battleship_col2 = random_col2(board)
print battleship_row1
print battleship_col1
print battleship_row2
print battleship_col2

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(6):
    class battleship(object):
        guess_row1 = int(raw_input("Guess Row1:"))
        guess_col1 = int(raw_input("Guess Col1:"))
        for battleship in range(2):
            if guess_row1 == battleship_row1 and guess_col1 == battleship_col1:
                print "That's a hit!"
                board[guess_row1][guess_col1] == "X"
            else:
                if (guess_row1 < 0 or guess_row1 > 4) or (guess_col1 < 0 or guess_col1 > 4):
                    print "Oops, that's not even in the ocean."
                elif(board[guess_row1][guess_col1] == "X"):
                    print "You guessed that one already."
                else:
                    print "You missed my battleship!"
            print turn + 1
            board[guess_row1][guess_col1] = "X"
            guess_row2 = int(raw_input("Guess Row2:"))
            guess_col2 = int(raw_input("Guess Col2:"))
            if guess_row2 == battleship_row2 and guess_col2 == battleship_col2:
                print "Congratulations! You sunk my battleship!"
                break
            else:
                if (guess_row2 < 0 or guess_row2 > 4) or (guess_col2 < 0 or guess_col2 > 4):
                    print "Oops, that's not even in the ocean."
                elif (board[guess_row2][guess_col2] == "X"):                        print "You guessed that one already."
                else:
                    print "You missed my battleship!"
                    board[guess_row2][guess_col2] = "X"
                print turn + 1
                print_board(board)
                if turn == 5:
                    print "Game Over"


#2

By 'second hit' do you mean second ship?


Defining a class in a loop is not possible.

Away from any repeated constructs we could define a ship class:

class Ship(object):
    def __init__(self, row=0, col=0):
        self.y = row
        self.x = col

Now we can arbitrarily assign row and col to create the instance, or create a blank instance and set the attributes later.

    def setRow(self,row):
        self.y = row
    def setCol(self,col):
        self.x = col

ship1 = Ship()
ship1.setRow(randint(0, len(board)-1))
ship1.setCol(randint(0, len(board[0])-1))

ship2 = Ship()
ship2.setRow(randint(0, len(board)-1))
ship2.setCol(randint(0, len(board[0])-1))

For now let's assume that the chance of them being the same is Nil. This validation can be covered later.

To check a guess, we can shorten the work by using tuples.

Eg.

>>> (1,2) == (1,2)
True
>>>

The ship positions are,

a = (ship1.y, ship1.x)
b = (ship2.y, ship2.x)

The guess is,

c = (guess_row, guess_col)

    if c == a or c == b:

Wrack you brains on this one for a little while and you will see where this is going. I'll leave you to it...


#3

okay, I can see where I was making a mistake with how I was choosing to work it. Thanks for helping see how to do two separate ships. But my earlier question was actually referring to one ship that takes two hits to sink.


#4

Then make the two tuples the fore and aft coordinates of the vessel.


#5

alright...I'll give that a shot.


#6

Still along the lines of earlier, two ships...

>>> class Ship(object):
    def __init__(self, row=0, col=0):
        self.y = row
        self.x = col
    def setRow(self, row):
        self.y = row
    def setCol(self, col):
        self.x = col
        
>>> ship1 = Ship()
>>> ship1.setRow(randint(0, len(board)-1))
>>> ship1.setCol(randint(0, len(board[0])-1))
>>> ship2 = Ship(2, 2)
>>> a = (ship1.y, ship1.x)
>>> b = (ship2.y, ship2.x)
>>> guess_row = 4
>>> guess_col = 2
>>> c = (guess_row, guess_col)
>>> c == a
False
>>> c == b
False
>>> a, b, c
((1, 0), (2, 2), (4, 2))

We can devise a class of ship with fore and aft components along these same lines.


#7

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