I am creating a game of "Carpet Fish", I have my code for my 2x2 grid, but I am not sure how to randomly put my "fish" in one of the cells or the "fishing line" and to check if they are in the same grid.
I am doing this with classes, and I am still trying to figure them out. Here is the code I currently have.
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():
'''
'''
main()
I'm not sure how to get the str funtion in CarpetSea to display the correct values, because they change depending on the user input.
Also I am not sure how to bug check with classes, because I do not know how to run individual things, such as the grid, or randomly generating a cell.
Aucun commentaire:
Enregistrer un commentaire