# Battleship creative

#1

Hi everyone

I wanted to try out some stuff.

However, both of my ships' coordinates seems to be fixed at 1,1 and 2,3 every time. Also somehow I only have one ship

The coordinates should differ from time to time and there should always be two ships.

``````Replace this line with your code.
from random import randint

board = []

for x in range(0,3):
board.append(["O"] * 3)

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

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

def random_row1(board):
return randint(0, len(board) - 1)
def random_row2(board):
return randint(0, len(board) - 1)
def random_col1(board):
return randint(0, len(board) - 1)
def random_col2(board):
return randint(0, len(board) - 1)

def ship(ship1_row,ship2_row,ship1_col,ship2_col):
for you in range(0,10):
ship1_row = random_row1(board) + 1
ship1_col = random_col1(board) + 1
ship2_row = random_row2(board) + 1
ship2_col = random_col2(board) + 1
if (ship1_row == ship2_row) and ship1_col == ship2_col:
continue
else:
return ship1_row
return ship2_row
return ship1_row
return ship2_row
break

ship(ship1_row,ship2_row,ship1_col,ship2_col)

for turn in range(0,4):
hit = 0
print "Turn %s" % (turn + 1)
guess_row = int(raw_input("Guess Row:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1
if (guess_row == ship1_row and guess_col == ship1_col) or (guess_row == ship2_row and guess_col == ship2_col):
print "lucky af, once only"
board[guess_row][guess_col] = "M"
print_board(board)
hit == hit + 1
if hit == 2:
print "fine, you win"
break
else:
if (guess_row < 0 or guess_row > len(board)-1) or (guess_col < 0 or guess_col > len(board)-1):
print "Oops, that's not even in the ocean."
if turn == 3:
print "Game Over"
elif (board[guess_row][guess_col] == "X") or (board[guess_row][guess_col] == "M"):
print "You guessed that one already."
if turn == 3:
print "Game Over"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
if turn == 3:
print "Game Over"

print "first ship (%s,%s)" % (ship1_col,ship1_row)
print "second ship (%s,%s)" % (ship2_col,ship2_row)``````

#2

i tried to run your code here, it doesn't run

on line 39 you have undefined variables :

``ship(ship1_row,ship2_row,ship1_col,ship2_col)``

you have undefined variables. There are no comments in your code, its difficult to tell what some part of your code are suppose to do

#4

Oh I see. the ship function was useless. I just removed the function line and it worked perfectly. Thank you so much

#5

You're welcome, do you manage from here?

If you need more help, post an updated version of your code + queston

#6

Yup I am trying to do the other suggestions.

#7

Hi I am sorry about this but the code looks super messy haha. I tried to create two ships each with different length. The usual error lies in the part where I try to print the final table to show where the ships actually are(turning them all into M). The indexerror is: list assignment index out of range. Also sometimes the ships clash. I thought I covered all cases already. If you can help me take a look that would be great.
Oh and on a side note, do you know where I can download a python version that is suitable for my laptop? I am currently using Windows 10 64-bit laptop. I tried downloading from the python page but then the version comes back as 32-bit. I am not sure if that's the problem but the software I downloaded couldn't run on my laptop for some reason.
Thank you so much for your help

``````from random import randint

board = []

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

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

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

def random_row1(board):
return randint(0, len(board) - 1)
def random_row2(board):
return randint(0, len(board) - 1)
def random_col1(board):
return randint(0, len(board) - 1)
def random_col2(board):
return randint(0, len(board) - 1)

for you in range(0,100):
ship1_1_row = random_row1(board)
ship1_1_col = random_col1(board)
ship2_1_row = random_row2(board)
ship2_1_col = random_col2(board)
if (ship1_1_row == ship2_1_row) and (ship1_1_col == ship2_1_col):
continue
else:
random_number = randint(0,4)
if random_number == 0:
ship1_2_row = ship1_1_row + 1
ship1_2_col = ship1_1_col
ship2_2_row = ship2_1_row + 1
ship2_2_col = ship2_1_col
ship2_3_row = ship2_1_row - 1
ship2_3_col = ship2_1_col
if random_number == 1:
ship1_2_row = ship1_1_row + 1
ship1_2_col = ship1_1_col
ship2_2_row = ship2_1_row
ship2_2_col = ship2_1_col + 1
ship2_3_row = ship2_1_row
ship2_3_col = ship2_1_col - 1
if random_number == 2:
ship1_2_row = ship1_1_row
ship1_2_col = ship1_1_col + 1
ship2_2_row = ship2_1_row + 1
ship2_2_col = ship2_1_col
ship2_3_row = ship2_1_row - 1
ship2_3_col = ship2_1_col
if random_number == 3:
ship1_2_row = ship1_1_row
ship1_2_col = ship1_1_col + 1
ship2_2_row = ship2_1_row
ship2_2_col = ship2_1_col + 1
ship2_3_row = ship2_1_row
ship2_3_col = ship2_1_col - 1

#Below is where I am trying ensure the ships dont clash nor do they fall out of the board. Can u tell me if I am missing any cases
if (ship1_1_row == ship2_1_row and ship1_1_col == ship2_1_col) or (ship1_1_row == ship2_2_row and ship1_1_col == ship2_2_col) or (ship1_1_row == ship2_3_row and ship1_1_col == ship2_3_col) or (ship1_2_row == ship2_1_row and ship1_2_col == ship2_1_col) or (ship1_2_row > len(board) - 1) or (ship1_2_col > len(board) - 1) or (ship2_2_row > len(board) - 1) or (ship2_2_row > len(board) - 1) or (ship2_3_row <= 0) or (ship2_3_row <= 0):
continue
else:
break

for turn in range(0,10):
hit = 0
print "Turn %s" % (turn + 1)
guess_row = int(raw_input("Guess Row:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1
if ( guess_row == ship1_1_row or guess_row == ship1_2_row ) and ( guess_col == ship1_1_col or guess_col == ship1_2_col) or ( guess_row == ship2_1_row or guess_row == ship2_2_row or guess_row == ship2_3_row ) and (guess_col == ship2_1_col or guess_col == ship2_2_col or guess_col == ship2_3_col):
print "lucky af, once only"
board[guess_row][guess_col] = "M"
print_board(board)
hit = hit + 1
if hit == 5:
print "fine, you win"
board[ship1_1_row][ship1_1_col] = "M"
board[ship1_2_row][ship1_2_col] = "M"
board[ship2_1_row][ship2_1_col] = "M"
board[ship2_2_row][ship2_2_col] = "M"
board[ship2_3_row][ship2_3_col] = "M"
print "Here are the ships"
print_board(board)
break
elif turn == 9:

#Here is where the problem of list assignment index out of range usually occur
board[ship1_1_row][ship1_1_col] = "M"
board[ship1_2_row][ship1_2_col] = "M"
board[ship2_1_row][ship2_1_col] = "M"
board[ship2_2_row][ship2_2_col] = "M"
board[ship2_3_row][ship2_3_col] = "M"
print "Game Over"
print "Here are the ships"
print_board(board)

else:
if (guess_row < 0 or guess_row > len(board)-1) or (guess_col < 0 or guess_col > len(board)-1):
print "Oops, that's not even in the ocean."
if turn == 9:
board[ship1_1_row][ship1_1_col] = "M"
board[ship1_2_row][ship1_2_col] = "M"
board[ship2_1_row][ship2_1_col] = "M"
board[ship2_2_row][ship2_2_col] = "M"
board[ship2_3_row][ship2_3_col] = "M"
print "Game Over"
print "Here are the ships"
print_board(board)
elif (board[guess_row][guess_col] == "X") or (board[guess_row][guess_col] == "M"):
print "You guessed that one already."
if turn == 9:
board[ship1_1_row][ship1_1_col] = "M"
board[ship1_2_row][ship1_2_col] = "M"
board[ship2_1_row][ship2_1_col] = "M"
board[ship2_2_row][ship2_2_col] = "M"
board[ship2_3_row][ship2_3_col] = "M"
print "Game Over"
print "Here are the ships"
print_board(board)
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
if turn == 9:
board[ship1_1_row][ship1_1_col] = "M"
board[ship1_2_row][ship1_2_col] = "M"
board[ship2_1_row][ship2_1_col] = "M"
board[ship2_2_row][ship2_2_col] = "M"
board[ship2_3_row][ship2_3_col] = "M"
print "Game Over"
print "Here are the ships"
print_board(board)

print "first ship (%s,%s),(%s,%s)" % (ship1_1_col+1,ship1_1_row+1,ship1_2_col+1,ship1_2_row+1)
print "second ship (%s,%s),(%s,%s),(%s,%s)" % (ship2_3_col+1,ship2_3_row+1,ship2_1_col+1,ship2_1_row+1,ship2_2_col+1,ship2_2_row+1)``````

#8

pff... this code is so lengthy, at earliest i have time for this tomorrow

How come the code is so lengthy?

#9

lol I tried to make it super random: the positions of the boats and whether the boats are vertical or horizontal. It's decided by the random_number variable. And then there is the whole chunk of conditions for the ships not to clash and not to fall out of the board. Since one ship's length is two and the other one's is three, the conditions get so long. Sorry haha =)). If you help me reduce the length that would be awesome =D

#10

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