Okay, I'm creating a Battleship game. It's going to be using the console for now, still don't know GUI. Anyways, this is my issue. I have created two boards as lists, the first for the user, the second for the computer. I have the computer generate random numbers and a direction, and it places the ship there. I have put in several checkers, to ensure that they cannot be placed on top of each other, or go off the board. My issue, is that it seems to freeze, I would expect it to take a while, maybe no more than a minute, but it stays the same for 10 minutes, and then it crashes. Any help?
# import needed modules
import os
import random
# These are the boards
user_board = [['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
comp_board = [['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
# This uses the random module to place the computer's boats at random
# Should be working completely fine now, all the checks are in place
def comp_placement(ship):
# This is for the initial square where the ship will go
d = [[random.randint(0, 9), random.randint(0, 9)]]
# Determine which way the ship faces
face = random.choice(["up", "down", "left", "right"])
# Place the ship
if face == "up":
if d[0] - ship[1] < 0:
comp_placement(ship)
if comp_board[d[0]][d[1]] == "+":
comp_placement(ship)
for x in range(0, ship[1]):
if comp_board[d[0]-x][d[1]] == "+":
comp_placement(ship)
if d[0] - ship[1] >= 0:
comp_board[d[0]][d[1]] = "+"
for x in range(0, ship[1]):
comp_board[d[0]-x][d[1]] = "+"
if face == "down":
try:
trying = comp_board[d[0]+ship[1]]
if comp_board[d[0]][d[1]] == "+":
comp_placement(ship)
for x in range(0, ship[1]):
if comp_board[d[0]+x][d[1]] == "+":
comp_placement(ship)
comp_board[d[0]][d[1]] = "+"
for x in range(0, ship[1]):
comp_board[d[0]+x][d[1]] = "+"
except:
comp_placement(ship)
if face == "left":
if d[1] - ship[1] < 0:
comp_placement(ship)
if comp_board[d[0]][d[1]] == "+":
comp_placement(ship)
for x in range(0, ship[1]):
if comp_board[d[0]][d[1]-x] == "+":
comp_placement(ship)
if d[0] - ship[1] >= 0:
comp_board[d[0]][d[1]] = "+"
for x in range(0, ship[1]):
comp_board[d[0]][d[1]-x] = "+"
if face == "right":
try:
trying = comp_board[d[1]+ship[1]]
if comp_board[d[0]][d[1]] == "+":
comp_placement(ship)
for x in range(0, ship[1]):
if comp_board[d[0]][d[1]+x] == "+":
comp_placement(ship)
comp_board[d[0]][d[1]] = "+"
for x in range(0, ship[1]):
comp_board[d[0]][d[1]+x] = "+"
except:
comp_placement(ship)
# Places the ships on the computer's board
def comp_place():
patrol = ["Patrol Boat", 2]
sub = ["Submarine", 3]
destroyer = ["Destroyer", 3]
battle = ["Battleship", 4]
ac = ["Aircraft Carrier", 5]
comp_placement(patrol)
comp_placement(sub)
comp_placement(destroyer)
comp_placement(battle)
comp_placement(ac)