Improving the final Battleship! product


#1

I have a completed Battleship! game. I have code to deal with most circumstances, except when a user forgets to enter a row or column. For instances in which the user enters a blank column or row, I would like my program to alert the user and return her to the guess stage. Here is what my code looks like inside of the for loop.


for turn in range(4):
    guess_row = raw_input("Guess Row:")
    guess_col = raw_input("Guess Col:")
    
    if guess_row == "":
        print "You forgot to guess a row."
    if guess_col == "":
        print "You forgot to guess a column."
        break
    else:
        int(guess_row)
        int(guess_col)

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

As you can see, I removed the int() wrapper on both raw_input so that the first two if statements can properly check for a blank response. After that, I run into trouble. If the response is not blank, I need to convert the input to an integer so that the rest of my program will function. I'm a bit puzzled as to how to accomplish this. Any suggestions?


#2

i would the following, first i would reduce this:

if guess_row == "":
        print "You forgot to guess a row."
    if guess_col == "":
        print "You forgot to guess a column."

to something like:

if guess_row == "" or guess_col == "":
     print "You left one or more of the fields blank"

then i would introduce a for loop giving the user a new change of entering coordinates:

    while True:
         guess_row = raw_input("Guess Row:")
         guess_col = raw_input("Guess Col:")
         if guess_row == "" or guess_col == "":
               print "You left one or more of the fields blank"
         else:
               break

then outside the loop, simply cast the input to integers and update the variables:

guess_row=int(guess_row)

#3

As stetim94 hinted above, the key is to add one or two while loops to your code. I would suggest having a separate loop for guess_row and guess_col, that way the user doesn't have to re-enter both for forgetting one.


#4

Separate loops is possible, it does mean more code, more loops. How does an additional loop affect preformance? In such a small program, the change will be barely noticable, but all this small kind of changes would matter if your project is 10,000+ lines. But this is always a choice and one should be aware of what advantages and disatvantage the difference approaches have.


#5

If I was a user of this game, I wouldn't want to re-enter my 'row' value if I forgot to enter the 'col' value. This is just a learning exercise, and the difference isn't that significant. Just a thought.
It could also be done with 1 loop and more conditional statements.


#6

Yes, its just an exercise, but with everything you do, you should wonder what you do and why. This is what programming is about

I am not saying your suggestion it bad, the UX outways anything else here i think, so your suggestion might be actuallly really good, but its important to get in this mindset. Critical thinking is important


#7

Alright, I'm a bit confused. I don't believe we covered while loops yet, but I'm confident I understand the logic behind them. Stetim94, you mentioned an additional for loop, would that be inserted after:

for turn in range(4):
    guess_row = raw_input("Guess Row:")
    guess_col = raw_input("Guess Col:")

Correct? I'm also confused on how to structure it. Here's what I came up with:

for guess in guess_row:
        if guess_row == "":
            print "You forgot to guess a row."
            guess_row = raw_input("Guess Row:")
for guess in guess_col:
        if guess_col == "":
            print "You forgot to guess a column."
            guess_col = raw_input("Guess Col:")
    guess_row = int(guess_row)
    guess_col = int(guess_col)

I know I'm missing the while loop, but am I on the right track?


#8

there is a reason i use a while loop here, its because you can't use a for loop

While loop is the next thing you will learn, please continue the course and come back here in a couple of exercises


#9

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