Including save option in my battleship game

<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!!!

```python

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>

I know how to do it, but I’m not going to write into your code. My reason is that I don’t believe that’s how people learn - they learn by writing things out themselves and researching how to do things.

I will tell you this: file I/O will be very helpful to you here. If you’re going for a basic system, look into the open() object and use it with a .txt file like data.txt. It’s used for opening, closinig, reading and writing to files.

If you want something a little more advanced after that, research something called CSV. It uses the open() object, but the file would be data.csv and it’s a little more complex (but SO much better for data storage than a plain text file). It’s good for storing specific values.

However, in this case, it would probably be easier to use a mixture of the two. I would store the board in a plain text file and any specific values (such as amount of attempts, who’s turn it is (in a two player game)).

Hope this helps!

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