我正在尝试创建一个网格,它将随机生成一条鱼,用户可以选择放置钓鱼线的位置,看看他们是否捕获了一条鱼。
为了让这个程序正常工作,我正在努力学习课程的概念。现在,我正在创建一个2x2网格,并定义了单元格中需要的内容,这就是我到目前为止所拥有的。
下面是我正在为这个项目工作的skelton的更多内容,但现在我正在尝试理解类单元格以及如何使它工作。
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()下面是我对类单元格的说明:
牢房:
location_coords:该单元格在板上的位置(一个坐标对象)。
contains_line:这个单元格是否包含用户的钓鱼线( bool)
:包含在这个单元格中的鱼(字符串)
init():接受为属性赋值的值。
str():返回单元格的字符串表示,即返回该单元格的内容(例如"“(nothing)、"F”(fish)、“”(行)、"F“(fish和line) )。
应在init中定义location_coords和fish吗?或者是单元格类中定义中的代码?
这学期我还在努力掌握我在python的最后一次作业的所有内容,所以我很抱歉,如果这是一个关于网站的大问题,我实在是太困惑了。
发布于 2016-12-12 02:54:19
设置Cell类
您的Cell类应该具有属性coords、contains_line和fish。这些应该在__init__中进行,就像您对coords属性所做的那样。一旦设置好这些,您就可以创建单元格,在那里您可以检查它们是否有鱼或钓鱼线等等。
带有这些属性的修改后的__init__,以及额外的参数,以便在创建单元格时可以设置它们,可能如下所示:
class Cell:
def __init__(self, row, col, fish='', line=False):
self.coords = Coordinate(row, col)
self.fish = fish
self.contains_line = line我们将fish的默认参数设置为空字符串'' (因为指定了fish是字符串),将line设置为False,因为我们假定游戏将从没有钓鱼线开始。代码的其他部分将填充这些内容。
在网格中使用单元格
网格定义为单元格列表,如下所示
[[Cell, Cell],
[Cell, Cell]]其中每一个都是单元格类的单独实例。每个单元格都有坐标(行、行),就像我们可以使用list[0]访问列表中的元素一样,我们也可以使用grid[0][0]访问网格中的特定单元。
如果我们选择一个特定的单元,比如说,selected_cell = grid[i][j],我们可以通过调用它的方法(比如selected_cell.add_fish() )来处理这个特定的单元。例如,如果我们想要将鱼添加到每个单元格中,我们可以在网格上循环,选择每个单元格,然后运行add_fish。
用细胞做事情
单元格的属性已经到位,现在您可以添加逻辑来做其他事情,比如添加fish,检查是否有fish,等等。
例如,如果我们想知道单元格是否有钓鱼线,我们可以检查self.contains_line,例如:
def contains_line(self):
return self.contains_line我们通过运行selected_cell.contains_line()进行检查(假设我们有一个名为selected_cell的单元格)。
如果我们想要添加一个fish,我们可以使用一个示例定义运行selected_cell.add_fish():
def add_fish(self):
self.fish = 'F'发布于 2016-12-12 02:25:15
因此,通常,您可以通过构造函数(本例中的init())或类中的其他方法初始化对象。有时,对象允许两者兼而有之(为了获得更大的灵活性)。真正由你来决定什么更方便。
在您的例子中,我会说是的,为什么不在init()中设置值呢?
细胞决定它是有一条鱼还是一条线是没有意义的。我将有一个单独的类,比如GameEngine (或main())具有这个逻辑。因此,无论谁拥有/创建单元格,都应该能够将初始“状态”值传递给它。
稍后,随着游戏的进行,在Cell类中还会有其他方法来更新其状态。也许一条鱼如果不被另一条鱼占据,它就会移动到邻近的细胞里?
不知道这是否对你有帮助。面向对象设计的整个思想是,您不把所有的代码放在一个单一的方法中,而是设计对象和分配责任。稍后,随着代码变得更大和更复杂,维护它和添加更多特性就更容易了。
https://stackoverflow.com/questions/41093180
复制相似问题