def __init__(self, config): self.config = config self._set_constraints() self.pond = Pond() self.lilly_pads = [] self.frogs = [] self.create_all_frogs() self.create_all_lilly_pads()
def test_get_frog_start_coords_returns_list_of_coordinates(self): # arrange pond = Pond() # act coord_list = pond.get_frog_start_coords() # assert assert type(coord_list) == list \ and type(coord_list[0][0]) == float
def test_pond_has_circle_in_expected_position(self): # arrange radius = 5 Pond.radius = radius # act pond = Pond() # assert assert pond.circle.centre_point == Point(radius, radius)
def gofishing(self): print '1.Add fish' print '2.catch fish' choice = input('enter your choice:') if choice == 1: pond = Pond() fish_add = pond.add(fish_name, fish_size, fish_id) elif choice == 2: pond = Pond() fishing = pond.catch()
def map_sprites(): global decorations decorations = pygame.sprite.Group() decorations.add(Pond(400, 325)) decorations.add(Road_2(150, 0)) decorations.add(Road_2(350, 0)) decorations.add(Road_2(550, 0)) decorations.add(Road_2(750, 0)) decorations.add(Road(0, 75)) decorations.add(Road(0, 375)) decorations.add(Road(0, 675)) decorations.add(Box(5, 5)) decorations.add(Box(495, 20)) decorations.add(Box(295, 320)) decorations.add(Box(95, 130)) decorations.add(Box(605, 320)) decorations.add(Box(205, 620)) decorations.add(Box(695, 430)) decorations.add(Box(945, 620)) decorations.add(Box(405, 730)) decorations.add(Box(805, 745)) decorations.add(Tower(0, 275)) decorations.add(Tower(200, 125)) decorations.add(Tower(950, 275)) decorations.add(Tower(300, 575)) decorations.add(Fan(700, 125)) decorations.add(Fan(950, 125)) decorations.add(Fan(0, 625)) decorations.add(Fan(600, 625)) decorations.add(Fan(300, 0)) decorations.add(Fan(200, 750)) decorations.add(Tree(400, 125)) decorations.add(Tree(500, 625)) decorations.add(Flower(405, 325)) decorations.add(Flower(530, 325)) decorations.add(Flower(465, 300)) decorations.add(Flower(405, 460)) decorations.add(Flower(530, 460)) decorations.add(Flower(465, 480)) decorations.add(Arrow(875, 25))
def read_pond_list_from_workbook(self, book): ''' READ POND LIST FROM WORKBOOK Opens the xlrd workbook and returns a list of Pond objects. @param book: an xlrd Workbook @return: list of Pond objects, storing the information in the workbook. @rtype: list ''' ############## #Worksheets ############## nsheets = book.nsheets sheet_names = book.sheet_names() pond_data_workSheet = xlrd.book benthic_photo_data_workSheet = xlrd.book phytoplankton_photo_data_sheet = xlrd.book shape_data_sheet = xlrd.book if (nsheets < self.DEFAULT_NUMBER_OF_SHEETS ): #Pond, benthic, planktonic. Guide optional. raise IOError( "file format incorrect. Number of sheets less than expected") if (self.POND_DATA_SHEET_NAME in sheet_names and self.BENTHIC_PHOTO_DATA_SHEET_NAME in sheet_names and self.PHYTOPLANKTON_PHOTO_DATA_SHEET_NAME in sheet_names and self.SHAPE_DATA_SHEET_NAME in sheet_names): pond_data_workSheet = book.sheet_by_name(self.POND_DATA_SHEET_NAME) benthic_photo_data_workSheet = book.sheet_by_name( self.BENTHIC_PHOTO_DATA_SHEET_NAME) phytoplankton_photo_data_sheet = book.sheet_by_name( self.PHYTOPLANKTON_PHOTO_DATA_SHEET_NAME) shape_data_sheet = book.sheet_by_name(self.SHAPE_DATA_SHEET_NAME) else: #Standard sheet names not detected. Attempting to read using sheet indices. pond_data_workSheet = book.sheet_by_index( self.POND_DATA_SHEET_INDEX) benthic_photo_data_workSheet = book.sheet_by_index( self.BENTHIC_PHOTO_DATA_SHEET_INDEX) phytoplankton_photo_data_sheet = book.sheet_by_name( self.PHYTOPLANKTON_PHOTO_DATA_SHEET_INDEX) shape_data_sheet = book.sheet_by_name(self.SHAPE_DATA_SHEET_INDEX) ############## #Rows, Columns ############## pond_data_workSheet_num_rows = pond_data_workSheet.nrows benthic_data_workSheet_num_rows = benthic_photo_data_workSheet.nrows phytoplankton_photo_data_sheet_num_rows = phytoplankton_photo_data_sheet.nrows shape_data_sheet_num_rows = shape_data_sheet.nrows ################################################# #make all the objects! ################################################# pond_list = [ ] #list of pond objects. The same water body on a different day counts as a separate "Pond" ################################################ #Make Pond objects from pond_data sheet ################################################ sheet = pond_data_workSheet num_rows = pond_data_workSheet_num_rows #TODO: read until blank space encountered might be better. curr_row = self.DEFAULT_FIRST_DATA_ROW #start at 1. row 0 is column headings while curr_row < num_rows: row = sheet.row(curr_row) #values try: row_year_value = row[self.yearIndex].value row_doy_value = row[self.dayOfYearIndex].value row_lakeID_value = row[self.lakeIDIndex].value row_kd_value = float(row[self.kd_index].value) row_noonlight_value = float( row[self.noon_surface_light_index].value) row_lod_value = float(row[self.length_of_day_index].value) except Exception as e: print str(e) print "Error: couldn't read values properly." #Do we need to make a pond object? pond = None pond = next( (i for i in pond_list if (i.get_lake_id() == row_lakeID_value and i.get_day_of_year( ) == row_doy_value and i.get_year() == row_year_value)), None ) #source: http://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi if pond is None: #not in list. Must create Pond object emptyShape = BathymetricPondShape( {}) #initialize with empty dict pond = Pond(row_year_value, row_lakeID_value, row_doy_value, row_lod_value, row_noonlight_value, row_kd_value, emptyShape, [], [], self.DEFAULT_TIME_INTERVAL) pond_list.append(pond) curr_row += 1 ####################################################### #we made all the ponds. Time to add all the members ####################################################### ################################# #Shape data from shape_data sheet ################################# sheet = shape_data_sheet num_rows = shape_data_sheet_num_rows curr_row = self.DEFAULT_FIRST_DATA_ROW #start at 1. row 0 is column headings while curr_row < num_rows: row = sheet.row(curr_row) #values row_lakeID_value = row[self.shape_ID_index].value row_depth_value = float(row[self.shape_depth_index].value) row_area_value = float(row[self.shape_area_index].value) row_dict = {row_depth_value: row_area_value} row_shape = BathymetricPondShape(row_dict) #find the correct pond pond = None # pond = next((i for i in pond_list if (i.get_lake_id()== row_lakeID_value )),None) #source: http://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi #http://stackoverflow.com/questions/14366511/return-the-first-item-in-a-list-matching-a-condition # matchingPonds = filter(next((i for i in pond_list if (i.get_lake_id()== row_lakeID_value )),None), pond_list) for pond in pond_list: if (pond.get_lake_id() == row_lakeID_value): pond.update_shape(row_shape) #add to Pond #increment while loop to next row curr_row += 1 ############### #Benthic data ############### sheet = benthic_photo_data_workSheet num_rows = benthic_data_workSheet_num_rows curr_row = self.DEFAULT_FIRST_DATA_ROW #start at 1. row 0 is column headings while curr_row < num_rows: row = sheet.row(curr_row) #values row_year_value = row[self.yearIndex].value row_doy_value = row[self.dayOfYearIndex].value row_lakeID_value = row[self.lakeIDIndex].value row_light_penetration_proportion_value = float( row[self.benthic_light_penetration_proportion_index].value) row_pmax_value = float(row[self.benthic_pmax_index].value) row_ik_value = float(row[self.benthic_ik_index].value) #find the correct pond pond = None pond = next( (i for i in pond_list if (i.get_lake_id() == row_lakeID_value and i.get_day_of_year( ) == row_doy_value and i.get_year() == row_year_value)), None ) #source: http://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi if pond is None: #something is terribly wrong raise FormatError( "Something went wrong. Benthic Measurement with DOY " + str(row_doy_value) + " and Lake ID " + row_lakeID_value + " does not match to any Pond.") #TODO: handle this better. else: #create PhotoSynthesisMeasurement object using values specific to that benthic_measurement/row row_depth_value = pond.calculate_depth_of_specific_light_percentage( row_light_penetration_proportion_value ) #convert from light proportions to depth in meters. benthic_measurement = BenthicPhotosynthesisMeasurement( row_depth_value, row_pmax_value, row_ik_value) pond.add_benthic_measurement_if_photic(benthic_measurement) #add to Pond curr_row += 1 #end of while loop ############### #Phyto data ############### sheet = phytoplankton_photo_data_sheet num_rows = phytoplankton_photo_data_sheet_num_rows curr_row = self.DEFAULT_FIRST_DATA_ROW #start at 1. row 0 is column headings while curr_row < num_rows: row = sheet.row(curr_row) #values row_year_value = row[self.yearIndex].value row_doy_value = row[self.dayOfYearIndex].value row_lakeID_value = row[self.lakeIDIndex].value row_thermal_layer_value = row[self.phyto_thermal_layer_index].value row_depth_value = row[self.phyto_depth_index].value row_phyto_pmax_value = row[self.phyto_pmax_index].value row_alpha_value = row[self.phyto_alpha_index].value row_beta_value = row[self.phyto_beta_index].value #find the correct pond pond = None pond = next( (i for i in pond_list if (i.get_lake_id() == row_lakeID_value and i.get_day_of_year( ) == row_doy_value and i.get_year() == row_year_value)), None ) #source: http://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi if pond is None: #something is terribly wrong raise FormatError( "Something went wrong. Benthic Measurement with DOY " + str(row_doy_value) + " and Lake ID " + row_lakeID_value + " does not match to any Pond.") else: #create PhotoSynthesisMeasurement object using values specific to that benthic_measurement/row phyto_measurement = PhytoPlanktonPhotosynthesisMeasurement( row_thermal_layer_value, row_depth_value, row_phyto_pmax_value, row_alpha_value, row_beta_value) pond.add_phytoplankton_measurement(phyto_measurement) #add to Pond curr_row += 1 return pond_list
def message_display(pond, text, dis, lenght=650, width=100): largeText = pygame.font.Font('freesansbold.ttf', 115) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = (lenght, (width)) dis.blit(TextSurf, TextRect) pygame.display.update() if __name__ == '__main__': leftside_frogs_number = int( input('Enter number of frogs for the left side: ')) rightside_frogs_number = int( input('Enter number of frogs for the right side: ')) pond = Pond(leftside_frogs_number, rightside_frogs_number) pond.set_pond_string() pygame.init() dis = pygame.display.set_mode((1500, 500)) dis.fill((0, 100, 255)) pygame.display.set_caption('Frogs Riddle') print_pond_in_pygame(pond, dis) while not pond.is_arranged(): pond.move_frog() print_pond_in_pygame(pond, dis) message_display(pond, 'SOLVED!', dis) message_display(pond, f'In {pond.steps_count} steps', dis, 650, 400) time.sleep(5) pygame.quit()
class FrogPond(object): def __init__(self, config): self.config = config self._set_constraints() self.pond = Pond() self.lilly_pads = [] self.frogs = [] self.create_all_frogs() self.create_all_lilly_pads() def _set_constraints(self): # Pond Pond.radius = self.config.pond_radius # Frog Frog.min_range = self.config.frog_min_jump_distance Frog.max_range = self.config.frog_max_jump_distance # LillyPad LillyPad.min_radius = 2/100 * self.config.pond_radius LillyPad.max_radius = self.config.lilly_pad_radius_max_percentage_of_pond_size/100 * self.config.pond_radius def _choose_frog_start_point(self): point = random.choice(self.pond.get_frog_start_coords()) return Point(point[0], point[1]) def _choose_lilly_pad_position(self, pad_radius): min_x, min_y, max_x, max_y = self.pond.circle.get_bounds() while True: x = random.uniform(min_x, max_x) y = random.uniform(min_y, max_y) pad = Circle(Point(x, y), pad_radius) if self.pond.lilly_pad_in_pond(pad) and not self._is_overlapping_current_lilly_pads(pad): return Point(x, y) def _is_overlapping_current_lilly_pads(self, circle): for lilly_pad in self.lilly_pads: if self._lilly_pads_overlap(circle, lilly_pad): return True return False @staticmethod def _lilly_pads_overlap(circle, lilly_pad): return circle.intersects_circle(lilly_pad.circle) @staticmethod def _choose_radius(min_radius, max_radius): return random.uniform(min_radius, max_radius) def _create_frog(self, num): position = self._choose_frog_start_point() radius = self._choose_radius(Frog.min_range, Frog.max_range) return Frog(position, radius, num) def _create_lilly_pad(self): radius = self._choose_radius(LillyPad.min_radius, LillyPad.max_radius) position = self._choose_lilly_pad_position(radius) return LillyPad(position, radius) def _create_centre_lilly_pad(self): position = self.pond.circle.centre_point radius = self._choose_radius(LillyPad.min_radius, LillyPad.max_radius) return LillyPad(position, radius, centre_pad=True) def create_all_frogs(self): for num in range(self.config.number_of_frogs_in_game): self.frogs.append(self._create_frog(num)) def create_all_lilly_pads(self): self.lilly_pads.append(self._create_centre_lilly_pad()) for num in range(self.config.number_of_lilly_pads_on_pond - 1): self.lilly_pads.append(self._create_lilly_pad()) def get_frog_circles(self): return [frog.get_range_circle() for frog in self.frogs] def get_pond_circle(self): return self.pond.circle def get_centre_lilly_pad_circle(self): return self.lilly_pads[0].circle def get_lilly_pad_circles(self): return [pad.circle for pad in self.lilly_pads]