def make_ship(length):
ship = []
ship_tmp = []
ship_row = random_row(board, length)
ship.append(ship_row)
ship_col = random_col(board, length)
ship.append(ship_col)
i = 0
if randint(0,1) == 0:
while i<length:
ship_tmp.append(ship[:])
ship[0] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(length)
else:
ship_all.append(ship)
i+=1
else:
while i<length:
ship_tmp.append(ship[:])
ship[1] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(length)
else:
ship_all.append(ship)
i+=1
这个问题的目的是在板上产生几艘船并防止它们相互接触,但make_ship()
递归没有解决,如何解决这个问题?
答案 0 :(得分:0)
使用全局变量:
global i
i=0
def make_ship(length):
global i
ship = []
ship_tmp = []
ship_row = random_row(board, length)
ship.append(ship_row)
ship_col = random_col(board, length)
ship.append(ship_col)
if randint(0,1) == 0:
while i<length:
ship_tmp.append(ship[:])
ship[0] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(length)
else:
ship_all.append(ship)
i+=1
else:
while i<length:
ship_tmp.append(ship[:])
ship[1] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(length)
else:
ship_all.append(ship)
i+=1
您也可以尝试使用类来解决问题:
class ship_class(object):
def __int__(self,length):
self.i=0
self.length=length
def make_ship(self):
ship = []
ship_tmp = []
ship_row = random_row(board, length)
ship.append(ship_row)
ship_col = random_col(board, length)
ship.append(ship_col)
if randint(0,1) == 0:
while self.i<self.length:
ship_tmp.append(ship[:])
ship[0] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(self.length)
else:
ship_all.append(ship)
self.i+=1
else:
while self.i<self.length:
ship_tmp.append(ship[:])
ship[1] += 1
for ship in ship_tmp:
if ship in ship_all:
make_ship(self.length)
else:
ship_all.append(ship)
self.i+=1
答案 1 :(得分:0)
你应该学习的东西:
全局变量很少是一个好主意;他们打破封装并进行调试,因为你无法分辨变化的来源(或者没有来自,有时!)。
有意义的评论是一件非常好的事情。有意义的变量名称甚至更好。精心设计,有意义的课程是来自天堂的礼物。
保持您的功能简单 - 理想情况下少于10或12行。这可以让你立刻推理整个事情,所以你可以一目了然地看到大多数问题。如果一次性考虑整件事情太复杂了,也许你应该进一步分解。
递归适用于处理分支结构。船舶不是分支结构。
这是一个简单的基于类的解决方案:
import random
class Board:
def __init__(self, width, height, empty_char="."):
"""
Create a new gameboard
"""
self.width = width
self.height = height
self.empty_char = empty_char
self.ships = []
def __str__(self):
# make an empty board
board = [[self.empty_char] * self.width for y in range(self.height)]
# add ships
for ship in self.ships:
ship.draw(board)
# return as string
return "\n".join("".join(row) for row in board)
def add_rand_ship(self, length, char, tries=10):
"""
Add a ship at a random location on the board,
not overlapping any existing ships
"""
for try_ in range(tries):
# create a random Ship
new_ship = Ship.rand_ship(length, char, self.width, self.height)
# make sure it doesn't intersect any existing ships
if not any(new_ship.hits(ship) for ship in self.ships):
self.ships.append(new_ship)
return
raise ValueError("Failed creating new ship after {} tries".format(tries))
class Ship:
def __init__(self, x, y, length, char, horiz=True):
self.x = x
self.y = y
self.length = length
self.char = char
self.horiz = horiz
# get every square occupied by this ship
if self.horiz:
self.locations = set((x + dx, y) for dx in range(self.length))
else:
self.locations = set((x, y + dy) for dy in range(self.length))
def draw(self, board):
"""
Draw ship to board
"""
for x,y in self.locations:
board[y][x] = self.char
def hits(self, ship):
"""
Does this ship occupy any of the same locations as the other one?
"""
return bool(self.locations & ship.locations)
@classmethod
def rand_ship(cls, length, char, board_width, board_height):
"""
Create a new Ship at a random board location
"""
horiz = random.choice((False, True))
x = random.randint(0, board_width - (length if horiz else 1))
y = random.randint(0, board_height - (1 if horiz else length))
return cls(x, y, length, char, horiz)
def main():
bd = Board(30, 20)
for length in (2, 2, 3, 4):
for side in ("X", "O"):
bd.add_rand_ship(length, side)
print(bd)
main()
产生类似的东西:
..............................
..............................
..............................
.X............................
.X............................
.X............................
..............................
..............................
..........OOO.................
...............XX.............
..............................
...X..........................
...X......OOOO................
...X..........................
...X..........................
............OO................
..............................
..............................
...X.....................O....
...X.....................O....