Extra credit - battleship sizes


I'm not sure how to go about the second point in the extra credit assignment. This is what I have so far (still haven't altered it to write out comments based on 2 sets of guess):

from random import randint

board = []

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

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"

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)

ship2_row = random_row(board)
ship2_col = random_col(board)

if ship_row != ship2_row and ship_col != ship2_col:

    print ship_row
    print ship_col
    print ship2_row
    print ship2_col

    for turn in range(4):
        print "Turn", turn + 1 
        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 sunk my battleship!"
            if (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."
                print "You missed my battleship!"
                board[guess_row][guess_col] = "X"
            if turn == 3:
                print "Game Over"
                print ship_row
                print ship_col
                print ship2_row
                print ship2_col
    # Print (turn + 1) here!


What are you asking?


How to go about making the ships different sizes.


Think about it this way having ships of different sizes is just specifying how much space a ship will take on your board. However you must account for the fact that it cannot be off the board so I would suggest that larger ships be random but in the confines of the outer row.


If you want the extra credit then do this on your own with what you have learned so far. If you are not yet equipped to deal with this, then set the project aside and complete the track. Then come back to it. Right now you are just spinning your wheels if you don't actually know how to move forward with this.


I suggest analysing how you would do this manually, that's likely something that can be described in code.

The design chosen by the exercise doesn't exactly invite any extensions to it, there's no "natural" way of adding it you just kind of have to bolt it on


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