0
我一直在努力从HackerRank的问题的实施。也就是这个:https://www.hackerrank.com/challenges/battleship1p/problem循环创建了太多的孩子
我选择的语言是Python3,我想这将是一个很好的选择。现在,这里是一个基本的构造上我的计划是,在建工程:
class Cell():
# - status open
# h hit
# m miss
neighboring = []
status = None
cell_x = None
cell_y = None
def __init__(self, cell_x, cell_y):
self.cell_x = cell_x
self.cell_y = cell_y
status = '-'
def check_status(self):
return self.status
def check_ut(self):
if (self.status == "-"):
return True
def check_hit(self):
if (self.status == "h"):
return True
def check_miss(self):
if (self.status == "m"):
return True
def add_neighboring(self, c):
self.neighboring.append(c)
def check_neighboring(self):
for x in self.neighboring:
if (x.check_ut()):
return x
def check_neighboring_is_hit(self):
if self.check_hit():
for x in self.neighboring:
if (x.check_ut()):
return x
class Row():
Cells = []
y = None
def __init__(self, y):
for i in range(10):
self.Cells.append(Cell(i, y))
class Board():
Rows = None
def populate_neighbors(self):
for l in self.Rows:
for c in l.Cells:
if (c.cell_x > 0):
prev_cell = l.Cells[c.cell_x - 1]
prev_cell.add_neighboring(c)
c.add_neighboring(prev_cell)
if (c.cell_y > 0):
above_cell = self.Rows[c.cell_y - 1].Cells[c.cell_x]
above_cell.add_neighboring(c)
c.add_neighboring(above_cell)
print("test")
def NewRow(self):
self.Rows.append(Row(len(self.Rows)))
def __init__(self, rows):
self.Rows = []
for i in range(rows):
self.NewRow()
list_ships = [1, 1, 2, 2, 3, 4, 5]
z = Board(10)
z.populate_neighbors()
它试图重建一个球员的一局,这是我10行基板(10)initalise且还应创建每行10个单元格。但是由于在后台发生的事情,它似乎每行创建了100个字段,至少我的调试器是这样说的。如果你能给我一个重复或娱乐或类似的事情后果,我将不胜感激。
在populate_neighbors中,我的目标是找到特定单元格的所有相邻单元格,方法是遍历第一个所有行,然后逐个单元格,以将前一个选中的元素添加到当前选定的元素中,游戏场的高度。现在我希望你明白这是故意的。
这是最好的提示,你可以给我! :) – marena