def test_move_and_eat_hungry(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(0, creature_hunger, "The creature (hunger level:1) moves and eats a food. Then its hunger level becomes 0.") self.assertEqual([0, 0], creature_location, "After move_and_eat(world,[0,0]) executed, the creature should be at [0,0].") self.assertEqual(FOOD_DEFAULT, food_level, "After move_and_eat(world,[0,0]) executed, the food at [0,0]'s level becomes the default value.")
def test_move_and_eat_no_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(6, 0) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(1, creature_hunger, "If there's no food at the destination, move_and_eat method does not change the hunger level of the creature.") self.assertEqual([0, 1], creature_location, "If there's no food at the destination, move_and_eat method does not change the location of the creature.") self.assertEqual(0, food_level, "If there's not food at the destination, get_food_level(destination) should return 0.")
def test_move_and_eat_not_hungry(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 = [[1, 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(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(0) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(0, creature_hunger, "The creature (hunger level:0) moves and eats a food. Then its hunger level stays 0.") self.assertEqual([0, 1], creature_location, "If not hungry, after move_and_eat(world,[0,0]) executed, the creature should stay at [0,1].") self.assertEqual(1, food_level, "If not hungry, the creature does not move and eat the food. Thus the food level stays same (1).")
def test_get_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 self.assertEqual(test_world.get_cell(0, 0), 'FOOD', "Testing get_cell() method on CELL_FODD.") self.assertEqual(test_world.get_cell(0, 1), '', "Testing get_cell() method on CELL_EMPTY")
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 = [['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] 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_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_get_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 self.assertEqual(test_world.get_cell(0, 0), 'FOOD', "Testing get_cell() method on CELL_FODD.") self.assertEqual(test_world.get_cell(0, 1), '', "Testing get_cell() method on CELL_EMPTY")
def test_move_and_eat_not_hungry(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 = [[1, 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(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(0) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 0, creature_hunger, "The creature (hunger level:0) moves and eats a food. Then its hunger level stays 0." ) self.assertEqual( [0, 1], creature_location, "If not hungry, after move_and_eat(world,[0,0]) executed, the creature should stay at [0,1]." ) self.assertEqual( 1, food_level, "If not hungry, the creature does not move and eat the food. Thus the food level stays same (1)." )
def test_move_and_eat_no_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(6, 0) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 1, creature_hunger, "If there's no food at the destination, move_and_eat method does not change the hunger level of the creature." ) self.assertEqual( [0, 1], creature_location, "If there's no food at the destination, move_and_eat method does not change the location of the creature." ) self.assertEqual( 0, food_level, "If there's not food at the destination, get_food_level(destination) should return 0." )
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_hungry(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 0, creature_hunger, "The creature (hunger level:1) moves and eats a food. Then its hunger level becomes 0." ) self.assertEqual( [0, 0], creature_location, "After move_and_eat(world,[0,0]) executed, the creature should be at [0,0]." ) self.assertEqual( FOOD_DEFAULT, food_level, "After move_and_eat(world,[0,0]) executed, the food at [0,0]'s level becomes the default value." )
def test_world_str_empty(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], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world proper_output = '[[\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\']]\n' print world submission_output = self.contained_output.getvalue() self.assertEqual(proper_output, submission_output, "Testing str() method of an empty World instance.") self.contained_output.truncate(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_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_world_str_basic(self): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world proper_output = '[[\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\']]\n' print world submission_output = self.contained_output.getvalue() self.assertEqual(proper_output, submission_output, "Testing str() method of an World instance whose cells on the 5th column contain food.") self.contained_output.truncate(0)
def test_legal_move(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_creature = Creature([0, 0], 0) self.assertTrue(test_creature.legal_move(test_world, (0, 0)), "Testing legal_move method for (0,0) - legal.") self.assertTrue(test_creature.legal_move(test_world, (5, 5)), "Testing legal_move method for (5,5) - legal.") self.assertFalse(test_creature.legal_move(test_world, (-1, -1)), "Testing legal_move method for (-1,-1) - illegal.") self.assertFalse(test_creature.legal_move(test_world, (6, 6)), "Testing legal_move method for (6,6) - illegal.")
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_wander_basic(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], ] test_world = World(6, 1) test_world.grid = world creature_row = randrange(0, WORLD_DIM) creature_col = randrange(0, WORLD_DIM) test_creature = Creature([creature_row, creature_col], 0) old_location = test_creature.get_location() test_creature.wander(test_world) new_location = test_creature.get_location() self.assertTrue(abs(old_location[0] - new_location[0]) <= 1 and abs(old_location[1] - new_location[1]) <= 1, "creature.wander() method moves the creature to an adjacent cell. The total difference of x and y coordinates (before and after wander()) should be equal or less than 1. ")
def test_get_food_level_empty_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 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.food_level = food_level self.assertEqual(test_world.get_food_level(0, 0), 0, "Testing get_food_level() method on an empty cell.")
def test_world_str_empty(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], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world proper_output = '[[\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\'], [\'\', \'\', \'\', \'\', \'\']]\n' print world submission_output = self.contained_output.getvalue() self.assertEqual(proper_output, submission_output, "Testing str() method of an empty World instance.") self.contained_output.truncate(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_world_str_basic(self): world = [ [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_FOOD], ] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world proper_output = '[[\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\'], [\'\', \'\', \'\', \'\', \'FOOD\']]\n' print world submission_output = self.contained_output.getvalue() self.assertEqual( proper_output, submission_output, "Testing str() method of an World instance whose cells on the 5th column contain food." ) self.contained_output.truncate(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_find_single_nearby_food_(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_creature = Creature((0, 1), 0) proper_output = [0, 0] submission_output = test_creature.find_nearby_food(test_world) self.assertEqual(proper_output, submission_output, "When creature is at (0,1) and food is on (0,0), find_nearby_food should return [0,0].")
def test_cant_find_far_food_(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([4, 4], 0) proper_output = None submission_output = test_creature.find_nearby_food(test_world) self.assertEqual(proper_output, submission_output, "When the food is far away from the creature, find_nearby_food should return None.")
def test_cant_find_far_food_(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([4, 4], 0) proper_output = None submission_output = test_creature.find_nearby_food(test_world) self.assertEqual( proper_output, submission_output, "When the food is far away from the creature, find_nearby_food should return None." )
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_find_single_nearby_food_(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 = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level test_creature = Creature((0, 1), 0) proper_output = [0, 0] submission_output = test_creature.find_nearby_food(test_world) self.assertEqual( proper_output, submission_output, "When creature is at (0,1) and food is on (0,0), find_nearby_food should return [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_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_legal_move(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_creature = Creature([0, 0], 0) self.assertTrue(test_creature.legal_move(test_world, (0, 0)), "Testing legal_move method for (0,0) - legal.") self.assertTrue(test_creature.legal_move(test_world, (5, 5)), "Testing legal_move method for (5,5) - legal.") self.assertFalse(test_creature.legal_move(test_world, (-1, -1)), "Testing legal_move method for (-1,-1) - illegal.") self.assertFalse(test_creature.legal_move(test_world, (6, 6)), "Testing legal_move method for (6,6) - illegal.")
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_get_food_level_empty_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 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.food_level = food_level self.assertEqual(test_world.get_food_level(0, 0), 0, "Testing get_food_level() method on an empty cell.")
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." )