# Battleship with Multi - Ship (different sizes)

#1

Hi guys. Thanks for the help at various points through other exercises. This is my battleship multi-ship code. This code has three different ships of different sizes and makes it impossible for two ships to be overlapping. It tells you what you have hit etc. although there is no multiplayer component yet. Anyway use this as you want (probably not helpful but might help 1/2 of you.

from random import randint

board = []

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

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

# Instructions

print "Let's play Battleship!"
print "There are three ships;"
print "- The Patrol boat, (1 unit in length, 1 in width)"
print "- The Destroyer, (3 units in length, 1 in width)"
print "- The Aircraft Carrier (4 units in length, 1 in width)"
print " Enter numbers when prompted and try to destroy the ships in as few moves as possible."
print_board(board)

# Patrol Boat

def Prandom_row(board):
return randint(0, len(board))

def Prandom_col(board):
return randint(0, len(board))

Patrol_row = Prandom_row(board)
Patrol_col = Prandom_col(board)

# Destroyer

def Drandom_col_start(board):
return randint >= Patrol_col + 3 or randint <= Patrol_col - 3

def Drandom_row_start(board):
return randint >= Patrol_col + 3 or randint <= Patrol_row - 3

Drandom_row1 = Drandom_row_start(board)
Drandom_col1 = Drandom_col_start(board)

if (Drandom_row1 % 2 == 0) and (Drandom_col1 % 2 == 0):
Dcol2 = Drandom_col1 + 1
Drow2 = Drandom_row1
Dcol3 = Drandom_col1 + 2
Drow3 = Drandom_row1

elif (Drandom_row1 % 2 == 0) and (Drandom_col1 % 2 != 0):
Dcol2 = Drandom_col1 - 1
Drow2 = Drandom_row1
Dcol3 = Drandom_col1 - 2
Drow3 = Drandom_row1

elif (Drandom_row1 % 2 != 0) and (Drandom_col1 % 2 == 0):
Drow2 = Drandom_row1 + 1
Dcol2 = Drandom_col1
Drow3 = Drandom_row1 + 2
Dcol3 = Drandom_col1

elif (Drandom_row1 % 2 != 0) and (Drandom_col1 % 2 != 0):
Drow2 = Drandom_row1 - 1
Dcol2 = Drandom_row1
Drow3 = Drandom_row1 - 2
Dcol3 = Drandom_row1

# Carrier

def Crandom_col_start(board):
return (randint >= Patrol_col + 4 or randint <= Patrol_col - 4) and ((randint >= Drandom_col1 + 4 and randint >= Dcol2 + 4 and randint >= Dcol3 + 4) or (randint >= Drandom_col1 - 4 and randint >= Dcol2 - 4 and randint >= Dcol3 - 4))

def Crandom_row_start(board):
return (randint >= Patrol_row + 4 or randint <= Patrol_row - 4) and ((randint >= Drandom_row1 + 4 and randint >= Drow2 + 4 and randint >= Drow3 + 4) or (randint >= Drandom_row1 - 4 and randint >= Drow2 - 4 and randint >= Drow3 - 4))

Crandom_row1 = Crandom_row_start(board)
Crandom_col1 = Crandom_col_start(board)

if (Crandom_row1 % 2 == 0) and (Crandom_col1 % 2 == 0):
Ccol2 = Crandom_col1 + 1
Crow2 = Crandom_row1
Ccol3 = Crandom_col1 + 2
Crow3 = Crandom_row1
Ccol4 = Crandom_col1 + 3
Crow4 = Crandom_row1

elif (Drandom_row1 % 2 == 0) and (Drandom_col1 % 2 != 0):
Ccol2 = Crandom_col1 - 1
Crow2 = Crandom_row1
Ccol3 = Crandom_col1 - 2
Crow3 = Crandom_row1
Ccol4 = Crandom_col1 - 3
Crow4 = Crandom_row1

elif (Drandom_row1 % 2 != 0) and (Drandom_col1 % 2 == 0):
Crow2 = Crandom_row1 + 1
Ccol2 = Crandom_col1
Crow3 = Crandom_row1 + 2
Ccol3 = Crandom_col1
Crow4 = Crandom_row1 + 3
Ccol4 = Crandom_col1

elif (Drandom_row1 % 2 != 0) and (Drandom_col1 % 2 != 0):
Crow2 = Crandom_row1 - 1
Ccol2 = Crandom_col1
Crow3 = Crandom_row1 - 2
Ccol3 = Crandom_col1
Crow4 = Crandom_row1 - 3
Ccol4 = Crandom_col1

# Guesses

turn = 0
for turn in range(20):
print "Turn", turn+1

``````guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

Patrol_Boat_Destruction = 0
Destroyer_Destruction = 0
Aircraft_Carrier_Destruction = 0

if guess_row == Patrol_row and guess_col == Patrol_col:
Patrol_Boat_Destruction = Patrol_Boat_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You hit my patrol boat!"

elif guess_row == Drandom_row1 and guess_col == Drandom_col1:
Destroyer_Destruction = Destroyer_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Destroyer!"

elif guess_row == Drow2 and guess_col == Dcol2:
Destroyer_Destruction = Destroyer_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Destroyer!"

elif guess_row == Drow3 and guess_col == Dcol3:
Destroyer_Destruction = Destroyer_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Destroyer!"

elif guess_row == Crandom_row1 and guess_col == Crandom_col1:
Aircraft_Carrier_Destruction = Aircraft_Carrier_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Aircraft Carrier!"

elif guess_row == Crow2 and guess_col == Ccol2:
Aircraft_Carrier_Destruction = Aircraft_Carrier_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Aircraft Carrier!"

elif guess_row == Crow3 and guess_col == Ccol3:
Aircraft_Carrier_Destruction = Aircraft_Carrier_Destruction + 1
board[guess_row][guess_col] = "1"
print_board(board)
print "You have hit my Aircraft Carrier!"

elif guess_row == Crow4 and guess_col == Ccol4:
Aircraft_Carrier_Destruction = Aircraft_Carrier_Destruction + 1
board[guess_row][guess_col] = "1"``````
``````    print_board(board)
print "You have hit my Aircraft Carrier!"

else:

if ((guess_row < 0) or (guess_row > 10)) or ((guess_col < 0) or (guess_col > 10)):
print "Oops, that's not even in the ocean."
print_board(board)``````
``````    elif (board[guess_row][guess_col] == "X") or (board[guess_row][guess_col] == "1"):
print "You guessed that one already."
print_board(board)

else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)

if turn == 20:
print "Game Over"
break

if Aircraft_Carrier_Destruction == 4:
print "Congratulations! You destroyed my Aircraft Carrier!"

if Destroyer_Destruction == 3:
print "Congratulations! You destroyed my Destroyer!"

if Patrol_Boat_Destruction == 1:
print "Congratulation! You destroyed my Patrol Boat!"

elif Aircraft_Carrier_Destruction == 4 and Destroyer_Destruction == 3 and Patrol_Boat_Destruction == 1:
print "You win"
break``````

turn += 1
print turn
print_board(board)

#3

@l_molm

Thanks for sharing the code but you are posting it in the wrong category This is for `JQuery` a framework in Javascript not for `Python`!

#5

Sorry! Think I changed it to python - thanks for the heads up though.

#6

@l_molm

You can switch the course in the topic by editing the topic. Then you can click on the course in your editing bar and select the appropriate course.