def test_move_and_eat_random_locations_not_hungry(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_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 0, "If the creature is not hungry, move_and_eat method does not change its hunger level.") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_GROWTH, "If the creature is not hungry, move_and_eat method does not change the level of the food.")
def test_set_cell(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 ], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.set_cell(0, 0, CELL_EMPTY) self.assertEqual(test_world.get_cell(0, 0), '', "Testing set_cell() method.") test_world.set_cell(0, 0, CELL_FOOD) self.assertEqual(test_world.get_cell(0, 0), 'FOOD', "Testing set_cell() method.")
def test_move_and_eat_random_locations_hungry(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), 1) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 1 - FOOD_GROWTH,"A creature (hunger level:1) at a random position moves and eat a food (level:0.025). Then the creature's hunger level becomes 1-0.025") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_DEFAULT, "After being eaten, the food level goes back to the FOOD_DEFAULT value.")
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_move_and_eat_random_locations_not_hungry(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_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 0, "If the creature is not hungry, move_and_eat method does not change its hunger level.") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_GROWTH, "If the creature is not hungry, move_and_eat method does not change the level of the food.")
def test_move_and_eat_random_locations_hungry(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), 1) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 1 - FOOD_GROWTH,"A creature (hunger level:1) at a random position moves and eat a food (level:0.025). Then the creature's hunger level becomes 1-0.025") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_DEFAULT, "After being eaten, the food level goes back to the FOOD_DEFAULT value.")
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_set_cell(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], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.set_cell(0, 0, CELL_EMPTY) self.assertEqual(test_world.get_cell(0, 0), '', "Testing set_cell() method.") test_world.set_cell(0, 0, CELL_FOOD) self.assertEqual(test_world.get_cell(0, 0), 'FOOD', "Testing set_cell() method.")
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_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." )