# How to add labels in battleship

#1

I’ve tried many things but I just can’t figure out how to add labels on the Battleship board.
I want the rows to be “A B C” and the columns to be 1 2 3.
Help appreciated.

``````from random import randint
from time import sleep

board = []

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

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

print_board(board)
def random_row(board):
return randint(0, len(board[0]) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

def random_Carrier(board):
return randint(4, len(board[0]) - 1)

def random_Battleship(board):
return randint(3, len(board[0]) - 1)
# Cruiser is 3 spaces and that is why it is "- 3"
def random_Cruiser(board):
return randint(0, len(board[0]) - 3)

def random_Submarine(board):
return randint(2, len(board[0]) - 1)
# Destroyer is 2 spaces and that is why it is "- 2"
def random_Destroyer(board):
return randint(0, len(board[0]) - 2)

# Give the variables a name
Carrier_row = random_Carrier(board)
Carrier_col = random_col(board)
Battleship_row = random_row(board)
Battleship_col = random_Battleship(board)
Cruiser_row = random_Cruiser(board)
Cruiser_col = random_row(board)
Submarine_row = random_row(board)
Submarine_col = random_Submarine(board)
Destroyer_row = random_col(board)
Destroyer_col = random_Destroyer(board)
# Carrier variables
CR1 = Carrier_row - 1
CR2 = Carrier_row - 2
CR3 = Carrier_row - 3
CR4 = Carrier_row - 4
# Battleship variables
B1 = Battleship_col - 1
B2 = Battleship_col - 2
B3 = Battleship_col - 3
# Cruiser Variables
C1 = Cruiser_row + 1
C2 = Cruiser_row + 2
# Submarine Variables
S1 = Submarine_col - 1
# Destroyer Variables
D1 = Destroyer_col + 1

for turn in range(20):
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Carrier_row and guess_col == Carrier_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == CR1 and guess_col == Carrier_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == CR2 and guess_col == Carrier_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == CR3 and guess_col == Carrier_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == CR4 and guess_col == Carrier_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Battleship_row and guess_col == Battleship_col:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Battleship_row and guess_col == B1:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Battleship_row and guess_col == B2:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Battleship_row and guess_col == B3:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Cruiser_row and guess_col == Cruiser_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == C1 and guess_col == Cruiser_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == C2 and guess_col == Cruiser_col:
sleep(1)
print "It's a hit!"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Submarine_row and guess_col == Submarine_col:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Submarine_row and guess_col == S1:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Destroyer_row and guess_col == Destroyer_col:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == Destroyer_row and guess_col == D1:
sleep(1)
print "It's a hit"
board[guess_row][guess_col] = "X"
print_board(board)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

else:

if guess_row not in range(10) or \
guess_col not in range(10):
print "Oops, that's not even in the ocean."
print_board(board)

elif board[guess_row][guess_col] == ".":
print "You guessed that one already."
print_board(board)

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

#2

Here is an example with cols in letters and rows in numbers, but simple to reverse.

How do I add letters and numbers for each row and col?

#3

Thank you very much I get it now

#4

You’re welcome.

Here is a local SERP of my own posts around the subject of multiple ships.

multiple ships @mtf

See what you can glean from the discussions and examples. If you have questions, include a link to that topic in your post. At this point I don’t remember what was posted where, only know that it relates to this subject. Seems there was some neat code devised, as well.

#5

Yeah, the problem with my code is that sometimes the ships overlap. I’ll try your solution. Once again, thank you very much.

#6

Ah, I do remember proposing one solution to that using a checksum. We can think of a number of ways to approach this, but the checksum was fast and dirty. A board is generated, if the checksum is wrong, it is rejected and a new board generated. No intermediate checks.

#7

So it generates a new board if the ships overlap?

#8

Correct. Only a valid board can be used going forward as I remember it. Will have to go through that serp to track down the topic. Might even be a repl that can be forked.

Found it…

Multiple Ships Troubleshooting

That version is two ships, only.

This is the deciding code…

``````a = randint(0, 24)
while True:
b = randint(0, 24)
if b != a: break
``````

That’s what makes sure b does not equal a on completion.

Still looking for the checksum version. Gotta be here somewhere.

Found the checksum version…

Track hits across different ships without using classes?

It never got any traction and with my time allowed was abandoned. If you wish to see this a little further, then wake up that topic. It is a permanently open topic, as near as I can tell.

#9

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