创建网格并在其中放置“鱼”

时间:2016-12-12 10:22:39

标签: python list class random

我正在创建一个“地毯鱼”游戏,我有我的2x2网格代码,但我不知道如何随机将我的“鱼”放在其中一个单元格或“钓鱼线”中并检查如果他们在同一个网格中。

我正在用课程这样做,我仍然试图解决它们。这是我目前的代码。

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:
    '''

    '''

    options = [" ", "F", "*", "F*"]
    def __init__(self, row, col):
        '''

        '''
        self.coords = Coordinate(row, col)
        self.fish = ""
        self.contains_line = False



    def __str__(self):
        '''
        '''
        return "%s"%(self.options)




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"]
        self.celloptions = [" ", "*", "S", "S*", "M", "M*", "T", "T*", "H", "H*"]

    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 -------- "%(self.celloptions)         
    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.
        '''
        random_fish = random.choice(self.grid)
        return random_fish

    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.
        '''
        self.coords = int(input("Please enter the coordnate that you wihsh to place your line: "))

        #this sould put the two numbers in a list
        self.coords.split()

    def check_fish_caught(self):
        '''
        If the cell containing the fishing line also contains a fish, returns the fish. 
        Otherwise, return False.
        '''


        if self.fish in self.coords:
            return True
        else:
            return False



def main():

    '''

    '''
    user_coords = str(input("Please enter the coordnate that you wihsh to place your line: "))
    coord = Coordinate(1,1)
    info = CarpetSea(2)
    info.randomly_place_fish()
    info.drop_fishing_line(user_coords)
    info.check_fish_caught()

main()

我不确定如何在CarpetSea中获得 str 功能来显示正确的值,因为它们会根据用户输入而改变。

如何将鱼和线放入细胞?

1 个答案:

答案 0 :(得分:0)

随机鱼很简单。

cell = random.choice(random.choice(self.grid))
cell.fish = random.choice(self.celloptions)

我不认为这种方法甚至应该返回一些东西。

您的__str__()应该遍历单元格,而不是self.celloptions。像这样的东西会从每一行捕获一条鱼。如果你还没有研究它们,括号中的那个东西是一个生成器表达式,它是自切片面包以来最伟大的发明。

return 'That_lengthy_string_of_yours'%(self.grid[x,y].fish for x in range(self.N) for y in range(self.N))

另外,如果它是python 3. *(老实说,你为什么要在2016年学习Py2?),''.format()优先于%语法形成字符串。这是风格问题,而不是实际功能,但风格在Python中很重要。

相关问题