所以我有一个工作网格和随机放置在网格中的鱼,我不确定是什么;用户将在2x2网格上输入一个坐标,并且*将出现在带有鱼的网格上,但我不知道该怎么做。此外,我不确定如何验证鱼和线是否在同一个细胞中,这意味着用户捕获了一条鱼。
以下是所有代码,以便您可以看到所有内容如何一起工作,但错误/不完整的功能(据我所知)是
CarpetSea str 功能 - 没有* - 我认为这与Cell类中的if elif else语句有关,但我不确定
class Coordinate:
'''
'''
def __init__(self, row, col):
'''
'''
self.row = row
self.col = col
def __str__(self):
'''
'''
return "(%d, %d)" %(self.row, self.col)
class Cell:
'''
'''
def __init__(self, row, col):
'''
'''
self.row = row
self.col = col
self.coords = Coordinate(row, col)
self.fish = ""
self.contains_line = False
def __str__(self):
'''
'''
if self.fish:
if self.contains_line:
result = 'F*'
else:
result = 'F'
else:
if self.contains_line:
result = '*'
else:
result = ' '
return result
class CarpetSea:
'''
'''
fish_caught = 0
def __init__(self, N):
'''
'''
self.N = N
self.grid = []
for i in range(self.N):
row = []
for j in range(self.N):
cell = Cell(i, j)
row.append(cell)
self.grid.append(row)
self.available_fish = ["Salmon", "Marlin", "Tuna", "Halibut"]
def __str__(self):
'''
returns a string representation of a CarpetSea, i.e. display the organized contents of each cell.
Rows and columns should be labeled with indices.
Example (see "Example Run" in the PA8 specs for more):
0 1
--------
0|M | |
--------
1| |* |
--------
Note: call Cell's __str__() to get a string representation of each Cell in grid
i.e. "M " for Cell at (0,0) in the example above
'''
str1 = ' | ' + '0' ' | ' +'1' + ' |\n'
str1 += '-' * 12 + '\n'
for i, k in enumerate(self.grid):
str1 += ' {0}| '.format(i)
for j in k:
str1 += '{0} | '.format(j)
str1 += '\n'
str1 += '-' * 12 + '\n'
return(str1)
drop_fishing_line - 不确定如何获得该行在用户输入单元格中的验证的bool条件
def drop_fishing_line(self, users_coords):
'''
accepts the location of the user's fishing line (a Coordinate object).
Marks the cell as containing the line.
'''
line_cell = self.grid[users_coords.row][users_coords.col]
line_cell.contains_line == True
print(line_cell)
check_fish_caught - 我不认为这是正确的。我一直在搞乱它,这有点混乱
def check_fish_caught(self, fish,):
'''
If the cell containing the fishing line also contains a fish, returns the fish.
Otherwise, return False.
'''
if str(self.grid) == "F*":
print("The fish was caught!")
print("\n")
else:
print("The fish was not caught!")
print("\n")
最后 - 如果以上所有内容都得到解决,有没有办法让它在网格中代替F而不是随机选择的鱼的第一个字母? 帮助任何这些功能将是一种生活品味,非常感谢你!