Another Question!


#1




I want to make it possible with (5, 5)! and here's supposed to locate the ship by four. (4,4)is maximum.
so I changed a bit like

def random_row(board):
return randint(0, len(board))
def random_col(board):
return randint(0, len(board))


but it doesnt work.. As I enter guess_row ==5, guess_col == 5,
then it says
Oops, that's not even in the ocean.
why they cannot show "X" on the board?


Replace this line with your 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))
def random_col(board):
    return randint(0, len(board))

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

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 sank my battleship!"
else:
    if guess_row not in range(len(board)) or guess_col not in range(len(board)):
        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)


#2

If you mean how can the user input range be 1..5, instead of 0..4, then use an offset when pointing to the list elements. Take the inputs and subtract 1 from each.

If you want a larger board, then simply make it bigger. The length of the 'sides' of the board are what set the limits on inputs.

This suggests the latter, which we can easily accomplish...

board_size = 5                # set the size of board you want
for x in range(board_size):
    board.append(["O"] * board_size)

#3

yeah!! I mean the former. But I cannot figure out how to code, when it comes to guesses..
I wrote like this.


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

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row +1
print ship_col +1

guess_row = int(raw_input("Guess Row:") + 1)
guess_col = int(raw_input("Guess Col:") +1)


Error says that
Traceback (most recent call last):
File "python", line 24, in
TypeError: coercing to Unicode: need string or buffer, int found


total stuck............ i cannot understand that "Take the inputs and subtract 1 from each."mean :cry:


#4

What we have here are a mix of concepts that may not yet be fully familiar. Are there concepts in here that you do not understand?

Break the problem into very small pieces by going through the lines asking yourself if there is a reason for that line. What does the line do, and what do you expect from it?

Take for instance these lines...

guess_row = int(raw_input("Guess Row:") + 1) 
guess_col = int(raw_input("Guess Col:") +1)

If we comment what we expect...

# get user inputs in range 1 to board size
guess_row = int(raw_input("Guess Row [1..%d]:" % board_size)) - 1
guess_col = int(raw_input("Guess Col [1..%d]:" % board_size)) - 1

The user can enter 1 thru 5, say, and your code interprets it as 0 to 4 so it can be used as an index.


#5

OHHHHHHHHH now I GOT the concept !!
Thank you so so much :heart_eyes: !!


#6

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