【发布时间】:2016-12-12 01:50:50
【问题描述】:
我正在尝试创建一个随机生成鱼的网格,用户可以选择放置钓鱼线的位置以查看他们是否钓到了鱼。
为了使程序正常工作,我正在努力解决类的概念。现在我正在创建一个 2x2 网格并定义单元格中需要的内容,这就是我目前所拥有的。
这是我正在为这个项目工作的更多骨架,但现在我正在尝试了解 Class 单元以及如何使其工作
import random
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.coords = Coordinate(row, col)
self.fish = ""
self.contains_line = False
def contains_line(self):
return self.contains_line
def add_fish(self):
self.fish = "F"
def __str__(self):
'''
'''
return
class CarpetSea:
'''
'''
num_of_fish = 0
total_time = 0
def __init__(self, N):
'''
'''
self.N = N
N = 2
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
'''
return " 0 1 \n -------- \n 0| %s | %s | \n -------- \n 1| %s | %s | \n -------- "%()
def randomly_place_fish(self):
'''
randomly selects coordinates of a cell and randomly selects a fish from the available_fish list attribute.
Marks the cell as containing the fish.
'''
pass
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.
'''
pass
def check_fish_caught(self):
'''
If the cell containing the fishing line also contains a fish, returns the fish.
Otherwise, return False.
'''
pass
def main():
main()
这是我对 Cell 类的说明:
单元格:
location_coords:此单元格在棋盘上的位置(坐标对象)。
contains_line:此单元格是否包含用户的钓鱼线(布尔值)
fish:此单元格中包含的鱼(字符串)
init():接受要分配给属性的值。
str():返回一个单元格的字符串表示,即返回这个单元格的内容(例如“”(无)、“F”(鱼)、“”( line), "F" (鱼和线)
是否应该在 init 中定义 location_coords 和 fish ?还是在 Cell 类的定义中编码
我仍在努力掌握本学期 Python 的最后作业的所有内容,所以如果这对网站来说是一个很大的问题,我深表歉意,我非常困惑。
【问题讨论】:
-
当整个程序可能只是
import random和if random.random() < 0.25: print('success')时,使用类似乎有点牵强。 -
我认为这就是为什么整个项目让我如此困惑的原因,因为有更有效的方法来完成它,但我必须按照我的老师希望它如何编码的说明和大纲。 .@tigerhawkT3 ..
-
我提供了更多代码,向您展示骨架格式,但现在我只是想弄清楚如何让 Class 单元工作......@TigerhawkT3 如果你有任何建议,我真的可以使用一些帮助