Battleship - Start from 1 instead of 0?


#1



18. To Your Battlestations.


I'm curious if there is a way to cause the board to start from 1 instead of 0. For instance when it asks for user input, a user may not start with the number 0 to designate the first column or row.


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)

""" This prints outs a debugging section
to test how the program works if a ship is
correctly discovered in the game 
print ship_row
print ship_col 
"""

for turn in range(4):
    print "\nTurn", turn + 1 
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (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"
        if turn == 3:
            print "Game Over"
    print_board(board)


#2

First off, the user is you. There is no other user. This is an exercise.

If you must have the user enter values from 1..5 then tell the user what to input, and subtract 1 so the code doesn't need to modified to accommodate this.


#3

I've spent a couple hours trying to figure out why this wouldn't accept "5" as a valid row or column. In previous exercises, code seemed to be written with the pretense that it should be suitable for an imaginary end-user, whereas this exercise appears to ditch that pretense without clueing me in to the change.

So, I tried modifying the code per your suggestion by subtracting one from the user's input, but it still tells me that the guess is not inside the ocean (except in one instance where I sunk the battleship). Can you help me determine what is wrong with my code?

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:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    if guess_row not in range(len(board) - 1) \
    or guess_col not in range(len(board) - 1):
        print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col] == "X":
        print "You guessed that one already."
    else:
      print "Sorry, you missed!"
      board[guess_row][guess_col] = "X"
      print_board(board)

#4

For this line, do not subtract 1. This part of the code should stay the same for either form of input since the value is adjusted to this range.

... not in range(5)

or

... not in range(len(board))

#5

Thanks, that worked!


#6

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