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)

Recommended Answers

All 3 Replies

You have an endless recursive loop within your function comp_placement(ship) that creates new random numbers each time it is called. In other words, this would be an endless loop ...

def comp_placement(ship):
    # function calls itself
    comp_placement(ship)

Also, carefully look at your logic statements and allow the function to return when the ship is marked.

Hint, you can also create the two boards this simple way ...

user_board = []
comp_board = []
for row in range(10):
    user_board.append([0]*10)
    comp_board.append([0]*10)

... let Python do the work for you.

All right, so I've been staring at the code, and I've been thinking, is there some way to have it select numbers that aren't already selected? And that won't go out of bounds?
! I got it!
I'll try soem while: loops, and check back with you

:D I got it all figured out, and it's a lot more readable too:

import random

# Thanks to Vegaseat
user_board = []
comp_board = []
comp_boardu = []
for row in range(10):
    user_board.append(['O']*10)
    comp_board.append(['O']*10)
    comp_boardu.append(['O']*10)

def comp_placement2(ship):
    d = [random.randint(0, 9), random.randint(0, 9)]
    di = random.choice(["up", "down", "left", "right"])
    a = 0
    if comp_board[d[0]][d[1]] == "+":
        a = 0
    elif comp_board[d[0]][d[1]] == "O":
        if di == "up":
            if d[0] - ship[1] < 0:
                a = 0
            elif d[0] - ship[1] >= 0:
                a = 1
                for x in range(0, ship[1]):
                    if comp_board[d[0]-x][d[1]] == "+":
                        a = 0
                if a == 1:
                    for x in range(0, ship[1]):
                        comp_board[d[0]-x][d[1]] = "+"
                        a = 1
        elif di == "down":
            try:
                trying = comp_board[d[0]+ship[1]]
                a = 1
                for x in range(0, ship[1]):
                    if comp_board[d[0]-x][d[1]] == "+":
                        a = 0
                if a == 1:
                    for x in range(0, ship[1]):
                        comp_board[d[0]+x][d[1]] = "+"
                    a = 1
            except:
                a = 0
        elif di == "left":
            if d[1] - ship[1] < 0:
                a = 0
            elif d[1] - ship[1] >= 0:
                a = 1
                for x in range(0, ship[1]):
                    if comp_board[d[0]][d[1]-x] == "+":
                        a = 0
                if a == 1:
                    for x in range(0, ship[1]):
                        comp_board[d[0]][d[1]-x] = "+"
                        a = 1
        elif di == "right":
            try:
                trying = comp_board[d[1]+ship[1]]
                a = 1
                for x in range(0, ship[1]):
                    if comp_board[d[0]][d[1]+x] == "+":
                        a = 0
                if a == 1:
                    for x in range(0, ship[1]):
                        comp_board[d[0]][d[1]+x] = "+"
                    a = 1
            except:
                a = 0
    comp_placement2.a = a

def comp_place2():
    patrol = ["Patrol Boat", 2]
    sub = ["Submarine", 3]
    destroyer = ["Destroyer", 3]
    battle = ["Battleship", 4]
    ac = ["Aircraft Carrier", 5]
    a = 0
    while a == 0:
        comp_placement2(patrol)
        if comp_placement2.a == 1:
            a = 1
        if comp_placement2.a == 0:
            a = 0
    a = 0
    while a == 0:
        comp_placement2(sub)
        if comp_placement2.a == 1:
            a = 1
        if comp_placement2.a == 0:
            a = 0
    a = 0
    while a == 0:
        comp_placement2(destroyer)
        if comp_placement2.a == 1:
            a = 1
        if comp_placement2.a == 0:
            a = 0
    a = 0
    while a == 0:
        comp_placement2(battle)
        if comp_placement2.a == 1:
            a = 1
        if comp_placement2.a == 0:
            a = 0
    a = 0
    while a == 0:
        comp_placement2(ac)
        if comp_placement2.a == 1:
            a = 1
        if comp_placement2.a == 0:
            a = 0

It doesn't freeze at all, and it's quite quick as well ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.