13. Battleship: Extra functionality for the game


#1

Hey guys,

I am going nuts with this. I am doing the 13. Battleship-Game. I already finished all the given tasks here and want to add now some extra functionality.

My game allows you to try 4 times to sink the battleship which is set by the for loop with range(4). I want that in case you either put in a value that is a) out of range or b) has been given already the "try" doesnt count. I want that only the tries which either result in a miss or victory will count. I want to have a maximum of 4 misses but endless of the above listed a) and b) results. They just shouldn't count.

I tried already to "deduct a turn" with "turn -1" or "turn = turn -1" but it doesnt help. Another idea was to use the "continue" statement, which as far as i understand skips the current part and continues afterwards with the iteration. But nothing worked. I can still give 4 times values outside of the board and the game ends after the 4th try.

I would really appreciate any input. Please see my code below. I left the parts of code I tried already out but added a comment instead.

Thank You.

#random number for battleship placement
from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

#remove " " between "O"'s in board
def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

#define position of battleship
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)

#four times user input
for turn in range(4):
    guess_row = int(raw_input("Guess Row:")) -1
    guess_col = int(raw_input("Guess Col:")) -1
    
    #different messages depending on the output
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        #get out of the loop earlier if battleship sunk but guesses are left
        break
    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."
            #i tried here already "turn -1", "turn = turn-1", "continue" to discount this from my 4 tries
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            #i tried here as well "turn -1", "turn = turn-1", "continue" to discount this from my 4 tries
        elif turn == 3:
            print "Game Over"
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"

            
        #print each turn
        print "Turn", turn +1
        print_board(board)

#2

Hey there! What you could do is use a while-loop and increment (and decrement accordingly) manually.


#3

@dutchc0de

Thank you. That was the advise I needed. I managed to fix it. See my code below:

Cheers!

#random number for battleship placement
from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

#remove " " between "O"'s in board
def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print "You have 4 tries to sink the battleship!"
print_board(board)

#define position of battleship
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)

#user input
count = 4
while (count >= 0):
    guess_row = int(raw_input("Guess Row:")) -1
    guess_col = int(raw_input("Guess Col:")) -1
    
    #different messages depending on the output
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        #get out of the loop earlier if battleship sunk but guesses are left
        break
    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."
        elif count == 1:
            board[guess_row][guess_col] = "X"
            print_board(board)
            print "You have 0 tries left. You didn't sink the battleship. Game Over!"
            break
        else:
            count -= 1
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"

        #print each turn
       	print "Turns left: %d" %count
        print_board(board)

#4

I always liked this particular project. It allows the learner to come up with some novel solutions. Check out this post, it has some other peoples projects on this including my own.

Best of luck in your endeavors.


#5

Thank you! I looked at your battleship-project. Its awesome. Great job. I also like this particular project as you can be really creative in your ideas and solutions.

Best of luck too you as well!