19! battleship


#1




some times this message appears!

ship2_row, ship2_col = random_row_ship2(board, ship1_row, ship1_col)#erro
TypeError: 'NoneType' object is not iterable


from random import randint

board = []

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

def print_board(board):
    print('  0 1 2 3 4')
    num = 0
    for row in board:
        print( num," ".join(row))
        num += 1
print("Let's play Battleship!")
print_board(board)


#ship 1
def random_row_ship1(board):
    random_r_ship1 = randint(0, len(board) - 1)
    print(random_r_ship1) #when done clear
    return random_r_ship1

def random_col_ship1(board):
    random_c_ship1 = randint(0, len(board[0]) - 1)
    print(random_c_ship1)#when done clear
    return random_c_ship1
#ship 2
def random_row_ship2(board, ship1_row, ship1_col):
    random_r_ship2 = randint(0, len(board) - 1)
    random_c_ship2 = randint(0, len(board) - 1)
    if ship1_row != random_r_ship2 and ship1_col != random_c_ship2 and random_r_ship2 != random_c_ship2:
        print(random_r_ship2)#when done clear
        print(random_c_ship2)#clear
        return random_r_ship2, random_c_ship2
    else: 
        random_row_ship2(board, ship1_row, ship1_col)
        
ship1_row = random_row_ship1(board)
ship1_col = random_col_ship1(board)
ship2_row, ship2_col = random_row_ship2(board, ship1_row, ship1_col)

for turn in range(4):
    print('Turn', turn + 1)
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))

    if guess_row == ship1_row and guess_col == ship1_col or guess_row == ship2_row and guess_col == ship2_col:
        print("Congratulations! You sunk my battleship!")
        break
    elif(guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print("Oops, that's not even in the ocean.")
    elif (board[guess_row][guess_col] == "X"):
        print("You guessed that one already.")
    else:
        print("You missed my battleship!")
        board[guess_row][guess_col] = "X"
        print_board(board)
        if turn == 3:
            print('Game Over')


#2

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