I remade Codecademy’s Battleship to be bigger, better, and less buggy than ever. It also has debug.
NOTE : This is in Python 3.

#BATTLESHIP CODE
board =

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

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

print("Unlike regular Battleship, you only have to hit 1 tile to win.")
print("However, you have only 4 chances!")
print_board(board)

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)
for turn in range(4):
guess_row = 1
guess_col = 2
guessconfirm = False
wingame = False
while guessconfirm == False:
guess_row_input = input("Guess Row:")
guess_col_input = input("Guess Col:")
if guess_row_input == "debug" and guess_col_input == "debug":
print (str(ship_row) + "," + str(ship_col))
elif guess_row_input.isdigit() and guess_col_input.isdigit():
guess_row = int(guess_row_input)
guess_col = int(guess_col_input)
guessconfirm = True
if int(guess_row) == ship_row and int(guess_col) == ship_col:
print("You sunk it. Hooray.")
print("It was at " + str(ship_row) + "," + str(ship_col))
print("Reopen the file to play another game.")
input('Continue...')
wingame = True
break
elif guess_row > 5 or guess_row < 1 or guess_col > 5 or guess_col < 1:
print("You missed... the map.")
else :
print("You missed!")
board[guess_row][guess_col] = 'X'
print("Turn " + str(turn))
print_board(board)
print ("Game Over. Reopen the file to play again.")
input ('')``````
4 Likes