我正在制作一个战舰游戏,当放置船只时,它们有时会重叠,函数中的if语句可以防止这种情况,但无济于事
我尝试更改if语句的含义:
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")
我希望有一个最终的栅格,该栅格取代随机放置的栅格,并且每个船只都不重叠,例如:
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 _ _
但是,如果接收到的船只重叠,例如:
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 _ _ _ _ _ _ _ _ _ _
答案 0 :(得分:1)
据我所见,您正在检查letters[y][x]
是否有重叠,但您正在将船停放在letters[y-1][x]
。这可能是问题吗?