<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
The user should be able to save the game (using files) and come back to it at any time.
<In what way does your code behave incorrectly? Include ALL error messages.>
I’m not sure how to take my battleship game and include a save option after each move so that if the game is left it can be called again
<What do you expect to happen instead?>
It would be lovely if someone could write into my code coding on how to do that and name the file whatever and I can save that onto my desktop and call it and store the saved info into it. I’m terrible at just understanding words that’s why I need code. Please help!!!
Replace this line with your code.
#def save();
#what it does , write entire board
#def load():
#call load function at beginning
#board[r][c] == “x”
from random import randint
Define blank list
board =
board1 =
#Generate rows with length of 3
for row in range(10):
Appen a blank list to each row cell
board.append()
board1.append()
for column in range(10):
# Assign x to each row
#board[row].append(str(row) + “,” + str(column))
board[row].append("~")
board1[row].append("~")
Function will print board like an actual board
def print_board(board):
print(" A B C D E F G H I J")
for row in range(len(board)):
print(str(row+1)+ " " + " ".join(board[row]))
#print_board(board)
#board[4][4] = ‘x’ # row,column board[row][column]
alpha = [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”]
print_board(board)
for i in range (0,5):
c = input(‘where do you want your column? pick (A-J)’)
r = int(input(‘where do you want your row? pick (1-10)’))
f = alpha.index©
print(f)
board[r-1][f] = “X”
print_board(board)
print_board(board1)
for i in range(0,5):
c = randint(0,9)
r = randint(0,9)
board1[r][c] = “X”
print_board(board1)
b = any(“X” in sublist for sublist in board)
b1 = any(“X” in sublist for sublist in board1)
while( b == True and b1 == True):
print(“user turn”)
d = input(“what column do you want to hit?”)
e = int(input(“what row do you want to hit?”))
i = alpha.index(d)
if board1[e-1][i] == "X":
board1[e-1][i] = "O"
print("hit")
else:
print("miss")
print("computer turn")
d = randint(0,9)
e = randint(0,9)
if board[e][d] == "X":
board[e][d] = "O"
print("hit")
else:
print("miss")
b = any("X" in sublist for sublist in board)
b1 = any("X" in sublist for sublist in board1)
if b1 == True:
print(“you lost”)
if b == True:
print(“you won”)
<do not remove the three backticks above>