def test_find_single_nearby_food_(self): for test_num in range(0, 100): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 0) food_location = [-1, -1] while not test_creature.legal_move(test_world, food_location): food_direction = choice([[1, 0], [-1, 0], [0, 1], [0, -1]]) food_location = [creature_row - food_direction[0], creature_col - food_direction[1]] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) test_world.reset_food_level(food_location[0], food_location[1]) test_world.grow_all_food_level() submission_output = test_creature.find_nearby_food(test_world) proper_output = food_location self.assertEqual(proper_output, submission_output, "The test randomly places a food on one of the cells adjacent to the creature. Then test find_nearby_food method.")
def test_find_single_nearby_food_(self): for test_num in range(0, 100): world = [ [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 0) food_location = [-1, -1] while not test_creature.legal_move(test_world, food_location): food_direction = choice([[1, 0], [-1, 0], [0, 1], [0, -1]]) food_location = [ creature_row - food_direction[0], creature_col - food_direction[1] ] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) test_world.reset_food_level(food_location[0], food_location[1]) test_world.grow_all_food_level() submission_output = test_creature.find_nearby_food(test_world) proper_output = food_location self.assertEqual( proper_output, submission_output, "The test randomly places a food on one of the cells adjacent to the creature. Then test find_nearby_food method." )
def test_reset_food_level_then_grow(self): world = [ [ CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() test_world.reset_food_level(0, 0) self.assertEqual( test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level." ) test_world.grow_all_food_level() self.assertEqual( test_world.get_food_level(0, 0), FOOD_DEFAULT + FOOD_GROWTH, "grow_all_food_level method will reset the food level at (0,0) to the default level and then increase it by FOOD_GROWTH." ) test_world.reset_food_level(0, 0) self.assertEqual( test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level." )
def test_grow_all_food_level_one_food_random_worlds(self): world = [ [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) test_world.set_cell(food_row, food_col, CELL_FOOD) test_world.food_level[food_row][food_col] = FOOD_DEFAULT test_world.grow_all_food_level() for row_index, row in enumerate(test_world.grid): for cell_index, cell in enumerate(row): if cell == CELL_FOOD: self.assertEqual( test_world.get_food_level(row_index, cell_index), FOOD_GROWTH, "Testing grow_all_food_level method.") else: self.assertEqual( test_world.get_food_level(row_index, cell_index), 0, "Testing grow_all_food_level method - if the cell does not have food, get_food_level method returns 0." )
def test_grow_all_food_level_random_worlds_more_food(self): world = [ [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level for test_num in range(100): food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) test_world.set_cell(food_row, food_col, CELL_FOOD) test_world.food_level[food_row][food_col] = FOOD_DEFAULT prev_food_level = [] for row in test_world.food_level: prev_food_level.append(list(row)) test_world.grow_all_food_level() for row_index, row in enumerate(test_world.grid): for cell_index, cell in enumerate(row): if cell == CELL_FOOD: prev_val = prev_food_level[row_index][cell_index] self.assertEqual( test_world.get_food_level(row_index, cell_index), prev_val + FOOD_GROWTH, "Testing grow_all_food_level method.", ) else: self.assertEqual( test_world.get_food_level(row_index, cell_index), 0, "Testing grow_all_food_level method - if the cell does not have food, get_food_level method returns 0.", )
def test_grow_all_food_level_once(self): world = [[CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() self.assertEqual(test_world.get_food_level(0, 0), FOOD_GROWTH, "Testing grow_all_food_level method with one food(level:-1) placed at [0,0].")
def test_reset_food_level(self): world = [[CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() test_world.reset_food_level(0, 0) self.assertEqual(test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level.")
def test_reset_food_level(self): world = [ [ CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() test_world.reset_food_level(0, 0) self.assertEqual( test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level." )
def test_grow_all_food_level_once(self): world = [ [ CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() self.assertEqual( test_world.get_food_level(0, 0), FOOD_GROWTH, "Testing grow_all_food_level method with one food(level:-1) placed at [0,0]." )
def test_grow_all_food_level_one_food_random_worlds(self): world = [ [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) test_world.set_cell(food_row, food_col, CELL_FOOD) test_world.food_level[food_row][food_col] = FOOD_DEFAULT test_world.grow_all_food_level() for row_index, row in enumerate(test_world.grid): for cell_index, cell in enumerate(row): if cell == CELL_FOOD: self.assertEqual( test_world.get_food_level(row_index, cell_index), FOOD_GROWTH, "Testing grow_all_food_level method.", ) else: self.assertEqual( test_world.get_food_level(row_index, cell_index), 0, "Testing grow_all_food_level method - if the cell does not have food, get_food_level method returns 0.", )
def test_reset_food_level_then_grow(self): world = [[CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[5, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_world.grow_all_food_level() test_world.reset_food_level(0, 0) self.assertEqual(test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level.") test_world.grow_all_food_level() self.assertEqual(test_world.get_food_level(0, 0), FOOD_DEFAULT+FOOD_GROWTH, "grow_all_food_level method will reset the food level at (0,0) to the default level and then increase it by FOOD_GROWTH.") test_world.reset_food_level(0, 0) self.assertEqual(test_world.get_food_level(0, 0), FOOD_DEFAULT, "reset_food_level(0,0) will set the food level at (0,0) to the default level.")
def test_grow_all_food_level_random_worlds_more_food(self): world = [ [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level for test_num in range(100): food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) test_world.set_cell(food_row, food_col, CELL_FOOD) test_world.food_level[food_row][food_col] = FOOD_DEFAULT prev_food_level = [] for row in test_world.food_level: prev_food_level.append(list(row)) test_world.grow_all_food_level() for row_index, row in enumerate(test_world.grid): for cell_index, cell in enumerate(row): if cell == CELL_FOOD: prev_val = prev_food_level[row_index][cell_index] self.assertEqual( test_world.get_food_level(row_index, cell_index), prev_val + FOOD_GROWTH, "Testing grow_all_food_level method.") else: self.assertEqual( test_world.get_food_level(row_index, cell_index), 0, "Testing grow_all_food_level method - if the cell does not have food, get_food_level method returns 0." )