Okay, so I think I almost have everything I need to make a full battleship game. If I can just figure out how to check for the win condition, how to make sure the random generated numbers for the different coordinates don’t overlap, and how to line up multiple points for a full ship then I’ll be set. Here’s what I’ve got so far:
from random import randint
board = []
for x in range(5):
board.append(["_"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print "Let's play Battleship!"
print_board(board)
def random_row1(board):
return randint(0, len(board) - 1)
def random_col1(board):
return randint(0, len(board[0]) - 1)
def random_row2(board):
return randint(0, len(board[0]) - 1)
def random_col2(baord):
return randint(0, len(board[0]) - 1)
battleship_row1 = random_row1(board)
battleship_col1 = random_col1(board)
battleship_row2 = random_row2(board)
battleship_col2 = random_col2(board)
battleship_aft = (battleship_row1, battleship_col1)
battleship_fore = (battleship_row2, battleship_col2)
print battleship_row1
print battleship_col1
print battleship_row2
print battleship_col2
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
class battleship(object):
for turn in range(6):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if (guess_row == battleship_row1) and (guess_col == battleship_col1):
print "That's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
elif (guess_row == battleship_row2) and (guess_col == battleship_col2):
print "That's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
else:
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") or (board[guess_row][guess_col] == "O"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "O"
print_board(board)
if turn == 5:
print "Game Over"
break
Any advice, hints, or ideas of where I can look to figure it out would be appreciated. Thanks.