Danger, Will Robinson!


#1



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 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:
    print "You missed my battleship!"
    board[guess_col][guess_row] = "X"
    print_board(board)

Need help. When my input is equal numbers, for example, 4 and 4, the code works. But, for example, if my input is 2 and 4, i get this message: Oops, try again. It looks like your game does not change the O to a capital X for an incorrect guess.


#2

Looking for something?


#3

It appears that you simply mixed the row and column. :slight_smile:


#4

actually that'd be (y, x) because the first value/index is which sublist, and each sublist is a row

the x-axis on a coordinate system is the one that goes towards the right

the nth row is at height n (each row adds height), and height is the y-axis, so (row, col) is (y, x)


#5

@ionatan
You're right. Should've thought about it myself more before posting.


#6

Well it is totally confusing, (I avoid using y/x as names it always turns bad)


#7

Just a guess, but you might want to change your last line to

print print_board(board)

the way you have it now only ends the loop, but adding another print allows the entire grid to be reprinted.


#8

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