# Extra credit: several ships with different sizes

#1

Codecademy error message "Something went wrong", after I added a line of code in an if statement.
I created 2 ships size 1 and 1 horizontal big ship size 2. It is composed of 2 parts. The problem occurs in lines describing the following situation:

• Part 1 was touched in a precedent guess
• Then part 2 gets hit

I think there is the pb in my code is how I referred to part 1's position when part 2 is hit.

When a part gets hit, its position should be marked with the character '|'. Then it should be checked wether the other part has already been marked in that way. I can't figure out how to refer to that other part properly.

``````from random import randint

# create a numbered board dimension 7*7
board = []
for x in range(7):
board.append(['O'] * 7)
board[x].insert(7, ' ' + str(x))
board.append([' '])
board.append(['0', '1', '2', '3', '4', '5', '6'])
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
print "Let's play Battleship!"

# create 2 distinct ships size 1
def random(board):
return randint(0, len(board) - 2)
ship_1 = [int, int]
ship_2 = [int, int]
while ship_1 == ship_2:
ship_1[0] = random(board)
ship_1[1] = random(board)
ship_2[0] = random(board)
ship_2[1] = random(board)

# create 1 horizontal ship size 2
ship_3a = [int, int]
ship_3b = [int, int]
while ship_3a == ship_1 or ship_3a == ship_2 or ship_3b == ship_1 or ship_3b == ship_2 or ship_3a == ship_3b:
ship_3a[0] = random(board)
ship_3a[1] = random(board)
ship_3b[0] = ship_3a[0]
if ship_3a[1] == 4:
ship_3b[1] = ship_3a[1] - 1
else:
ship_3b[1] = ship_3a[1] + 1

# the loops begins...
for x in range(3):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

# This is where I'm having trouble (the 2 last possibilities for the next if statement)
if (guess_row == ship_1[0] and guess_col == ship_1[1]) or \
(guess_row == ship_2[0] and guess_col == ship_2[1]) or \
(guess_row == ship_3a[0] and guess_col == ship_3a[1] and board[ship_3b[0]][ship_3b[1]] == '|') or \
(guess_row == ship_3b[0] and guess_col == ship_3b[1] and board[ship_3a[0]][ship_3a[1]] == '|') or \
print "A boat has sunk!"
board[guess_row][guess_col] = "|"
if (guess_row == ship_3a[0] and guess_col == ship_3a[1]) or \
(guess_row == ship_3b[0] and guess_col == ship_3b[1]):
print "A boat was touched!"
board[guess_row][guess_col] = "|"``````

#2

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