I've been trying to learn python and get back into coding after many years, so I've attempted a very basic battleship game in the terminal. I've had many roadblocks that I've worked through, but I've become stumped on my generate_(n)ship() functions. I attempted to write the code so that I begun the function with a savestate that took the current state of the board, so that later if the function decided to try to place a ship on another space that already had one, it could give up, reset to the savestate, and try again until it picked and placed upon (n) blank spaces. import random board = [] pboard = [] row = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] col = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] def print_board(board, prnt): for x in range(10): board.append(["O"] * 10) if prnt == True: print(" ".join(board[x])) def print_pboard(pboard): for x in range(10): pboard.append(["O"] * 10) print(" ".join(pboard[x])) print_board(board, False) print_pboard(pboard) print("Welcome to Battleship!") def get_coords(): coord = str(input("Enter coordinates (e.g., A1): ").upper()) if len(coord) == 3: if coord[2] == "0": return row.index(coord[0]), 9 if len(coord) != 2: print("Invalid input. Please enter a letter followed by a number (e.g., A1).") return get_coords() if coord[0] not in row or not coord[1].isdigit() or int(coord[1]) < 1 or int(coord[1]) > 10: print("Invalid coordinates. Please try again.") return get_coords() return row.index(coord[0]), int(coord[1]) - 1 def generate_5ship(board): orientation = random.choice(["horizontal", "vertical"]) savestate = board.copy() if orientation == "horizontal": x = [random.randint(4, 9)] y = [random.randint(0, 9)] for i in range(1, 5): x.append(x[0] - i) else: x = [random.randint(0, 9)] y = [random.randint(4, 9)] for i in range(1, 5): y.append(y[0] - i) pos = x, y if len(pos[0]) > len(pos[1]): for i in range(5): inty = sum(pos[1]) if board[x][inty] == "O": board[x][inty] = "5" else: board = savestate.copy() return generate_5ship(board) else: for i in range(5): intx = sum(pos[0]) if board[intx][y] == "O": board[intx][y] = "5" else: board = savestate.copy() return generate_5ship(board) def generate_4ship(board): orientation = random.choice(["horizontal", "vertical"]) savestate = board.copy() print("board") print_board(board, True) print("savestate") print_board(savestate, True) if orientation == "horizontal": x = [random.randint(3, 9)] y = [random.randint(0, 9)] for i in range(1, 4): x.append(x[0] - i) else: x = [random.randint(0, 9)] y = [random.randint(4, 9)] for i in range(1, 4): y.append(y[0] - i) pos = x, y if len(pos[0]) > len(pos[1]): for i in range(4): inty = sum(pos[1]) if board[x][inty] == "O": board[x][inty] = "4" else: print("board before reverting") print_board(board, True) board = savestate.copy() print("board after reverting") print_board(board, True) return generate_4ship(board) else: for i in range(4): intx = sum(pos[0]) if board[intx][y] == "O": board[intx][y] = "4" else: board = savestate.copy() return generate_4ship(board) def generate_3ship(board): orientation = random.choice(["horizontal", "vertical"]) savestate = board.copy() if orientation == "horizontal": x = [random.randint(2, 9)] y = [random.randint(0, 9)] for i in range(1, 3): x.append(x[0] - i) else: x = [random.randint(0, 9)] y = [random.randint(2, 9)] for i in range(1, 3): y.append(y[0] - i) pos = x, y if len(pos[0]) > len(pos[1]): for i in range(3): inty = sum(pos[1]) if board[x][inty] == "O": board[x][inty] = "3" else: board = savestate.copy() return generate_3ship(board) else: for i in range(3): intx = sum(pos[0]) if board[intx][y] == "O": board[intx][y] = "3" else: board = savestate.copy() return generate_3ship(board) def generate_2ship(board): orientation = random.choice(["horizontal", "vertical"]) savestate = board.copy() if orientation == "horizontal": x = [random.randint(1, 9)] y = [random.randint(0, 9)] for i in range(1, 2): x.append(x[0] - i) else: x = [random.randint(0, 9)] y = [random.randint(1, 9)] for i in range(1, 2): y.append(y[0] - i) pos = x, y if len(pos[0]) > len(pos[1]): for i in range(2): inty = sum(pos[1]) if board[x][inty] == "O": board[x][inty] = "2" else: board = savestate.copy() return generate_2ship(board) else: for i in range(2): intx = sum(pos[0]) if board[intx][y] == "O": board[intx][y] = "2" else: board = savestate.copy() return generate_2ship(board) def is_hit(x, y): if board[x][y] == "X": print("Hit!") pboard[x][y] = "X" board[x][y] = "O" elif pboard[x][y] == "X": print("Already hit!") else: print("Miss!") pboard[x][y] = "M" def fill_board(): generate_5ship(board) generate_4ship(board) generate_4ship(board) generate_3ship(board) generate_3ship(board) generate_3ship(board) generate_2ship(board) generate_2ship(board) generate_2ship(board) generate_2ship(board) def get_win_condition(): for i in range(10): for j in range(10): if board[j] == "X": return False return True def get_misses(): misses = 0 for i in range(10): for j in range(10): if pboard[j] == "M": misses += 1 return misses def get_hits_left(): hits = 0 for i in range(10): for j in range(10): if board[j] == "X": hits += 1 return hits fill_board() while True: print_board(board, True) x, y = get_coords() is_hit(x, y) print_pboard(pboard) print("Hits left: " + str(get_hits_left())) if get_win_condition() == True: print("You win!") print("You missed " + str(get_misses()) + " times!") break print("Game Over!") This is the function I've been using to debug. What I was hoping it would do was save the current state of the board, then choose a "seed" coordinate, build the ship (in this case, 4 long) by checking for the string, "O", then replacing it with "X" to indicate a ship and then subtracting from the corresponding side of the tuple based on its decision to be placed vertically or horizontally. I substituted the X's with their corresponding lengths, again for debugging purposes. When the function begins to collide with a pre-existing ship on the board, I expect the else statement to replace the value of the newly changed board back to its savestate, then try again. Instead, it places as many as it can, stops when it realizes that it can't, skips the else statement entirely and just returns to the beginning of the function. P.S. If you're wondering why I took the sum for intx or inty, I for some reason couldn't figure out how to retrieve the only item in the list and store it as an integer. Taking the list's sum was the first thing that worked. def generate_4ship(board): orientation = random.choice(["horizontal", "vertical"]) savestate = board.copy() print("board") print_board(board, True) print("savestate") print_board(savestate, True) if orientation == "horizontal": x = [random.randint(3, 9)] y = [random.randint(0, 9)] for i in range(1, 4): x.append(x[0] - i) else: x = [random.randint(0, 9)] y = [random.randint(4, 9)] for i in range(1, 4): y.append(y[0] - i) pos = x, y if len(pos[0]) > len(pos[1]): for i in range(4): inty = sum(pos[1]) if board[x][inty] == "O": board[x[i]][inty] = "4" else: print("board before reverting") print_board(board, True) board = savestate.copy() print("board after reverting") print_board(board, True) return generate_4ship(board) else: for i in range(4): intx = sum(pos[0]) if board[intx][y[i]] == "O": board[intx][y[i]] = "4" else: board = savestate.copy() return generate_4ship(board) [url="https://stackoverflow.com/questions/79565748/reverting-to-savestate-doesnt-seem-to-work-whatsoever"]Continue reading...[/url][/i][/i][/i]