Oops, try again. Did you remember to print out turn + 1 each turn?


#1

This my code thus far. Also, my testing has showed that there is something wrong with my if, elif logic, for the case where I should have gotton the "you have aleeady chosen that senario".

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 "Let's play Battleship!"
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)

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    # Hit the ship
    if ship_row == guess_row and ship_col == guess_col:
        print ("Congratulations! You sank my battleship!")
        # Missed the ship
    elif guess_row in range(5) and guess_col in range(5):
        print ("You missed my battleship!")
        board[guess_col][guess_row] = "X"
        print_board(board)
        # Out of the ocean
    elif guess_row not in range(5) or guess_col not in range(5):
        print ("Whoops, that's not even in the ocean.")
    else:
        board[guess_col][guess_row] == "X"
        print ("You guessed that one already.")
    print ("Turn ", turn +1)

Thanks in advance


#2

Test the range before anything else (after the win).

This check for a hit is impossible to verify as a hit or a miss on the ship.

We are given coordinates to the computer's ship that we must match.

if guess_row != ship_row or guess_col != ship_col:
    print ("You missed my battleship!")

#3

Hi Roy,

Thanks for the reply. Sorry, I have read your comments several times, but can`t make heads or tails of it. Can you clarify:

This >>>
"""
if guess_row != ship_row or guess_col != ship_col:
print ("You missed my battleship!")
"""
Should replace this>>>
""""
elif guess_row in range(5) and guess_col in range(5):
print ("You missed my battleship!")
"""

Then next: "This check for a hit is impossible to verify as a hit or a miss on the ship.", which range are you referring to, there are two:
"""
elif guess_row in range(5) and guess_col in range(5):
print ("You missed my battleship!")
board[guess_col][guess_row] = "X"
print_board(board)
# Out of the ocean
elif guess_row not in range(5) or guess_col not in range(5):
print ("Whoops, that's not even in the ocean.")
"""

Thanks in advance,

H


#4

A miss in x or y is a miss. When both of the those operands yield False, it's a hit. But wait, we already tracked the hit. What is this?

Sorry to be messing with you. Why does missing the ship have to be a condition, at all? It should be the default.


#6

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

    # Hit the ship
    if ship_row == guess_row and ship_col == guess_col:
        print ("Congratulations! You sank my battleship!")
        
    # Missed the ship but still in the ocean range
    elif guess_row in range(5) and guess_col in range(5):
        print ("You missed my battleship!")
        board[guess_col][guess_row] = "X"
        print_board(board)
    
    # Out of the ocean
    elif guess_row not in range(5) or guess_col not in range(5):
        print ("Oops, that's not even in the ocean.")
        
    # Already guessed
    elif board[guess_col][guess_row] == "X":
        print ("You already guessed that.")

#7

Why? Why should it be default? Can you explicate or expatiate? - Do you mean that it is good coding practice or...?

As far as I can see all the cases are already covered above, right? It seems to me that:
# Out of the ocean and # Missed the ship but still in the ocean range, already covers all miss cases except for bad user input.

"Why does missing the ship have to be a condition, at all?" It has to be a condition, because it has to check where it missed... either in or out of the ocean.

Also, I still keep getting this error: "Oops, try again. Did you remember to print out turn + 1 each turn?"

Thanks in advance,


#8

Once we rule out a win, we need to check the range, then check if the element has already got an 'X'. After those checks the only thing remaining is not a hit. No conditional test to do.


#9

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

    # 1 Rule out a win
    if guess_row != ship_row or guess_col != ship_col:
        print ("You missed my battleship!")
 
    # 2 Missed the ship but still in the ocean range
    elif guess_row in range(5) and guess_col in range(5):
        print ("You missed my battleship!")
        board[guess_col][guess_row] = "X"
        print_board(board)
    
    # 3 Out of the ocean
    elif guess_row not in range(5) or guess_col not in range(5):
        print ("Oops, that's not even in the ocean.")
        
    # 4 Already guessed
    elif board[guess_col][guess_row] == "X":
        print ("You already guessed that.")
        
    # 5 Default condition    
    else:
        print ("You missed my battleship!")

#10

Still have this nagging pop-up


#11

#2, can be removed.

  1. check for win
  2. check range
  3. check is already marked
  4. default to miss.

#12

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