我已经创建了板,但是当我尝试使用我的功能放置船只时,它会填满整个列。当我使用 1 的长度时,我希望它放置一个 X 而不是填满整个列 函数
def ship(length):
toplace = True
placed = False
while placed == False:
row = random.randint(0,9)
column = random.randint(0,9)
direction = random.randint(1,4)
direction = 1
if direction == 1 and ((row+length)-1)<10:
for i in range(length):
if board[row+i][column]!="O":
toplace = False
else:
pass
if toplace == True:
placed = True
for i in range(length):
temp = board[row+i]
temp[column]= "X"
board[row+i]=temp
运行输出的代码
ship(1)
for i in range(size):
print(i)
print(board[i])
创建看板的代码
size = 10
btemp = []
for i in range(size):
btemp.append("O")
for i in range(size):
board.append(btemp)