I'm making a Battleship game and when placing the boats they sometimes overlap, the if statements in the function are there to prevent this but aren't helping
I've tried changing the if statements implication:
import random
# Here I create a grid
a = ["a","_","_","_","_","_","_","_","_","_","_"]
b = ["b","_","_","_","_","_","_","_","_","_","_"]
c = ["c","_","_","_","_","_","_","_","_","_","_"]
d = ["d","_","_","_","_","_","_","_","_","_","_"]
e = ["e","_","_","_","_","_","_","_","_","_","_"]
f = ["f","_","_","_","_","_","_","_","_","_","_"]
g = ["g","_","_","_","_","_","_","_","_","_","_"]
h = ["h","_","_","_","_","_","_","_","_","_","_"]
i = ["i","_","_","_","_","_","_","_","_","_","_"]
j = ["j","_","_","_","_","_","_","_","_","_","_"]
letters = [a,b,c,d,e,f,g,h,i,j]
# This is just asphetic
def board(line_0, Letters):
print
print line_0
for i in Letters:
print " %s %s %s %s %s %s %s %s %s %s %s" % (i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9],i[10])
#Here's the function
# And what I'm trying to do is to position boat on the grid
def place_boat(letters, length, BT, Name):
while len(BT) != length:
AC_y = random.randint(0,9)
AC_x = random.randint(0,9)
if letters[AC_y][AC_x] == "_": # These if statements
rotation = random.randint(0,1)
if rotation == 0:
if AC_y >= 5:
del BT[:]
for i in range(0, length):
if letters[AC_y - i][AC_x] == "_": # Don't work
BT.append(str(AC_y - i) + str(AC_x))
else:
break
if len(BT) != length:
del BT[:]
if AC_y <= 6 and AC_y != 0:
for i in range(0, length):
if letters[AC_y + i][AC_x] == "_": # Since they end up
BT.append(str(AC_y + i) + str(AC_x))
else:
break
else:
if len(BT) != length:
del BT[:]
if AC_x >= 5:
for i in range(0, length):
if letters[AC_y][AC_x - i] == "_": # reassining multiple values
BT.append(str(AC_y) + str(AC_x - i))
else:
break
if len(BT) != length:
del BT[:]
if AC_x <= 6 and AC_y != 0:
for i in range(0, length):
if letters[AC_y][AC_x + i] == "_": # to the same coordinate
BT.append(str(AC_y) + str(AC_x + i))
else:
break
for i in BT:
letters[int(BT[BT.index(i)][0]) - 1][int(BT[BT.index(i)][1])] = Name
print BT
board(line_0, letters)
return BT
return letters
#Here are the lists that represent the boat I want to create
AC = [] #
Bat = [] #
Cru = [] #
Sub_1 = [] #
Sub_2 = [] #
Des_1 = [] #
Des_2 = [] #
#Here I call the function for the individual boats
place_boat(letters, 5, AC, "A")
place_boat(letters, 4, Bat, "B")
place_boat(letters, 3, Cru, "C")
place_boat(letters, 3, Sub_1, "S")
place_boat(letters, 3, Sub_2, "$")
place_boat(letters, 2, Des_1, "D")
place_boat(letters, 2, Des_2, "T")
I expect to have a final grid that displais a grid with randomlly placed and not overlaping boats everythime, example:
1 2 3 4 5 6 7 8 9 10
a _ _ _ _ _ _ _ _ T _
b _ _ C _ _ _ _ _ T _
c _ _ C _ _ _ _ _ _ _
d _ _ C _ _ B B B B _
e _ _ _ _ _ _ _ _ _ _
f D D _ _ _ $ $ $ _ _
g _ _ _ _ _ _ _ _ _ _
h _ _ _ _ _ _ _ _ _ _
i _ _ A A A A A _ _ _
j _ _ _ _ _ S S S _ _
But instead recieve one with overlaping boats, example:
1 2 3 4 5 6 7 8 9 10
a _ _ _ _ _ _ _ _ _ _
b _ _ T T _ _ A _ _ _
c _ _ C _ _ _ A _ _ _
d _ _ C _ _ B B B B _
e _ _ _ _ _ _ A _ _ _
f _ _ _ _ _ $ $ $ _ _
g _ _ _ D _ _ _ _ _ _
h _ _ _ D S S _ _ _ _
i _ _ _ _ _ _ _ _ _ _
j _ _ _ _ _ _ _ _ _ _
Aucun commentaire:
Enregistrer un commentaire