Bugs that weren't adressed in the BATTLESHIP tutorial


#1



OK guys, I just finished the "Battleship" tutorial and there were some questions left unanswered. 
For starters I am wondering how can the problem with the coordinates be fixed. 
Personally I think that it would make more sense if the top left corners' coordinates are 1,1 insted of 0,0...so any ideas about fixing that would be very much appreciated!
Also...the application displays errors when you enter a letter insted of a number. 
I tried fixig this as was suggested in another post with adding the .isdigit() function in a while loop, but then I seem to always get the "not in the ocean" error when i type actual digits.
Also, please excuse my english, since I'm not a native speaker and if something isn't clear I'll try to explain it better...


#2

@docata

Everything you should need to look at is here!

Any questions, feel free to ask.


#3

Thank you soo much! I don't know how I didn't think about looking up an "extra credit" thread.... :confused:


#4

I still don't understand how to make it happen so the guesses start from 1 to 5. I was thinking that it would be possible if the board starts with the " " at the beginning of each column and each row, so the index 0 is a " " and index 1 is the first "O". I tried a few combinations, but i could only do the first row as a blank row, and i don't know how to make the first column blank. Any ideas?


#5

I kind of figured it out, so it looks like what i wanted when it prints out the board, and i can guess from 1 to 5, and i changed the randint to choose from 1 to 5, however, when it comes to marking X on the board, let's say i guessed ROW 5 and COL 3, it marks the X on row indexed 5, as expected and column indexed 4. ? If it makes any sense?

from random import randint

board = [" "]

for x in range(1,6):
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(1, 5)

def random_col(board):
return randint(1, 5)

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:
if guess_row not in range(1,6) or guess_col not in range(1,6):
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)


#6

what i did was very simple.

just add a -1 to the guess'. that did it for me. If anyone have debugged it furthermore please let me know if that modification did something it shouldn't.

hope it helps

    from random import randint #importa randint de random

    board = [] #cria um lista vazia

    for x in range(5): #seta que para cada "x" em uma lista 0,5 e coloca 0, em 5 sublistas em todos os index
        board.append(["O"] * 5)

    def print_board(board):#função que printa a lista board setando como row cada sublista e unindo os elemente das sublistas com espaço vazio
        for row in board:
           print " ".join(row)

    print "Let's play Battleship!"# printa a fras
    print_board(board)#executa a def

    def random_row(board):#função que seleciona um número aleatóriamente entre 0 e o tamanho de 
       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)
print ship_row
print ship_col

for turn in range(4):

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

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
else:
    if (turn + 1) == 4:
        print "Game Over"
    elif (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"
        print_board(board)
    print "Turn", turn + 1