Battleship 12


#1

not sure what to do
please help

from random import randint
print "battleships beta"
board = []

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

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

print_board(board)

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

def random_col(board):
    return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

# Write your code below!
if guess_col == ship_col and guess_row == ship_row:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

#2

so currently we have 1 if/else, if checks if the user sank the ship, else will inform the user they missed and mark the missed guess with a X. So, our else handles a miss

However, what if the user guess not only miss the ship, but its nowhere on the board? Updating the board will then crash our program.

So, in our else which handles misses, we want to add another if and else, the if should check if the shot is on the board, if not, inform the user about this, else can get the existing code we have (inform the user they missed and mark the missed guess with a X)


#3

i added this but it still was not functioning corectly

if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:


#4

can i see your full code?


#5

ok

from random import randint

board = []

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

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

print_board(board)

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

def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

Write your code below!

if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
if guess_col == ship_col and guess_row == ship_row:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)


#6

this if/else you are adding in this lesson should be in the else clauses which handles misses:

if guess_col == ship_col and guess_row == ship_row:
    print "Congratulations! You sank my battleship!"
else:
    # this else handles misses of ship, add your code here!
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

see the comment

Please think about the logic and game flow while building this game


#7

IndentationError: expected an indented block
thats what it says

from random import randint

board = []

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

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

print_board(board)

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

def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

Write your code below

if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
if guess_col == ship_col and guess_row == ship_row:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)


#8

look at this pseudo code:

# if user guesses ship
if correct guess:
    print "you won"
else:
   # user didn't guess ship
   # lets check if its in the ocean
   if in the ocean:
        print "Not in ocean"
   # its a valid miss
   else:
       print "you missed"
       # update and print board

#9

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